fix: Fix IDE layer not resolving some macro calls

This commit is contained in:
Lukas Wirth 2025-05-28 06:56:48 +02:00
parent 93182f4670
commit bbbcfaab8b
5 changed files with 62 additions and 35 deletions

View File

@ -75,7 +75,7 @@ impl TraitItems {
})
}
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
pub fn macro_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
}
}
@ -109,7 +109,7 @@ impl ImplItems {
(Arc::new(ImplItems { items, macro_calls }), DefDiagnostics::new(diagnostics))
}
pub fn attribute_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
pub fn macro_calls(&self) -> impl Iterator<Item = (AstId<ast::Item>, MacroCallId)> + '_ {
self.macro_calls.iter().flat_map(|it| it.iter()).copied()
}
}

View File

@ -408,11 +408,7 @@ impl<'db> SemanticsImpl<'db> {
}
pub fn expand_macro_call(&self, macro_call: &ast::MacroCall) -> Option<InFile<SyntaxNode>> {
let sa = self.analyze_no_infer(macro_call.syntax())?;
let macro_call = InFile::new(sa.file_id, macro_call);
let file_id = sa.expansion(self.db, macro_call)?;
let file_id = self.to_def(macro_call)?;
let node = self.parse_or_expand(file_id.into());
Some(InFile::new(file_id.into(), node))
}
@ -434,10 +430,7 @@ impl<'db> SemanticsImpl<'db> {
&self,
macro_call: &ast::MacroCall,
) -> Option<ExpandResult<SyntaxNode>> {
let sa = self.analyze_no_infer(macro_call.syntax())?;
let macro_call = InFile::new(sa.file_id, macro_call);
let file_id = sa.expansion(self.db, macro_call)?;
let file_id = self.to_def(macro_call)?;
let macro_call = self.db.lookup_intern_macro_call(file_id);
let skip = matches!(
@ -1097,16 +1090,7 @@ impl<'db> SemanticsImpl<'db> {
let file_id = match m_cache.get(&mcall) {
Some(&it) => it,
None => {
let it = token
.parent()
.and_then(|parent| {
self.analyze_impl(
InFile::new(expansion, &parent),
None,
false,
)
})?
.expansion(self.db, mcall.as_ref())?;
let it = ast::MacroCall::to_def(self, mcall.as_ref())?;
m_cache.insert(mcall, it);
it
}
@ -1562,14 +1546,8 @@ impl<'db> SemanticsImpl<'db> {
}
pub fn resolve_macro_call_arm(&self, macro_call: &ast::MacroCall) -> Option<u32> {
let sa = self.analyze(macro_call.syntax())?;
self.db
.parse_macro_expansion(
sa.expansion(self.db, self.wrap_node_infile(macro_call.clone()).as_ref())?,
)
.value
.1
.matched_arm
let file_id = self.to_def(macro_call)?;
self.db.parse_macro_expansion(file_id).value.1.matched_arm
}
pub fn get_unsafe_ops(&self, def: DefWithBody) -> FxHashSet<ExprOrPatSource> {

View File

@ -36,9 +36,14 @@ impl ChildBySource for TraitId {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
let data = db.trait_items(*self);
data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
data.macro_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
|(ast_id, call_id)| {
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_ptr(db), call_id);
let ptr = ast_id.to_ptr(db);
if let Some(ptr) = ptr.cast::<ast::MacroCall>() {
res[keys::MACRO_CALL].insert(ptr, call_id);
} else {
res[keys::ATTR_MACRO_CALL].insert(ptr, call_id);
}
},
);
data.items.iter().for_each(|&(_, item)| {
@ -50,10 +55,14 @@ impl ChildBySource for TraitId {
impl ChildBySource for ImplId {
fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) {
let data = db.impl_items(*self);
// FIXME: Macro calls
data.attribute_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
data.macro_calls().filter(|(ast_id, _)| ast_id.file_id == file_id).for_each(
|(ast_id, call_id)| {
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_ptr(db), call_id);
let ptr = ast_id.to_ptr(db);
if let Some(ptr) = ptr.cast::<ast::MacroCall>() {
res[keys::MACRO_CALL].insert(ptr, call_id);
} else {
res[keys::ATTR_MACRO_CALL].insert(ptr, call_id);
}
},
);
data.items.iter().for_each(|&(_, item)| {

View File

@ -719,4 +719,44 @@ __log!(written:%; "Test"$0);
"#]],
);
}
#[test]
fn assoc_call() {
check(
r#"
macro_rules! mac {
() => { fn assoc() {} }
}
impl () {
mac$0!();
}
"#,
expect![[r#"
mac!
fn assoc(){}"#]],
);
}
#[test]
fn eager() {
check(
r#"
//- minicore: concat
macro_rules! my_concat {
($head:expr, $($tail:tt)*) => { concat!($head, $($tail)*) };
}
fn test() {
_ = my_concat!(
conc$0at!("<", ">"),
"hi",
);
}
"#,
expect![[r#"
my_concat!
"<>hi""#]],
);
}
}

View File

@ -1023,7 +1023,7 @@ impl flags::AnalysisStats {
percentage(num_pats_partially_unknown, num_pats),
num_pat_type_mismatches
);
eprintln!(" panics: {}", panics);
eprintln!(" panics: {panics}");
eprintln!("{:<20} {}", "Inference:", inference_time);
report_metric("unknown type", num_exprs_unknown, "#");
report_metric("type mismatches", num_expr_type_mismatches, "#");