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 +++++++++++------- crates/hir_expand/src/builtin_derive.rs | 8 ++++++-- crates/hir_expand/src/lib.rs | 2 +- 5 files changed, 22 insertions(+), 12 deletions(-) (limited to 'crates') 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 }, }); } } diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index 392079ed4..537c03028 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs @@ -269,7 +269,7 @@ mod tests { use expect_test::{expect, Expect}; use name::AsName; - use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc}; + use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc}; use super::*; @@ -317,7 +317,11 @@ $0 local_inner: false, }, krate: CrateId(0), - kind: MacroCallKind::Derive { ast_id, derive_name: name.to_string() }, + kind: MacroCallKind::Derive { + ast_id, + derive_name: name.to_string(), + derive_attr: AttrId(0), + }, }; let id: MacroCallId = db.intern_macro(loc).into(); diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 8637abc6a..a0e6aec62 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -291,7 +291,7 @@ pub struct MacroCallLoc { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum MacroCallKind { FnLike { ast_id: AstId }, - Derive { ast_id: AstId, derive_name: String }, + Derive { ast_id: AstId, derive_name: String, derive_attr: AttrId }, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -- cgit v1.2.3