Skip to content

Commit c0dd36f

Browse files
Store #[derive] attribute ID along macro invoc
1 parent 546da15 commit c0dd36f

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

crates/hir_def/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use hir_expand::{
6262
ast_id_map::FileAstId,
6363
eager::{expand_eager_macro, ErrorEmitted, ErrorSink},
6464
hygiene::Hygiene,
65-
AstId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
65+
AstId, AttrId, HirFileId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
6666
};
6767
use la_arena::Idx;
6868
use nameres::DefMap;
@@ -699,6 +699,7 @@ fn macro_call_as_call_id(
699699

700700
fn derive_macro_as_call_id(
701701
item_attr: &AstIdWithPath<ast::Item>,
702+
derive_attr: AttrId,
702703
db: &dyn db::DefDatabase,
703704
krate: CrateId,
704705
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
@@ -712,6 +713,7 @@ fn derive_macro_as_call_id(
712713
MacroCallKind::Derive {
713714
ast_id: item_attr.ast_id,
714715
derive_name: last_segment.to_string(),
716+
derive_attr,
715717
},
716718
)
717719
.into();

crates/hir_def/src/nameres.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ mod diagnostics {
617617
let node = ast_id.to_node(db.upcast());
618618
(ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
619619
}
620-
MacroCallKind::Derive { ast_id, derive_name } => {
620+
MacroCallKind::Derive { ast_id, derive_name, .. } => {
621621
let node = ast_id.to_node(db.upcast());
622622

623623
// Compute the precise location of the macro name's token in the derive

crates/hir_def/src/nameres/collector.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hir_expand::{
1313
builtin_macro::find_builtin_macro,
1414
name::{AsName, Name},
1515
proc_macro::ProcMacroExpander,
16-
HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
16+
AttrId, HirFileId, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
1717
};
1818
use hir_expand::{InFile, MacroCallLoc};
1919
use rustc_hash::{FxHashMap, FxHashSet};
@@ -216,7 +216,7 @@ struct MacroDirective {
216216
#[derive(Clone, Debug, Eq, PartialEq)]
217217
enum MacroDirectiveKind {
218218
FnLike { ast_id: AstIdWithPath<ast::MacroCall> },
219-
Derive { ast_id: AstIdWithPath<ast::Item> },
219+
Derive { ast_id: AstIdWithPath<ast::Item>, derive_attr: AttrId },
220220
}
221221

222222
struct DefData<'a> {
@@ -831,10 +831,14 @@ impl DefCollector<'_> {
831831
Err(UnresolvedMacro) | Ok(Err(_)) => {}
832832
}
833833
}
834-
MacroDirectiveKind::Derive { ast_id } => {
835-
match derive_macro_as_call_id(ast_id, self.db, self.def_map.krate, |path| {
836-
self.resolve_derive_macro(directive.module_id, &path)
837-
}) {
834+
MacroDirectiveKind::Derive { ast_id, derive_attr } => {
835+
match derive_macro_as_call_id(
836+
ast_id,
837+
*derive_attr,
838+
self.db,
839+
self.def_map.krate,
840+
|path| self.resolve_derive_macro(directive.module_id, &path),
841+
) {
838842
Ok(call_id) => {
839843
resolved.push((directive.module_id, call_id, directive.depth));
840844
res = ReachedFixedPoint::No;
@@ -1368,7 +1372,7 @@ impl ModCollector<'_, '_> {
13681372
self.def_collector.unexpanded_macros.push(MacroDirective {
13691373
module_id: self.module_id,
13701374
depth: self.macro_depth + 1,
1371-
kind: MacroDirectiveKind::Derive { ast_id },
1375+
kind: MacroDirectiveKind::Derive { ast_id, derive_attr: derive.id },
13721376
});
13731377
}
13741378
}

crates/hir_expand/src/builtin_derive.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ mod tests {
269269
use expect_test::{expect, Expect};
270270
use name::AsName;
271271

272-
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
272+
use crate::{test_db::TestDB, AstId, AttrId, MacroCallId, MacroCallKind, MacroCallLoc};
273273

274274
use super::*;
275275

@@ -317,7 +317,11 @@ $0
317317
local_inner: false,
318318
},
319319
krate: CrateId(0),
320-
kind: MacroCallKind::Derive { ast_id, derive_name: name.to_string() },
320+
kind: MacroCallKind::Derive {
321+
ast_id,
322+
derive_name: name.to_string(),
323+
derive_attr: AttrId(0),
324+
},
321325
};
322326

323327
let id: MacroCallId = db.intern_macro(loc).into();

crates/hir_expand/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ pub struct MacroCallLoc {
291291
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
292292
pub enum MacroCallKind {
293293
FnLike { ast_id: AstId<ast::MacroCall> },
294-
Derive { ast_id: AstId<ast::Item>, derive_name: String },
294+
Derive { ast_id: AstId<ast::Item>, derive_name: String, derive_attr: AttrId },
295295
}
296296

297297
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)