Unnamed repository; edit this file 'description' to name the repository.
Fix mod item in included file resolving incorrectly
Lukas Wirth 2023-12-02
parent d2a31ac · commit 5edf7bd
-rw-r--r--crates/hir-def/src/nameres/mod_resolution.rs2
-rw-r--r--crates/hir-expand/src/builtin_fn_macro.rs25
-rw-r--r--crates/hir-expand/src/lib.rs29
-rw-r--r--crates/hir-ty/src/tests/macros.rs1
-rw-r--r--crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs16
-rw-r--r--crates/rust-analyzer/src/integrated_benchmarks.rs2
6 files changed, 39 insertions, 36 deletions
diff --git a/crates/hir-def/src/nameres/mod_resolution.rs b/crates/hir-def/src/nameres/mod_resolution.rs
index 22802433aa..9dcb971729 100644
--- a/crates/hir-def/src/nameres/mod_resolution.rs
+++ b/crates/hir-def/src/nameres/mod_resolution.rs
@@ -66,7 +66,7 @@ impl ModDir {
attr_path: Option<&SmolStr>,
) -> Result<(FileId, bool, ModDir), Box<[String]>> {
let name = name.unescaped();
- let orig_file_id = file_id.original_file(db.upcast());
+ let orig_file_id = file_id.original_file_respecting_includes(db.upcast());
let mut candidate_files = ArrayVec::<_, 2>::new();
match attr_path {
diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs
index 74ca2f7ec4..4b2f27bd46 100644
--- a/crates/hir-expand/src/builtin_fn_macro.rs
+++ b/crates/hir-expand/src/builtin_fn_macro.rs
@@ -578,18 +578,12 @@ fn include_expand(
tt: &tt::Subtree,
span: SpanData,
) -> ExpandResult<tt::Subtree> {
- let path = match parse_string(tt) {
+ let file_id = match include_input_to_file_id(db, arg_id, tt) {
Ok(it) => it,
Err(e) => {
return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
}
};
- let file_id = match relative_file(db, arg_id, &path, false) {
- Ok(file_id) => file_id,
- Err(e) => {
- return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e);
- }
- };
match parse_to_token_tree(
SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
SyntaxContextId::ROOT,
@@ -603,19 +597,20 @@ fn include_expand(
}
}
+pub fn include_input_to_file_id(
+ db: &dyn ExpandDatabase,
+ arg_id: MacroCallId,
+ arg: &tt::Subtree,
+) -> Result<FileId, ExpandError> {
+ relative_file(db, arg_id, &parse_string(arg)?, false)
+}
+
fn include_bytes_expand(
_db: &dyn ExpandDatabase,
_arg_id: MacroCallId,
- tt: &tt::Subtree,
+ _tt: &tt::Subtree,
span: SpanData,
) -> ExpandResult<tt::Subtree> {
- let _path = match parse_string(tt) {
- Ok(it) => it,
- Err(e) => {
- return ExpandResult::new(tt::Subtree::empty(DelimSpan { open: span, close: span }), e)
- }
- };
-
// FIXME: actually read the file here if the user asked for macro expansion
let res = tt::Subtree {
delimiter: tt::Delimiter::dummy_invisible(),
diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs
index d743f2adae..9caf5513bf 100644
--- a/crates/hir-expand/src/lib.rs
+++ b/crates/hir-expand/src/lib.rs
@@ -136,7 +136,7 @@ pub enum MacroDefKind {
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
-struct EagerCallInfo {
+pub struct EagerCallInfo {
/// The expanded argument of the eager macro.
arg: Arc<tt::Subtree>,
/// 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 {
/// expansion originated from.
fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId;
+ fn original_file_respecting_includes(self, db: &dyn db::ExpandDatabase) -> FileId;
+
/// If this is a macro call, returns the syntax node of the call.
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>>;
@@ -215,6 +217,29 @@ impl HirFileIdExt for HirFileId {
}
}
+ fn original_file_respecting_includes(mut self, db: &dyn db::ExpandDatabase) -> FileId {
+ loop {
+ match self.repr() {
+ base_db::span::HirFileIdRepr::FileId(id) => break id,
+ base_db::span::HirFileIdRepr::MacroFile(file) => {
+ let loc = db.lookup_intern_macro_call(file.macro_call_id);
+ if loc.def.is_include() {
+ if let Some(eager) = &loc.eager {
+ if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
+ db,
+ file.macro_call_id,
+ &eager.arg,
+ ) {
+ break it;
+ }
+ }
+ }
+ self = loc.kind.file_id();
+ }
+ }
+ }
+ }
+
fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> {
let macro_file = self.macro_file()?;
let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id);
@@ -473,7 +498,7 @@ impl MacroCallKind {
}
/// Returns the file containing the macro invocation.
- fn file_id(&self) -> HirFileId {
+ pub fn file_id(&self) -> HirFileId {
match *self {
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
diff --git a/crates/hir-ty/src/tests/macros.rs b/crates/hir-ty/src/tests/macros.rs
index 8091017529..1e10a6feca 100644
--- a/crates/hir-ty/src/tests/macros.rs
+++ b/crates/hir-ty/src/tests/macros.rs
@@ -787,7 +787,6 @@ fn main() {
}
#[test]
-#[should_panic] // FIXME
fn infer_builtin_macros_include_child_mod() {
check_types(
r#"
diff --git a/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs b/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs
index edcfa073a0..33e7c2e37c 100644
--- a/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs
+++ b/crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs
@@ -70,20 +70,4 @@ self::m!(); self::m2!();
"#,
);
}
-
- #[test]
- #[should_panic] // FIXME: https://github.com/rust-lang/rust-analyzer/issues/14968
- fn include_does_not_break_diagnostics() {
- check_diagnostics(
- r#"
-//- minicore: include
-//- /lib.rs crate:lib
-include!("include-me.rs");
-//- /include-me.rs
-/// long doc that pushes the diagnostic range beyond the first file's text length
-#[err]
-mod prim_never {}
-"#,
- );
- }
}
diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs
index a865d9e4ab..ed2cf07551 100644
--- a/crates/rust-analyzer/src/integrated_benchmarks.rs
+++ b/crates/rust-analyzer/src/integrated_benchmarks.rs
@@ -30,7 +30,7 @@ fn integrated_highlighting_benchmark() {
// Load rust-analyzer itself.
let workspace_to_load = project_root();
- let file = "./crates/ide-db/src/apply_change.rs";
+ let file = "./crates/rust-analyzer/src/config.rs";
let cargo_config = CargoConfig::default();
let load_cargo_config = LoadCargoConfig {