From c0dd36fd425416f5465abf7f12f5d3a14b35751d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 9 Apr 2021 13:38:01 +0200 Subject: Store `#[derive]` attribute ID along macro invoc --- crates/hir_def/src/lib.rs | 4 +++- crates/hir_def/src/nameres.rs | 2 +- crates/hir_def/src/nameres/collector.rs | 18 +++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) (limited to 'crates/hir_def/src') diff --git a/crates/hir_def/src/lib.rs b/crates/hir_def/src/lib.rs index b72884925..d69116d51 100644 --- a/crates/hir_def/src/lib.rs +++ b/crates/hir_def/src/lib.rs @@ -62,7 +62,7 @@ use hir_expand::{ ast_id_map::FileAstId, eager::{expand_eager_macro, ErrorEmitted, ErrorSink}, hygiene::Hygiene, - AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use la_arena::Idx; use nameres::DefMap; @@ -699,6 +699,7 @@ fn macro_call_as_call_id( fn derive_macro_as_call_id( item_attr: &AstIdWithPath, + derive_attr: AttrId, db: &dyn db::DefDatabase, krate: CrateId, resolver: impl Fn(path::ModPath) -> Option, @@ -712,6 +713,7 @@ fn derive_macro_as_call_id( MacroCallKind::Derive { ast_id: item_attr.ast_id, derive_name: last_segment.to_string(), + derive_attr, }, ) .into(); diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index d966fc239..9e181751c 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs @@ -617,7 +617,7 @@ mod diagnostics { let node = ast_id.to_node(db.upcast()); (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) } - MacroCallKind::Derive { ast_id, derive_name } => { + MacroCallKind::Derive { ast_id, derive_name, .. } => { let node = ast_id.to_node(db.upcast()); // Compute the precise location of the macro name's token in the derive diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index f431da3f2..d13d7be27 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs @@ -13,7 +13,7 @@ use hir_expand::{ builtin_macro::find_builtin_macro, name::{AsName, Name}, proc_macro::ProcMacroExpander, - HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, }; use hir_expand::{InFile, MacroCallLoc}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -216,7 +216,7 @@ struct MacroDirective { #[derive(Clone, Debug, Eq, PartialEq)] enum MacroDirectiveKind { FnLike { ast_id: AstIdWithPath }, - Derive { ast_id: AstIdWithPath }, + Derive { ast_id: AstIdWithPath, derive_attr: AttrId }, } struct DefData<'a> { @@ -831,10 +831,14 @@ impl DefCollector<'_> { Err(UnresolvedMacro) | Ok(Err(_)) => {} } } - MacroDirectiveKind::Derive { ast_id } => { - match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| { - self.resolve_derive_macro(directive.module_id, &path) - }) { + MacroDirectiveKind::Derive { ast_id, derive_attr } => { + match derive_macro_as_call_id( + ast_id, + *derive_attr, + self.db, + self.def_map.krate, + |path| self.resolve_derive_macro(directive.module_id, &path), + ) { Ok(call_id) => { resolved.push((directive.module_id, call_id, directive.depth)); res = ReachedFixedPoint::No; @@ -1368,7 +1372,7 @@ impl ModCollector<'_, '_> { self.def_collector.unexpanded_macros.push(MacroDirective { module_id: self.module_id, depth: self.macro_depth + 1, - kind: MacroDirectiveKind::Derive { ast_id }, + kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id }, }); } } -- cgit v1.2.3