From 96b3402e1bc59351da5117fecc95188009d90600 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 5 Oct 2024 15:34:05 +0200 Subject: [PATCH] Fix IDE layer not correctly resolving opt-in extern crates --- src/tools/rust-analyzer/crates/hir-def/src/data.rs | 13 ++++++++----- .../test_data/highlight_extern_crate.html | 5 ++++- .../crates/ide/src/syntax_highlighting/tests.rs | 7 ++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/tools/rust-analyzer/crates/hir-def/src/data.rs b/src/tools/rust-analyzer/crates/hir-def/src/data.rs index d17ebd7ff920..3ecb57c75671 100644 --- a/src/tools/rust-analyzer/crates/hir-def/src/data.rs +++ b/src/tools/rust-analyzer/crates/hir-def/src/data.rs @@ -506,14 +506,17 @@ impl ExternCrateDeclData { let crate_id = if name == sym::self_.clone() { Some(krate) } else { - db.crate_def_map(krate) - .extern_prelude() - .find(|&(prelude_name, ..)| *prelude_name == name) - .map(|(_, (root, _))| root.krate()) + db.crate_graph()[krate].dependencies.iter().find_map(|dep| { + if dep.name.symbol() == name.symbol() { + Some(dep.crate_id) + } else { + None + } + }) }; Arc::new(Self { - name: extern_crate.name.clone(), + name, visibility: item_tree[extern_crate.visibility].clone(), alias: extern_crate.alias.clone(), crate_id, diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html index 0c08a7d5ecd4..7820e4e5a5f9 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html @@ -49,5 +49,8 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd extern crate std; extern crate alloc as abc; extern crate unresolved as definitely_unresolved; -extern crate test as opt_in_crate; +extern crate unresolved as _; +extern crate test as opt_in_crate; +extern crate test as _; +extern crate proc_macro; \ No newline at end of file diff --git a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs index 3b2421a095fe..a20147add362 100644 --- a/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs +++ b/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/tests.rs @@ -874,18 +874,23 @@ pub fn block_comments2() {} fn test_extern_crate() { check_highlighting( r#" -//- /main.rs crate:main deps:std,alloc,test extern-prelude:std,alloc +//- /main.rs crate:main deps:std,alloc,test,proc_macro extern-prelude:std,alloc extern crate self as this; extern crate std; extern crate alloc as abc; extern crate unresolved as definitely_unresolved; +extern crate unresolved as _; extern crate test as opt_in_crate; +extern crate test as _; +extern crate proc_macro; //- /std/lib.rs crate:std pub struct S; //- /alloc/lib.rs crate:alloc pub struct A; //- /test/lib.rs crate:test pub struct T; +//- /proc_macro/lib.rs crate:proc_macro +pub struct ProcMacro; "#, expect_file!["./test_data/highlight_extern_crate.html"], false,