Skip to content

Commit 0263e98

Browse files
committed
fix: Name resolution failures inside include!d files
1 parent 8dd53a3 commit 0263e98

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

crates/hir-def/src/nameres/collector.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,20 @@ impl DefCollector<'_> {
14251425
tracing::warn!("macro expansion is too deep");
14261426
return;
14271427
}
1428-
let file_id = macro_call_id.as_file();
1428+
1429+
let loc = macro_call_id.lookup(self.db.upcast());
1430+
let file_id = if loc.def.is_include() {
1431+
// If the given macro is an builtin `include!` macro, we expand into the included file.
1432+
// So we should collect items within it.
1433+
if let Some(it) = loc.include_file_id(self.db.upcast(), macro_call_id) {
1434+
it.into()
1435+
} else {
1436+
// This case the file is not exist. Do not collect items
1437+
return;
1438+
}
1439+
} else {
1440+
macro_call_id.as_file()
1441+
};
14291442

14301443
let item_tree = self.db.file_item_tree(file_id);
14311444

crates/hir/src/semantics/source_to_def.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ use hir_expand::{
104104
};
105105
use rustc_hash::FxHashMap;
106106
use smallvec::SmallVec;
107-
use span::{FileId, MacroFileId};
107+
use span::{EditionedFileId, FileId, MacroFileId};
108108
use stdx::impl_from;
109109
use syntax::{
110110
ast::{self, HasName},
@@ -118,6 +118,7 @@ pub(super) struct SourceToDefCache {
118118
pub(super) dynmap_cache: FxHashMap<(ChildContainer, HirFileId), DynMap>,
119119
expansion_info_cache: FxHashMap<MacroFileId, ExpansionInfo>,
120120
pub(super) file_to_def_cache: FxHashMap<FileId, SmallVec<[ModuleId; 1]>>,
121+
included_file_cache: FxHashMap<EditionedFileId, MacroFileId>,
121122
}
122123

123124
impl SourceToDefCache {
@@ -163,7 +164,11 @@ impl SourceToDefCtx<'_, '_> {
163164
.include_macro_invoc(crate_id)
164165
.iter()
165166
.filter(|&&(_, file_id)| file_id == file)
166-
.flat_map(|(call, _)| {
167+
.flat_map(|(call, file_id)| {
168+
self.cache
169+
.included_file_cache
170+
.insert(*file_id, MacroFileId { macro_call_id: *call });
171+
167172
modules(
168173
call.lookup(self.db.upcast())
169174
.kind
@@ -499,7 +504,12 @@ impl SourceToDefCtx<'_, '_> {
499504
let parent = |this: &mut Self, node: InFile<&SyntaxNode>| match node.value.parent() {
500505
Some(parent) => Some(node.with_value(parent)),
501506
None => {
502-
let macro_file = node.file_id.macro_file()?;
507+
let macro_file = if let Some(macro_file) = node.file_id.macro_file() {
508+
macro_file
509+
} else {
510+
let file_id = node.file_id.file_id()?;
511+
*this.cache.included_file_cache.get(&file_id)?
512+
};
503513

504514
let expansion_info = this
505515
.cache

crates/ide/src/goto_definition.rs

+43
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,49 @@ fn func_in_include() {
497497
//^^^^^^^^^^^^^^^
498498
}
499499
500+
fn foo() {
501+
func_in_include$0();
502+
}
503+
"#,
504+
);
505+
}
506+
507+
#[test]
508+
fn goto_def_in_included_file_inside_mod() {
509+
check(
510+
r#"
511+
//- minicore:include
512+
//- /main.rs
513+
514+
mod a {
515+
include!("b.rs");
516+
}
517+
518+
//- /b.rs
519+
fn func_in_include() {
520+
//^^^^^^^^^^^^^^^
521+
}
522+
523+
fn foo() {
524+
func_in_include$0();
525+
}
526+
"#,
527+
);
528+
529+
check(
530+
r#"
531+
//- minicore:include
532+
//- /main.rs
533+
534+
mod a {
535+
include!("a.rs");
536+
}
537+
538+
//- /a.rs
539+
fn func_in_include() {
540+
//^^^^^^^^^^^^^^^
541+
}
542+
500543
fn foo() {
501544
func_in_include$0();
502545
}

crates/ide/src/references.rs

+23
Original file line numberDiff line numberDiff line change
@@ -2750,4 +2750,27 @@ impl Foo {
27502750
"#]],
27512751
);
27522752
}
2753+
2754+
#[test]
2755+
fn goto_ref_on_included_file() {
2756+
check(
2757+
r#"
2758+
//- minicore:include
2759+
//- /lib.rs
2760+
include!("foo.rs");
2761+
2762+
fn howdy() {
2763+
let _ = FOO;
2764+
}
2765+
2766+
//- /foo.rs
2767+
const FOO$0: i32 = 0;
2768+
"#,
2769+
expect![[r#"
2770+
FOO Const FileId(1) 0..19 6..9
2771+
2772+
FileId(0) 46..49
2773+
"#]],
2774+
);
2775+
}
27532776
}

0 commit comments

Comments
 (0)