Skip to content

Commit 5edf7bd

Browse files
committed
Fix mod item in included file resolving incorrectly
1 parent d2a31ac commit 5edf7bd

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl ModDir {
6666
attr_path: Option<&SmolStr>,
6767
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
6868
let name = name.unescaped();
69-
let orig_file_id = file_id.original_file(db.upcast());
69+
let orig_file_id = file_id.original_file_respecting_includes(db.upcast());
7070

7171
let mut candidate_files = ArrayVec::<_, 2>::new();
7272
match attr_path {

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,12 @@ fn include_expand(
578578
tt: &tt::Subtree,
579579
span: SpanData,
580580
) -> ExpandResult<tt::Subtree> {
581-
let path = match parse_string(tt) {
581+
let file_id = match include_input_to_file_id(db, arg_id, tt) {
582582
Ok(it) => it,
583583
Err(e) => {
584584
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
585585
}
586586
};
587-
let file_id = match relative_file(db, arg_id, &path, false) {
588-
Ok(file_id) => file_id,
589-
Err(e) => {
590-
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e);
591-
}
592-
};
593587
match parse_to_token_tree(
594588
SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
595589
SyntaxContextId::ROOT,
@@ -603,19 +597,20 @@ fn include_expand(
603597
}
604598
}
605599

600+
pub fn include_input_to_file_id(
601+
db: &dyn ExpandDatabase,
602+
arg_id: MacroCallId,
603+
arg: &tt::Subtree,
604+
) -> Result<FileId, ExpandError> {
605+
relative_file(db, arg_id, &parse_string(arg)?, false)
606+
}
607+
606608
fn include_bytes_expand(
607609
_db: &dyn ExpandDatabase,
608610
_arg_id: MacroCallId,
609-
tt: &tt::Subtree,
611+
_tt: &tt::Subtree,
610612
span: SpanData,
611613
) -> ExpandResult<tt::Subtree> {
612-
let _path = match parse_string(tt) {
613-
Ok(it) => it,
614-
Err(e) => {
615-
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
616-
}
617-
};
618-
619614
// FIXME: actually read the file here if the user asked for macro expansion
620615
let res = tt::Subtree {
621616
delimiter: tt::Delimiter::dummy_invisible(),

crates/hir-expand/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub enum MacroDefKind {
136136
}
137137

138138
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
139-
struct EagerCallInfo {
139+
pub struct EagerCallInfo {
140140
/// The expanded argument of the eager macro.
141141
arg: Arc<tt::Subtree>,
142142
/// Call id of the eager macro's input file (this is the macro file for its fully expanded input).
@@ -176,6 +176,8 @@ pub trait HirFileIdExt {
176176
/// expansion originated from.
177177
fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId;
178178

179+
fn original_file_respecting_includes(self, db: &dyn db::ExpandDatabase) -> FileId;
180+
179181
/// If this is a macro call, returns the syntax node of the call.
180182
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>>;
181183

@@ -215,6 +217,29 @@ impl HirFileIdExt for HirFileId {
215217
}
216218
}
217219

220+
fn original_file_respecting_includes(mut self, db: &dyn db::ExpandDatabase) -> FileId {
221+
loop {
222+
match self.repr() {
223+
base_db::span::HirFileIdRepr::FileId(id) => break id,
224+
base_db::span::HirFileIdRepr::MacroFile(file) => {
225+
let loc = db.lookup_intern_macro_call(file.macro_call_id);
226+
if loc.def.is_include() {
227+
if let Some(eager) = &loc.eager {
228+
if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
229+
db,
230+
file.macro_call_id,
231+
&eager.arg,
232+
) {
233+
break it;
234+
}
235+
}
236+
}
237+
self = loc.kind.file_id();
238+
}
239+
}
240+
}
241+
}
242+
218243
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> {
219244
let macro_file = self.macro_file()?;
220245
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
@@ -473,7 +498,7 @@ impl MacroCallKind {
473498
}
474499

475500
/// Returns the file containing the macro invocation.
476-
fn file_id(&self) -> HirFileId {
501+
pub fn file_id(&self) -> HirFileId {
477502
match *self {
478503
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
479504
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }

crates/hir-ty/src/tests/macros.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,6 @@ fn main() {
787787
}
788788

789789
#[test]
790-
#[should_panic] // FIXME
791790
fn infer_builtin_macros_include_child_mod() {
792791
check_types(
793792
r#"

crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,6 @@ macro_rules! m { () => {} } }
6767
6868
self::m!(); self::m2!();
6969
//^^ error: unresolved macro `self::m2!`
70-
"#,
71-
);
72-
}
73-
74-
#[test]
75-
#[should_panic] // FIXME: https://github.com/rust-lang/rust-analyzer/issues/14968
76-
fn include_does_not_break_diagnostics() {
77-
check_diagnostics(
78-
r#"
79-
//- minicore: include
80-
//- /lib.rs crate:lib
81-
include!("include-me.rs");
82-
//- /include-me.rs
83-
/// long doc that pushes the diagnostic range beyond the first file's text length
84-
#[err]
85-
mod prim_never {}
8670
"#,
8771
);
8872
}

crates/rust-analyzer/src/integrated_benchmarks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn integrated_highlighting_benchmark() {
3030

3131
// Load rust-analyzer itself.
3232
let workspace_to_load = project_root();
33-
let file = "./crates/ide-db/src/apply_change.rs";
33+
let file = "./crates/rust-analyzer/src/config.rs";
3434

3535
let cargo_config = CargoConfig::default();
3636
let load_cargo_config = LoadCargoConfig {

0 commit comments

Comments
 (0)