Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 051c659

Browse files
committed
Resolve macro2's derive helpers in IDE layer
Macro2's generally don't have derive helpers, but currently builtin derive macros are declared with macro2 syntax, which can have derive helpers.
1 parent cf54b8c commit 051c659

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

crates/hir-def/src/data.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ use crate::{
1313
intern::Interned,
1414
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
1515
nameres::{
16-
attr_resolution::ResolvedAttr, diagnostics::DefDiagnostic, proc_macro::ProcMacroKind,
16+
attr_resolution::ResolvedAttr,
17+
diagnostics::DefDiagnostic,
18+
proc_macro::{parse_macro_name_and_helper_attrs, ProcMacroKind},
1719
DefMap,
1820
},
1921
type_ref::{TraitRef, TypeBound, TypeRef},
@@ -348,6 +350,10 @@ impl ImplData {
348350
pub struct Macro2Data {
349351
pub name: Name,
350352
pub visibility: RawVisibility,
353+
// It's a bit wasteful as currently this is only for builtin `Default` derive macro, but macro2
354+
// are rarely used in practice so I think it's okay for now.
355+
/// Derive helpers, if this is a derive rustc_builtin_macro
356+
pub helpers: Option<Box<[Name]>>,
351357
}
352358

353359
impl Macro2Data {
@@ -356,9 +362,18 @@ impl Macro2Data {
356362
let item_tree = loc.id.item_tree(db);
357363
let makro = &item_tree[loc.id.value];
358364

365+
let helpers = item_tree
366+
.attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
367+
.by_key("rustc_builtin_macro")
368+
.tt_values()
369+
.next()
370+
.and_then(|attr| parse_macro_name_and_helper_attrs(&attr.token_trees))
371+
.map(|(_, helpers)| helpers);
372+
359373
Arc::new(Macro2Data {
360374
name: makro.name.clone(),
361375
visibility: item_tree[makro.visibility].clone(),
376+
helpers,
362377
})
363378
}
364379
}

crates/hir/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2349,12 +2349,14 @@ impl DeriveHelper {
23492349

23502350
pub fn name(&self, db: &dyn HirDatabase) -> Name {
23512351
match self.derive {
2352-
MacroId::Macro2Id(_) => None,
2352+
MacroId::Macro2Id(it) => {
2353+
db.macro2_data(it).helpers.as_deref().and_then(|it| it.get(self.idx)).cloned()
2354+
}
23532355
MacroId::MacroRulesId(_) => None,
23542356
MacroId::ProcMacroId(proc_macro) => db
23552357
.proc_macro_data(proc_macro)
23562358
.helpers
2357-
.as_ref()
2359+
.as_deref()
23582360
.and_then(|it| it.get(self.idx))
23592361
.cloned(),
23602362
}

0 commit comments

Comments
 (0)