Merge pull request #19879 from Veykril/push-mqykxnqtktuw

fix: Fix IDE layer not resolving some macro calls
This commit is contained in:
Lukas Wirth 2025-05-28 05:20:02 +00:00 committed by GitHub
commit a420ef2b17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 65 additions and 84 deletions

View File

@ -422,7 +422,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
block: loc.container.block.map(|block| salsa::plumbing::AsId::as_id(&block)),
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
local_inner: false,
allow_internal_unsafe: loc.allow_internal_unsafe,
@ -436,7 +435,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
block: loc.container.block.map(|block| salsa::plumbing::AsId::as_id(&block)),
kind: kind(loc.expander, loc.id.file_id(), makro.ast_id.upcast()),
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
allow_internal_unsafe: loc
@ -452,7 +450,6 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
let makro = &item_tree[loc.id.value];
MacroDefId {
krate: loc.container.krate,
block: None,
kind: MacroDefKind::ProcMacro(
InFile::new(loc.id.file_id(), makro.ast_id),
loc.expander,

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

@ -696,15 +696,6 @@ impl<'db> Resolver<'db> {
&def_map[local_id].scope
}
pub fn item_scopes(&self) -> impl Iterator<Item = &ItemScope> {
self.scopes()
.filter_map(move |scope| match scope {
Scope::BlockScope(m) => Some(&m.def_map[m.module_id].scope),
_ => None,
})
.chain(std::iter::once(&self.module_scope.def_map[self.module_scope.module_id].scope))
}
pub fn krate(&self) -> Crate {
self.module_scope.def_map.krate()
}

View File

@ -258,8 +258,6 @@ pub struct MacroCallLoc {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
pub krate: Crate,
// FIXME: In `hir-expand` we can't refer to `BlockId`.
pub block: Option<salsa::Id>,
pub edition: Edition,
pub kind: MacroDefKind,
pub local_inner: bool,

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!(
@ -574,9 +567,7 @@ impl<'db> SemanticsImpl<'db> {
speculative_args: &ast::TokenTree,
token_to_map: SyntaxToken,
) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> {
let analyzer = self.analyze_no_infer(actual_macro_call.syntax())?;
let macro_call = InFile::new(analyzer.file_id, actual_macro_call);
let macro_file = analyzer.expansion(self.db, macro_call)?;
let macro_file = self.to_def(actual_macro_call)?;
hir_expand::db::expand_speculative(
self.db,
macro_file,
@ -1097,16 +1088,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
}
@ -1551,9 +1533,6 @@ impl<'db> SemanticsImpl<'db> {
.and_then(|call| macro_call_to_macro_id(ctx, call))
.map(Into::into)
})
.or_else(|| {
self.analyze(macro_call.value.syntax())?.resolve_macro_call(self.db, macro_call)
})
}
pub fn is_proc_macro_call(&self, macro_call: InFile<&ast::MacroCall>) -> bool {
@ -1562,14 +1541,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

@ -26,12 +26,12 @@ use hir_def::{
},
hir::{BindingId, Expr, ExprId, ExprOrPatId, Pat},
lang_item::LangItem,
nameres::{MacroSubNs, block_def_map, crate_def_map},
nameres::MacroSubNs,
resolver::{HasResolver, Resolver, TypeNs, ValueNs, resolver_for_scope},
type_ref::{Mutability, TypeRefId},
};
use hir_expand::{
HirFileId, InFile, MacroCallId,
HirFileId, InFile,
mod_path::{ModPath, PathKind, path},
name::{AsName, Name},
};
@ -218,18 +218,6 @@ impl<'db> SourceAnalyzer<'db> {
})
}
pub(crate) fn expansion(
&self,
db: &dyn HirDatabase,
macro_call: InFile<&ast::MacroCall>,
) -> Option<MacroCallId> {
self.store_sm().and_then(|sm| sm.expansion(macro_call)).or_else(|| {
let ast_id_map = db.ast_id_map(macro_call.file_id);
let call_ast_id = macro_call.with_value(ast_id_map.ast_id(macro_call.value));
self.resolver.item_scopes().find_map(|scope| scope.macro_invoc(call_ast_id))
})
}
fn trait_environment(&self, db: &'db dyn HirDatabase) -> Arc<TraitEnvironment> {
self.body_().map(|(def, ..)| def).map_or_else(
|| TraitEnvironment::empty(self.resolver.krate()),
@ -753,21 +741,6 @@ impl<'db> SourceAnalyzer<'db> {
))
}
pub(crate) fn resolve_macro_call(
&self,
db: &dyn HirDatabase,
macro_call: InFile<&ast::MacroCall>,
) -> Option<Macro> {
self.expansion(db, macro_call).and_then(|it| {
let def = it.lookup(db).def;
let def_map = match def.block {
Some(block) => block_def_map(db, base_db::salsa::plumbing::FromId::from_id(block)),
None => crate_def_map(db, def.krate),
};
def_map.macro_def_to_macro_id.get(&def.kind.erased_ast_id()).map(|it| (*it).into())
})
}
pub(crate) fn resolve_bind_pat_to_const(
&self,
db: &'db dyn HirDatabase,

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, "#");