This commit is contained in:
Jonas Schievink 2022-01-05 17:26:34 +01:00
parent c5049bdcda
commit 3eddda6f4c
2 changed files with 47 additions and 52 deletions

View File

@ -781,12 +781,10 @@ fn attr_macro_as_call_id(
macro_attr: &Attr, macro_attr: &Attr,
db: &dyn db::DefDatabase, db: &dyn db::DefDatabase,
krate: CrateId, krate: CrateId,
def: Option<MacroDefId>, def: MacroDefId,
) -> Result<MacroCallId, UnresolvedMacro> { ) -> MacroCallId {
let attr_path = &item_attr.path; let attr_path = &item_attr.path;
let def = def.ok_or_else(|| UnresolvedMacro { path: attr_path.clone() })?; let last_segment = attr_path.segments().last().expect("empty attribute path");
let last_segment =
attr_path.segments().last().ok_or_else(|| UnresolvedMacro { path: attr_path.clone() })?;
let mut arg = match macro_attr.input.as_deref() { let mut arg = match macro_attr.input.as_deref() {
Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()), Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()),
_ => Default::default(), _ => Default::default(),
@ -805,5 +803,5 @@ fn attr_macro_as_call_id(
invoc_attr_index: macro_attr.id.ast_index, invoc_attr_index: macro_attr.id.ast_index,
}, },
); );
Ok(res) res
} }

View File

@ -1124,10 +1124,13 @@ impl DefCollector<'_> {
} }
} }
let def = resolver(path.clone()).filter(MacroDefId::is_attribute); let def = match resolver(path.clone()) {
Some(def) if def.is_attribute() => def,
_ => return true,
};
if matches!( if matches!(
def, def,
Some(MacroDefId { kind:MacroDefKind::BuiltInAttr(expander, _),.. }) MacroDefId { kind:MacroDefKind::BuiltInAttr(expander, _),.. }
if expander.is_derive() if expander.is_derive()
) { ) {
// Resolved to `#[derive]` // Resolved to `#[derive]`
@ -1184,52 +1187,46 @@ impl DefCollector<'_> {
return true; return true;
} }
// Not resolved to a derive helper or the derive attribute, so try to resolve as a normal attribute. // Not resolved to a derive helper or the derive attribute, so try to treat as a normal attribute.
match attr_macro_as_call_id(file_ast_id, attr, self.db, self.def_map.krate, def) let call_id =
{ attr_macro_as_call_id(file_ast_id, attr, self.db, self.def_map.krate, def);
Ok(call_id) => { let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage // Skip #[test]/#[bench] expansion, which would merely result in more memory usage
// due to duplicating functions into macro expansions // due to duplicating functions into macro expansions
if matches!( if matches!(
loc.def.kind, loc.def.kind,
MacroDefKind::BuiltInAttr(expander, _) MacroDefKind::BuiltInAttr(expander, _)
if expander.is_test() || expander.is_bench() if expander.is_test() || expander.is_bench()
) { ) {
return recollect_without(self); return recollect_without(self);
}
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
if exp.is_dummy() {
// Proc macros that cannot be expanded are treated as not
// resolved, in order to fall back later.
self.def_map.diagnostics.push(
DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
),
);
return recollect_without(self);
}
}
self.def_map.modules[directive.module_id]
.scope
.add_attr_macro_invoc(ast_id, call_id);
resolved.push((
directive.module_id,
call_id,
directive.depth,
directive.container,
));
res = ReachedFixedPoint::No;
return false;
}
Err(UnresolvedMacro { .. }) => (),
} }
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
if exp.is_dummy() {
// Proc macros that cannot be expanded are treated as not
// resolved, in order to fall back later.
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
directive.module_id,
loc.kind,
));
return recollect_without(self);
}
}
self.def_map.modules[directive.module_id]
.scope
.add_attr_macro_invoc(ast_id, call_id);
resolved.push((
directive.module_id,
call_id,
directive.depth,
directive.container,
));
res = ReachedFixedPoint::No;
return false;
} }
} }