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.
This commit is contained in:
Ryo Yoshida 2022-12-06 15:53:03 +09:00
parent cf54b8c3a4
commit 051c6598be
No known key found for this signature in database
GPG key ID: E25698A930586171
2 changed files with 20 additions and 3 deletions

View file

@ -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,
})
}
}

View file

@ -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(),
}