Skip to content

Commit d6ed146

Browse files
bors[bot]Veykril
andauthored
Merge #11512
11512: internal: Remove `name` fields from `MacroCallKind` r=Veykril a=Veykril Co-authored-by: Lukas Wirth <[email protected]>
2 parents e534702 + 035bedc commit d6ed146

File tree

5 files changed

+38
-60
lines changed

5 files changed

+38
-60
lines changed

crates/hir/src/lib.rs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use rustc_hash::FxHashSet;
7474
use stdx::{format_to, impl_from};
7575
use syntax::{
7676
ast::{self, HasAttrs as _, HasDocComments, HasName},
77-
AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr,
77+
AstNode, AstPtr, SmolStr, SyntaxNodePtr, T,
7878
};
7979
use tt::{Ident, Leaf, Literal, TokenTree};
8080

@@ -628,43 +628,37 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
628628

629629
DefDiagnosticKind::UnresolvedProcMacro { ast } => {
630630
let mut precise_location = None;
631-
let (node, name) = match ast {
631+
let (node, macro_name) = match ast {
632632
MacroCallKind::FnLike { ast_id, .. } => {
633633
let node = ast_id.to_node(db.upcast());
634634
(ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
635635
}
636-
MacroCallKind::Derive { ast_id, derive_name, .. } => {
636+
MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => {
637637
let node = ast_id.to_node(db.upcast());
638638

639639
// Compute the precise location of the macro name's token in the derive
640640
// list.
641-
// FIXME: This does not handle paths to the macro, but neither does the
642-
// rest of r-a.
643-
let derive_attrs =
644-
node.attrs().filter_map(|attr| match attr.as_simple_call() {
645-
Some((name, args)) if name == "derive" => Some(args),
646-
_ => None,
647-
});
648-
'outer: for attr in derive_attrs {
649-
let tokens =
650-
attr.syntax().children_with_tokens().filter_map(|elem| match elem {
651-
syntax::NodeOrToken::Node(_) => None,
641+
let token = (|| {
642+
let derive_attr = node.attrs().nth(*derive_attr_index as usize)?;
643+
derive_attr
644+
.syntax()
645+
.children_with_tokens()
646+
.filter_map(|elem| match elem {
652647
syntax::NodeOrToken::Token(tok) => Some(tok),
653-
});
654-
for token in tokens {
655-
if token.kind() == SyntaxKind::IDENT && token.text() == &**derive_name {
656-
precise_location = Some(token.text_range());
657-
break 'outer;
658-
}
659-
}
660-
}
661-
648+
_ => None,
649+
})
650+
.group_by(|t| t.kind() == T![,])
651+
.into_iter()
652+
.nth(*derive_index as usize)
653+
.and_then(|(_, mut g)| g.find(|t| t.kind() == T![ident]))
654+
})();
655+
precise_location = token.as_ref().map(|tok| tok.text_range());
662656
(
663657
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
664-
Some(derive_name.clone()),
658+
token.as_ref().map(ToString::to_string),
665659
)
666660
}
667-
MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => {
661+
MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
668662
let node = ast_id.to_node(db.upcast());
669663
let attr = node
670664
.doc_comments_and_attrs()
@@ -673,14 +667,15 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
673667
.unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index));
674668
(
675669
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
676-
Some(attr_name.clone()),
670+
attr.path()
671+
.and_then(|path| path.segment())
672+
.and_then(|seg| seg.name_ref())
673+
.as_ref()
674+
.map(ToString::to_string),
677675
)
678676
}
679677
};
680-
acc.push(
681-
UnresolvedProcMacro { node, precise_location, macro_name: name.map(Into::into) }
682-
.into(),
683-
);
678+
acc.push(UnresolvedProcMacro { node, precise_location, macro_name }.into());
684679
}
685680

686681
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {

crates/hir_def/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -741,23 +741,19 @@ fn macro_call_as_call_id(
741741
fn derive_macro_as_call_id(
742742
item_attr: &AstIdWithPath<ast::Adt>,
743743
derive_attr: AttrId,
744+
derive_pos: u32,
744745
db: &dyn db::DefDatabase,
745746
krate: CrateId,
746747
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
747748
) -> Result<MacroCallId, UnresolvedMacro> {
748749
let def: MacroDefId = resolver(item_attr.path.clone())
749750
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
750-
let last_segment = item_attr
751-
.path
752-
.segments()
753-
.last()
754-
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
755751
let res = def.as_lazy_macro(
756752
db.upcast(),
757753
krate,
758754
MacroCallKind::Derive {
759755
ast_id: item_attr.ast_id,
760-
derive_name: last_segment.to_string().into_boxed_str(),
756+
derive_index: derive_pos,
761757
derive_attr_index: derive_attr.ast_index,
762758
},
763759
);
@@ -771,8 +767,6 @@ fn attr_macro_as_call_id(
771767
krate: CrateId,
772768
def: MacroDefId,
773769
) -> MacroCallId {
774-
let attr_path = &item_attr.path;
775-
let last_segment = attr_path.segments().last().expect("empty attribute path");
776770
let mut arg = match macro_attr.input.as_deref() {
777771
Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()),
778772
_ => Default::default(),
@@ -786,7 +780,6 @@ fn attr_macro_as_call_id(
786780
krate,
787781
MacroCallKind::Attr {
788782
ast_id: item_attr.ast_id,
789-
attr_name: last_segment.to_string().into_boxed_str(),
790783
attr_args: Arc::new(arg),
791784
invoc_attr_index: macro_attr.id.ast_index,
792785
},

crates/hir_def/src/nameres/collector.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,9 @@ impl DefCollector<'_> {
10361036
fn resolve_macros(&mut self) -> ReachedFixedPoint {
10371037
let mut macros = std::mem::take(&mut self.unresolved_macros);
10381038
let mut resolved = Vec::new();
1039+
let mut push_resolved = |directive: &MacroDirective, call_id| {
1040+
resolved.push((directive.module_id, directive.depth, directive.container, call_id));
1041+
};
10391042
let mut res = ReachedFixedPoint::Yes;
10401043
macros.retain(|directive| {
10411044
let resolver = |path| {
@@ -1060,12 +1063,7 @@ impl DefCollector<'_> {
10601063
&mut |_err| (),
10611064
);
10621065
if let Ok(Ok(call_id)) = call_id {
1063-
resolved.push((
1064-
directive.module_id,
1065-
call_id,
1066-
directive.depth,
1067-
directive.container,
1068-
));
1066+
push_resolved(directive, call_id);
10691067
res = ReachedFixedPoint::No;
10701068
return false;
10711069
}
@@ -1074,6 +1072,7 @@ impl DefCollector<'_> {
10741072
let call_id = derive_macro_as_call_id(
10751073
ast_id,
10761074
*derive_attr,
1075+
*derive_pos as u32,
10771076
self.db,
10781077
self.def_map.krate,
10791078
&resolver,
@@ -1086,12 +1085,7 @@ impl DefCollector<'_> {
10861085
*derive_pos,
10871086
);
10881087

1089-
resolved.push((
1090-
directive.module_id,
1091-
call_id,
1092-
directive.depth,
1093-
directive.container,
1094-
));
1088+
push_resolved(directive, call_id);
10951089
res = ReachedFixedPoint::No;
10961090
return false;
10971091
}
@@ -1229,12 +1223,7 @@ impl DefCollector<'_> {
12291223
.scope
12301224
.add_attr_macro_invoc(ast_id, call_id);
12311225

1232-
resolved.push((
1233-
directive.module_id,
1234-
call_id,
1235-
directive.depth,
1236-
directive.container,
1237-
));
1226+
push_resolved(directive, call_id);
12381227
res = ReachedFixedPoint::No;
12391228
return false;
12401229
}
@@ -1245,7 +1234,7 @@ impl DefCollector<'_> {
12451234
// Attribute resolution can add unresolved macro invocations, so concatenate the lists.
12461235
self.unresolved_macros.extend(macros);
12471236

1248-
for (module_id, macro_call_id, depth, container) in resolved {
1237+
for (module_id, depth, container, macro_call_id) in resolved {
12491238
self.collect_macro_expansion(module_id, macro_call_id, depth, container);
12501239
}
12511240

crates/hir_expand/src/db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
336336
ast::Item::cast(node.clone())?
337337
.attrs()
338338
.take(derive_attr_index as usize + 1)
339+
// FIXME
339340
.filter(|attr| attr.simple_name().as_deref() == Some("derive"))
340341
.map(|it| it.syntax().clone())
341342
.collect()

crates/hir_expand/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ pub enum MacroCallKind {
125125
},
126126
Derive {
127127
ast_id: AstId<ast::Adt>,
128-
derive_name: Box<str>,
129128
/// Syntactical index of the invoking `#[derive]` attribute.
130129
///
131130
/// Outer attributes are counted first, then inner attributes. This does not support
132131
/// out-of-line modules, which may have attributes spread across 2 files!
133132
derive_attr_index: u32,
133+
/// Index of the derive macro in the derive attribute
134+
derive_index: u32,
134135
},
135136
Attr {
136137
ast_id: AstId<ast::Item>,
137-
attr_name: Box<str>,
138138
attr_args: Arc<(tt::Subtree, mbe::TokenMap)>,
139139
/// Syntactical index of the invoking `#[attribute]`.
140140
///

0 commit comments

Comments
 (0)