Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 262e06f

Browse files
committed
Remove MacroCallKind::DeriveAttr
1 parent c376add commit 262e06f

File tree

6 files changed

+34
-79
lines changed

6 files changed

+34
-79
lines changed

crates/hir-def/src/nameres/attr_resolution.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use base_db::CrateId;
44
use hir_expand::{
55
attrs::{Attr, AttrId, AttrInput},
6-
AstId, MacroCallId, MacroCallKind, MacroDefId,
6+
MacroCallId, MacroCallKind, MacroDefId,
77
};
88
use span::SyntaxContextId;
99
use syntax::{ast, SmolStr};
@@ -98,20 +98,7 @@ impl DefMap {
9898
false
9999
}
100100
}
101-
pub(super) fn derive_attr_macro_as_call_id(
102-
db: &dyn DefDatabase,
103-
item_attr: &AstId<ast::Adt>,
104-
macro_attr: &Attr,
105-
krate: CrateId,
106-
def: MacroDefId,
107-
) -> MacroCallId {
108-
def.make_call(
109-
db.upcast(),
110-
krate,
111-
MacroCallKind::DeriveAttr { ast_id: *item_attr, invoc_attr_index: macro_attr.id },
112-
macro_attr.ctxt,
113-
)
114-
}
101+
115102
pub(super) fn attr_macro_as_call_id(
116103
db: &dyn DefDatabase,
117104
item_attr: &AstIdWithPath<ast::Item>,

crates/hir-def/src/nameres/collector.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ use crate::{
3737
},
3838
macro_call_as_call_id, macro_call_as_call_id_with_eager,
3939
nameres::{
40-
attr_resolution::{
41-
attr_macro_as_call_id, derive_attr_macro_as_call_id, derive_macro_as_call_id,
42-
},
40+
attr_resolution::{attr_macro_as_call_id, derive_macro_as_call_id},
4341
diagnostics::DefDiagnostic,
4442
mod_resolution::ModDir,
4543
path_resolution::ReachedFixedPoint,
@@ -1234,7 +1232,8 @@ impl DefCollector<'_> {
12341232
Some(def) if def.is_attribute() => def,
12351233
_ => return Resolved::No,
12361234
};
1237-
1235+
let call_id =
1236+
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def);
12381237
if let MacroDefId {
12391238
kind:
12401239
MacroDefKind::BuiltInAttr(
@@ -1266,13 +1265,7 @@ impl DefCollector<'_> {
12661265

12671266
let ast_id = ast_id.with_value(ast_adt_id);
12681267
// the call_id for the actual derive macro. This is used so all the derive proc macros can share a token tree
1269-
let call_id = derive_attr_macro_as_call_id(
1270-
self.db,
1271-
&ast_id,
1272-
attr,
1273-
self.def_map.krate,
1274-
def,
1275-
);
1268+
12761269
match attr.parse_path_comma_token_tree(self.db.upcast()) {
12771270
Some(derive_macros) => {
12781271
let mut len = 0;
@@ -1320,8 +1313,7 @@ impl DefCollector<'_> {
13201313

13211314
return recollect_without(self);
13221315
}
1323-
let call_id =
1324-
attr_macro_as_call_id(self.db, file_ast_id, attr, self.def_map.krate, def);
1316+
13251317
// Skip #[test]/#[bench] expansion, which would merely result in more memory usage
13261318
// due to duplicating functions into macro expansions
13271319
if matches!(

crates/hir-expand/src/cfg_process.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::{
1010
use tracing::{debug, warn};
1111
use tt::SmolStr;
1212

13-
use crate::{db::ExpandDatabase, MacroCallKind, MacroCallLoc};
13+
use crate::{db::ExpandDatabase, proc_macro::ProcMacroKind, MacroCallLoc, MacroDefKind};
1414

1515
fn check_cfg_attr(attr: &Attr, loc: &MacroCallLoc, db: &dyn ExpandDatabase) -> Option<bool> {
1616
if !attr.simple_name().as_deref().map(|v| v == "cfg")? {
@@ -180,7 +180,13 @@ pub(crate) fn process_cfg_attrs(
180180
db: &dyn ExpandDatabase,
181181
) -> Option<FxHashSet<SyntaxElement>> {
182182
// FIXME: #[cfg_eval] is not implemented. But it is not stable yet
183-
if !matches!(loc.kind, MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. }) {
183+
let is_derive = match loc.def.kind {
184+
MacroDefKind::BuiltInDerive(..)
185+
| MacroDefKind::ProcMacro(_, ProcMacroKind::CustomDerive, _) => true,
186+
MacroDefKind::BuiltInAttr(expander, _) => expander.is_derive(),
187+
_ => false,
188+
};
189+
if !is_derive {
184190
return None;
185191
}
186192
let mut remove = FxHashSet::default();

crates/hir-expand/src/db.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ pub fn expand_speculative(
164164
SyntaxFixupUndoInfo::NONE,
165165
),
166166
MacroCallKind::Derive { derive_attr_index: index, .. }
167-
| MacroCallKind::Attr { invoc_attr_index: index, .. }
168-
| MacroCallKind::DeriveAttr { invoc_attr_index: index, .. } => {
167+
| MacroCallKind::Attr { invoc_attr_index: index, .. } => {
169168
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
170169
censor_derive_input(index, &ast::Adt::cast(speculative_args.clone())?)
171170
} else {
@@ -436,9 +435,9 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
436435
}
437436
return (Arc::new(tt), SyntaxFixupUndoInfo::NONE, span);
438437
}
438+
439439
// MacroCallKind::Derive should not be here. As we are getting the argument for the derive macro
440-
MacroCallKind::Derive { ast_id, derive_attr_index, .. }
441-
| MacroCallKind::DeriveAttr { ast_id, invoc_attr_index: derive_attr_index } => {
440+
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
442441
let node = ast_id.to_ptr(db).to_node(&root);
443442
let censor_derive_input = censor_derive_input(derive_attr_index, &node);
444443
let item_node = node.into();
@@ -454,13 +453,23 @@ fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
454453
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
455454
let node = ast_id.to_ptr(db).to_node(&root);
456455
let attr_source = attr_source(invoc_attr_index, &node);
456+
457457
let span = map.span_for_range(
458458
attr_source
459459
.as_ref()
460460
.and_then(|it| it.path())
461461
.map_or_else(|| node.syntax().text_range(), |it| it.syntax().text_range()),
462462
);
463-
(attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span)
463+
// If derive attribute we need to censor the derive input
464+
if matches!(loc.def.kind, MacroDefKind::BuiltInAttr(expander, ..) if expander.is_derive())
465+
&& ast::Adt::can_cast(node.syntax().kind())
466+
{
467+
let adt = ast::Adt::cast(node.syntax().clone()).unwrap();
468+
let censor_derive_input = censor_derive_input(invoc_attr_index, &adt);
469+
(censor_derive_input, node, span)
470+
} else {
471+
(attr_source.into_iter().map(|it| it.syntax().clone().into()).collect(), node, span)
472+
}
464473
}
465474
};
466475

crates/hir-expand/src/lib.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,6 @@ pub enum MacroCallKind {
228228
/// We will resolve the same token tree for all derive macros in the same derive attribute.
229229
derive_macro_id: MacroCallId,
230230
},
231-
DeriveAttr {
232-
ast_id: AstId<ast::Adt>,
233-
invoc_attr_index: AttrId,
234-
},
235231
Attr {
236232
ast_id: AstId<ast::Item>,
237233
// FIXME: This shouldn't be here, we can derive this from `invoc_attr_index`
@@ -519,8 +515,7 @@ impl MacroCallLoc {
519515
MacroCallKind::FnLike { ast_id, .. } => {
520516
ast_id.with_value(ast_id.to_node(db).syntax().clone())
521517
}
522-
MacroCallKind::Derive { ast_id, derive_attr_index, .. }
523-
| MacroCallKind::DeriveAttr { ast_id, invoc_attr_index: derive_attr_index } => {
518+
MacroCallKind::Derive { ast_id, derive_attr_index, .. } => {
524519
// FIXME: handle `cfg_attr`
525520
ast_id.with_value(ast_id.to_node(db)).map(|it| {
526521
collect_attrs(&it)
@@ -554,7 +549,7 @@ impl MacroCallLoc {
554549
fn expand_to(&self) -> ExpandTo {
555550
match self.kind {
556551
MacroCallKind::FnLike { expand_to, .. } => expand_to,
557-
MacroCallKind::Derive { .. } | MacroCallKind::DeriveAttr { .. } => ExpandTo::Items,
552+
MacroCallKind::Derive { .. } => ExpandTo::Items,
558553
MacroCallKind::Attr { .. } if self.def.is_attribute_derive() => ExpandTo::Items,
559554
MacroCallKind::Attr { .. } => {
560555
// FIXME(stmt_expr_attributes)
@@ -588,7 +583,6 @@ impl MacroCallKind {
588583
MacroCallKind::FnLike { .. } => "macro call",
589584
MacroCallKind::Derive { .. } => "derive macro",
590585
MacroCallKind::Attr { .. } => "attribute macro",
591-
MacroCallKind::DeriveAttr { .. } => "derive attribute",
592586
}
593587
}
594588

@@ -597,16 +591,14 @@ impl MacroCallKind {
597591
match *self {
598592
MacroCallKind::FnLike { ast_id: InFile { file_id, .. }, .. }
599593
| MacroCallKind::Derive { ast_id: InFile { file_id, .. }, .. }
600-
| MacroCallKind::DeriveAttr { ast_id: InFile { file_id, .. }, .. }
601594
| MacroCallKind::Attr { ast_id: InFile { file_id, .. }, .. } => file_id,
602595
}
603596
}
604597

605598
pub fn erased_ast_id(&self) -> ErasedFileAstId {
606599
match *self {
607600
MacroCallKind::FnLike { ast_id: InFile { value, .. }, .. } => value.erase(),
608-
MacroCallKind::Derive { ast_id: InFile { value, .. }, .. }
609-
| MacroCallKind::DeriveAttr { ast_id: InFile { value, .. }, .. } => value.erase(),
601+
MacroCallKind::Derive { ast_id: InFile { value, .. }, .. } => value.erase(),
610602
MacroCallKind::Attr { ast_id: InFile { value, .. }, .. } => value.erase(),
611603
}
612604
}
@@ -627,9 +619,7 @@ impl MacroCallKind {
627619

628620
let range = match kind {
629621
MacroCallKind::FnLike { ast_id, .. } => ast_id.to_ptr(db).text_range(),
630-
MacroCallKind::Derive { ast_id, .. } | MacroCallKind::DeriveAttr { ast_id, .. } => {
631-
ast_id.to_ptr(db).text_range()
632-
}
622+
MacroCallKind::Derive { ast_id, .. } => ast_id.to_ptr(db).text_range(),
633623
MacroCallKind::Attr { ast_id, .. } => ast_id.to_ptr(db).text_range(),
634624
};
635625

@@ -675,15 +665,6 @@ impl MacroCallKind {
675665
.syntax()
676666
.text_range()
677667
}
678-
MacroCallKind::DeriveAttr { ast_id, invoc_attr_index } => {
679-
collect_attrs(&ast_id.to_node(db))
680-
.nth(invoc_attr_index.ast_index())
681-
.expect("missing attribute")
682-
.1
683-
.expect_left("attribute macro is a doc comment?")
684-
.syntax()
685-
.text_range()
686-
}
687668
};
688669

689670
FileRange { range, file_id }
@@ -694,7 +675,7 @@ impl MacroCallKind {
694675
MacroCallKind::FnLike { ast_id, .. } => {
695676
ast_id.to_in_file_node(db).map(|it| Some(it.token_tree()?.syntax().clone()))
696677
}
697-
MacroCallKind::Derive { ast_id, .. } | MacroCallKind::DeriveAttr { ast_id, .. } => {
678+
MacroCallKind::Derive { ast_id, .. } => {
698679
ast_id.to_in_file_node(db).syntax().cloned().map(Some)
699680
}
700681
MacroCallKind::Attr { ast_id, .. } => {

crates/hir/src/lib.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,26 +1022,6 @@ fn precise_macro_call_location(
10221022
MacroKind::Attr,
10231023
)
10241024
}
1025-
MacroCallKind::DeriveAttr { ast_id, invoc_attr_index } => {
1026-
let node = ast_id.to_node(db.upcast());
1027-
let attr = collect_attrs(&node)
1028-
.nth(invoc_attr_index.ast_index())
1029-
.and_then(|x| Either::left(x.1))
1030-
.unwrap_or_else(|| {
1031-
panic!("cannot find attribute #{}", invoc_attr_index.ast_index())
1032-
});
1033-
1034-
(
1035-
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
1036-
Some(attr.syntax().text_range()),
1037-
attr.path()
1038-
.and_then(|path| path.segment())
1039-
.and_then(|seg| seg.name_ref())
1040-
.as_ref()
1041-
.map(ToString::to_string),
1042-
MacroKind::Attr,
1043-
)
1044-
}
10451025
}
10461026
}
10471027

0 commit comments

Comments
 (0)