Skip to content

Commit 3eddda6

Browse files
author
Jonas Schievink
committed
simplify
1 parent c5049bd commit 3eddda6

File tree

2 files changed

+43
-48
lines changed

2 files changed

+43
-48
lines changed

crates/hir_def/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,10 @@ fn attr_macro_as_call_id(
781781
macro_attr: &Attr,
782782
db: &dyn db::DefDatabase,
783783
krate: CrateId,
784-
def: Option<MacroDefId>,
785-
) -> Result<MacroCallId, UnresolvedMacro> {
784+
def: MacroDefId,
785+
) -> MacroCallId {
786786
let attr_path = &item_attr.path;
787-
let def = def.ok_or_else(|| UnresolvedMacro { path: attr_path.clone() })?;
788-
let last_segment =
789-
attr_path.segments().last().ok_or_else(|| UnresolvedMacro { path: attr_path.clone() })?;
787+
let last_segment = attr_path.segments().last().expect("empty attribute path");
790788
let mut arg = match macro_attr.input.as_deref() {
791789
Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()),
792790
_ => Default::default(),
@@ -805,5 +803,5 @@ fn attr_macro_as_call_id(
805803
invoc_attr_index: macro_attr.id.ast_index,
806804
},
807805
);
808-
Ok(res)
806+
res
809807
}

crates/hir_def/src/nameres/collector.rs

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,10 +1124,13 @@ impl DefCollector<'_> {
11241124
}
11251125
}
11261126

1127-
let def = resolver(path.clone()).filter(MacroDefId::is_attribute);
1127+
let def = match resolver(path.clone()) {
1128+
Some(def) if def.is_attribute() => def,
1129+
_ => return true,
1130+
};
11281131
if matches!(
11291132
def,
1130-
Some(MacroDefId { kind:MacroDefKind::BuiltInAttr(expander, _),.. })
1133+
MacroDefId { kind:MacroDefKind::BuiltInAttr(expander, _),.. }
11311134
if expander.is_derive()
11321135
) {
11331136
// Resolved to `#[derive]`
@@ -1184,52 +1187,46 @@ impl DefCollector<'_> {
11841187
return true;
11851188
}
11861189

1187-
// Not resolved to a derive helper or the derive attribute, so try to resolve as a normal attribute.
1188-
match attr_macro_as_call_id(file_ast_id, attr, self.db, self.def_map.krate, def)
1189-
{
1190-
Ok(call_id) => {
1191-
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
1192-
1193-
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1194-
// due to duplicating functions into macro expansions
1195-
if matches!(
1196-
loc.def.kind,
1197-
MacroDefKind::BuiltInAttr(expander, _)
1198-
if expander.is_test() || expander.is_bench()
1199-
) {
1200-
return recollect_without(self);
1201-
}
1202-
1203-
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
1204-
if exp.is_dummy() {
1205-
// Proc macros that cannot be expanded are treated as not
1206-
// resolved, in order to fall back later.
1207-
self.def_map.diagnostics.push(
1208-
DefDiagnostic::unresolved_proc_macro(
1209-
directive.module_id,
1210-
loc.kind,
1211-
),
1212-
);
1213-
1214-
return recollect_without(self);
1215-
}
1216-
}
1190+
// Not resolved to a derive helper or the derive attribute, so try to treat as a normal attribute.
1191+
let call_id =
1192+
attr_macro_as_call_id(file_ast_id, attr, self.db, self.def_map.krate, def);
1193+
let loc: MacroCallLoc = self.db.lookup_intern_macro_call(call_id);
12171194

1218-
self.def_map.modules[directive.module_id]
1219-
.scope
1220-
.add_attr_macro_invoc(ast_id, call_id);
1195+
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
1196+
// due to duplicating functions into macro expansions
1197+
if matches!(
1198+
loc.def.kind,
1199+
MacroDefKind::BuiltInAttr(expander, _)
1200+
if expander.is_test() || expander.is_bench()
1201+
) {
1202+
return recollect_without(self);
1203+
}
12211204

1222-
resolved.push((
1205+
if let MacroDefKind::ProcMacro(exp, ..) = loc.def.kind {
1206+
if exp.is_dummy() {
1207+
// Proc macros that cannot be expanded are treated as not
1208+
// resolved, in order to fall back later.
1209+
self.def_map.diagnostics.push(DefDiagnostic::unresolved_proc_macro(
12231210
directive.module_id,
1224-
call_id,
1225-
directive.depth,
1226-
directive.container,
1211+
loc.kind,
12271212
));
1228-
res = ReachedFixedPoint::No;
1229-
return false;
1213+
1214+
return recollect_without(self);
12301215
}
1231-
Err(UnresolvedMacro { .. }) => (),
12321216
}
1217+
1218+
self.def_map.modules[directive.module_id]
1219+
.scope
1220+
.add_attr_macro_invoc(ast_id, call_id);
1221+
1222+
resolved.push((
1223+
directive.module_id,
1224+
call_id,
1225+
directive.depth,
1226+
directive.container,
1227+
));
1228+
res = ReachedFixedPoint::No;
1229+
return false;
12331230
}
12341231
}
12351232

0 commit comments

Comments
 (0)