Skip to content

Commit df725d6

Browse files
committed
fix: Do not create fn macro calls with non-fn expanders
1 parent 3db437c commit df725d6

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

crates/hir-def/src/lib.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1155,18 +1155,22 @@ fn macro_call_as_call_id_(
11551155
let def =
11561156
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
11571157

1158-
let res = if let MacroDefKind::BuiltInEager(..) = def.kind {
1159-
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
1160-
expand_eager_macro_input(db, krate, macro_call, def, &resolver)?
1161-
} else {
1162-
ExpandResult {
1158+
let res = match def.kind {
1159+
MacroDefKind::BuiltInEager(..) => {
1160+
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
1161+
expand_eager_macro_input(db, krate, macro_call, def, &|path| {
1162+
resolver(path).filter(MacroDefId::is_fn_like)
1163+
})?
1164+
}
1165+
_ if def.is_fn_like() => ExpandResult {
11631166
value: Some(def.as_lazy_macro(
11641167
db,
11651168
krate,
11661169
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
11671170
)),
11681171
err: None,
1169-
}
1172+
},
1173+
_ => return Err(UnresolvedMacro { path: call.path.clone() }),
11701174
};
11711175
Ok(res)
11721176
}
@@ -1251,6 +1255,7 @@ fn derive_macro_as_call_id(
12511255
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
12521256
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
12531257
let (macro_id, def_id) = resolver(item_attr.path.clone())
1258+
.filter(|(_, def_id)| def_id.is_derive())
12541259
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
12551260
let call_id = def_id.as_lazy_macro(
12561261
db.upcast(),

crates/hir-expand/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,24 @@ impl MacroDefId {
415415
)
416416
}
417417

418+
pub fn is_derive(&self) -> bool {
419+
matches!(
420+
self.kind,
421+
MacroDefKind::BuiltInDerive(..)
422+
| MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _)
423+
)
424+
}
425+
426+
pub fn is_fn_like(&self) -> bool {
427+
matches!(
428+
self.kind,
429+
MacroDefKind::BuiltIn(..)
430+
| MacroDefKind::ProcMacro(_, ProcMacroKind::FuncLike, _)
431+
| MacroDefKind::BuiltInEager(..)
432+
| MacroDefKind::Declarative(..)
433+
)
434+
}
435+
418436
pub fn is_attribute_derive(&self) -> bool {
419437
matches!(self.kind, MacroDefKind::BuiltInAttr(expander, ..) if expander.is_derive())
420438
}

0 commit comments

Comments
 (0)