Fix mod item in included file resolving incorrectly

This commit is contained in:
Lukas Wirth 2023-12-02 13:34:40 +01:00
parent d2a31acda1
commit 5edf7bddc6
6 changed files with 39 additions and 36 deletions

View File

@ -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 {

View File

@ -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(),

View File

@ -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, .. }, .. }

View File

@ -787,7 +787,6 @@ fn main() {
}
#[test]
#[should_panic] // FIXME
fn infer_builtin_macros_include_child_mod() {
check_types(
r#"

View File

@ -67,22 +67,6 @@ macro_rules! m { () => {} } }
self::m!(); self::m2!();
//^^ error: unresolved macro `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 {}
"#,
);
}

View File

@ -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 {