Unnamed repository; edit this file 'description' to name the repository.
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.
Ryo Yoshida 2022-12-06
parent cf54b8c · commit 051c659
-rw-r--r--crates/hir-def/src/data.rs17
-rw-r--r--crates/hir/src/lib.rs6
2 files changed, 20 insertions, 3 deletions
diff --git a/crates/hir-def/src/data.rs b/crates/hir-def/src/data.rs
index 0e7acda4a7..b78ab71ef2 100644
--- a/crates/hir-def/src/data.rs
+++ b/crates/hir-def/src/data.rs
@@ -13,7 +13,9 @@ use crate::{
intern::Interned,
item_tree::{self, AssocItem, FnFlags, ItemTree, ItemTreeId, ModItem, Param, TreeId},
nameres::{
- attr_resolution::ResolvedAttr, diagnostics::DefDiagnostic, proc_macro::ProcMacroKind,
+ attr_resolution::ResolvedAttr,
+ diagnostics::DefDiagnostic,
+ proc_macro::{parse_macro_name_and_helper_attrs, ProcMacroKind},
DefMap,
},
type_ref::{TraitRef, TypeBound, TypeRef},
@@ -348,6 +350,10 @@ impl ImplData {
pub struct Macro2Data {
pub name: Name,
pub visibility: RawVisibility,
+ // It's a bit wasteful as currently this is only for builtin `Default` derive macro, but macro2
+ // are rarely used in practice so I think it's okay for now.
+ /// Derive helpers, if this is a derive rustc_builtin_macro
+ pub helpers: Option<Box<[Name]>>,
}
impl Macro2Data {
@@ -356,9 +362,18 @@ impl Macro2Data {
let item_tree = loc.id.item_tree(db);
let makro = &item_tree[loc.id.value];
+ let helpers = item_tree
+ .attrs(db, loc.container.krate(), ModItem::from(loc.id.value).into())
+ .by_key("rustc_builtin_macro")
+ .tt_values()
+ .next()
+ .and_then(|attr| parse_macro_name_and_helper_attrs(&attr.token_trees))
+ .map(|(_, helpers)| helpers);
+
Arc::new(Macro2Data {
name: makro.name.clone(),
visibility: item_tree[makro.visibility].clone(),
+ helpers,
})
}
}
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index cbbcaebb42..5f36ce62f8 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -2349,12 +2349,14 @@ impl DeriveHelper {
pub fn name(&self, db: &dyn HirDatabase) -> Name {
match self.derive {
- MacroId::Macro2Id(_) => None,
+ MacroId::Macro2Id(it) => {
+ db.macro2_data(it).helpers.as_deref().and_then(|it| it.get(self.idx)).cloned()
+ }
MacroId::MacroRulesId(_) => None,
MacroId::ProcMacroId(proc_macro) => db
.proc_macro_data(proc_macro)
.helpers
- .as_ref()
+ .as_deref()
.and_then(|it| it.get(self.idx))
.cloned(),
}