Skip to content

Commit

Permalink
Fix mod item in included file resolving incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Dec 2, 2023
1 parent d2a31ac commit 7c6897f
Show file tree
Hide file tree
Showing 6 changed files with 70,860 additions and 35 deletions.
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/mod_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 10 additions & 15 deletions crates/hir-expand/src/builtin_fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand Down
29 changes: 27 additions & 2 deletions crates/hir-expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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>>;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, .. }, .. }
Expand Down
1 change: 0 additions & 1 deletion crates/hir-ty/src/tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ fn main() {
}

#[test]
#[should_panic] // FIXME
fn infer_builtin_macros_include_child_mod() {
check_types(
r#"
Expand Down
16 changes: 0 additions & 16 deletions crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
"#,
);
}
Expand Down
Loading

0 comments on commit 7c6897f

Please sign in to comment.