Skip to content

Commit 597d8e8

Browse files
committed
Fix IDE layer not correctly resolving opt-in extern crates
1 parent 727555f commit 597d8e8

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

crates/hir-def/src/data.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,14 +506,17 @@ impl ExternCrateDeclData {
506506
let crate_id = if name == sym::self_.clone() {
507507
Some(krate)
508508
} else {
509-
db.crate_def_map(krate)
510-
.extern_prelude()
511-
.find(|&(prelude_name, ..)| *prelude_name == name)
512-
.map(|(_, (root, _))| root.krate())
509+
db.crate_graph()[krate].dependencies.iter().find_map(|dep| {
510+
if dep.name.symbol() == name.symbol() {
511+
Some(dep.crate_id)
512+
} else {
513+
None
514+
}
515+
})
513516
};
514517

515518
Arc::new(Self {
516-
name: extern_crate.name.clone(),
519+
name,
517520
visibility: item_tree[extern_crate.visibility].clone(),
518521
alias: extern_crate.alias.clone(),
519522
crate_id,

crates/ide/src/syntax_highlighting/test_data/highlight_extern_crate.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@
4949
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">std</span><span class="semicolon">;</span>
5050
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">alloc</span> <span class="keyword">as</span> <span class="module crate_root declaration">abc</span><span class="semicolon">;</span>
5151
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="unresolved_reference">unresolved</span> <span class="keyword">as</span> <span class="module crate_root declaration">definitely_unresolved</span><span class="semicolon">;</span>
52-
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="unresolved_reference">test</span> <span class="keyword">as</span> <span class="module crate_root declaration">opt_in_crate</span><span class="semicolon">;</span>
52+
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="unresolved_reference">unresolved</span> <span class="keyword">as</span> <span class="punctuation">_</span><span class="semicolon">;</span>
53+
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">test</span> <span class="keyword">as</span> <span class="module crate_root declaration">opt_in_crate</span><span class="semicolon">;</span>
54+
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">test</span> <span class="keyword">as</span> <span class="punctuation">_</span><span class="semicolon">;</span>
55+
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">proc_macro</span><span class="semicolon">;</span>
5356
</code></pre>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,18 +874,23 @@ pub fn block_comments2() {}
874874
fn test_extern_crate() {
875875
check_highlighting(
876876
r#"
877-
//- /main.rs crate:main deps:std,alloc,test extern-prelude:std,alloc
877+
//- /main.rs crate:main deps:std,alloc,test,proc_macro extern-prelude:std,alloc
878878
extern crate self as this;
879879
extern crate std;
880880
extern crate alloc as abc;
881881
extern crate unresolved as definitely_unresolved;
882+
extern crate unresolved as _;
882883
extern crate test as opt_in_crate;
884+
extern crate test as _;
885+
extern crate proc_macro;
883886
//- /std/lib.rs crate:std
884887
pub struct S;
885888
//- /alloc/lib.rs crate:alloc
886889
pub struct A;
887890
//- /test/lib.rs crate:test
888891
pub struct T;
892+
//- /proc_macro/lib.rs crate:proc_macro
893+
pub struct ProcMacro;
889894
"#,
890895
expect_file!["./test_data/highlight_extern_crate.html"],
891896
false,

0 commit comments

Comments
 (0)