11512: internal: Remove `name` fields from `MacroCallKind` r=Veykril a=Veykril



Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2022-02-20 23:16:59 +00:00 committed by GitHub
commit d6ed146a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 60 deletions

View File

@ -74,7 +74,7 @@ use rustc_hash::FxHashSet;
use stdx::{format_to, impl_from}; use stdx::{format_to, impl_from};
use syntax::{ use syntax::{
ast::{self, HasAttrs as _, HasDocComments, HasName}, ast::{self, HasAttrs as _, HasDocComments, HasName},
AstNode, AstPtr, SmolStr, SyntaxKind, SyntaxNodePtr, AstNode, AstPtr, SmolStr, SyntaxNodePtr, T,
}; };
use tt::{Ident, Leaf, Literal, TokenTree}; use tt::{Ident, Leaf, Literal, TokenTree};
@ -628,43 +628,37 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
DefDiagnosticKind::UnresolvedProcMacro { ast } => { DefDiagnosticKind::UnresolvedProcMacro { ast } => {
let mut precise_location = None; let mut precise_location = None;
let (node, name) = match ast { let (node, macro_name) = match ast {
MacroCallKind::FnLike { ast_id, .. } => { MacroCallKind::FnLike { ast_id, .. } => {
let node = ast_id.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
(ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None) (ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), None)
} }
MacroCallKind::Derive { ast_id, derive_name, .. } => { MacroCallKind::Derive { ast_id, derive_attr_index, derive_index } => {
let node = ast_id.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
// Compute the precise location of the macro name's token in the derive // Compute the precise location of the macro name's token in the derive
// list. // list.
// FIXME: This does not handle paths to the macro, but neither does the let token = (|| {
// rest of r-a. let derive_attr = node.attrs().nth(*derive_attr_index as usize)?;
let derive_attrs = derive_attr
node.attrs().filter_map(|attr| match attr.as_simple_call() { .syntax()
Some((name, args)) if name == "derive" => Some(args), .children_with_tokens()
_ => None, .filter_map(|elem| match elem {
});
'outer: for attr in derive_attrs {
let tokens =
attr.syntax().children_with_tokens().filter_map(|elem| match elem {
syntax::NodeOrToken::Node(_) => None,
syntax::NodeOrToken::Token(tok) => Some(tok), syntax::NodeOrToken::Token(tok) => Some(tok),
}); _ => None,
for token in tokens { })
if token.kind() == SyntaxKind::IDENT && token.text() == &**derive_name { .group_by(|t| t.kind() == T![,])
precise_location = Some(token.text_range()); .into_iter()
break 'outer; .nth(*derive_index as usize)
} .and_then(|(_, mut g)| g.find(|t| t.kind() == T![ident]))
} })();
} precise_location = token.as_ref().map(|tok| tok.text_range());
( (
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))), ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&node))),
Some(derive_name.clone()), token.as_ref().map(ToString::to_string),
) )
} }
MacroCallKind::Attr { ast_id, invoc_attr_index, attr_name, .. } => { MacroCallKind::Attr { ast_id, invoc_attr_index, .. } => {
let node = ast_id.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
let attr = node let attr = node
.doc_comments_and_attrs() .doc_comments_and_attrs()
@ -673,14 +667,15 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec<AnyDiagnostic>, diag:
.unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index)); .unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index));
( (
ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))), ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))),
Some(attr_name.clone()), attr.path()
.and_then(|path| path.segment())
.and_then(|seg| seg.name_ref())
.as_ref()
.map(ToString::to_string),
) )
} }
}; };
acc.push( acc.push(UnresolvedProcMacro { node, precise_location, macro_name }.into());
UnresolvedProcMacro { node, precise_location, macro_name: name.map(Into::into) }
.into(),
);
} }
DefDiagnosticKind::UnresolvedMacroCall { ast, path } => { DefDiagnosticKind::UnresolvedMacroCall { ast, path } => {

View File

@ -741,23 +741,19 @@ fn macro_call_as_call_id(
fn derive_macro_as_call_id( fn derive_macro_as_call_id(
item_attr: &AstIdWithPath<ast::Adt>, item_attr: &AstIdWithPath<ast::Adt>,
derive_attr: AttrId, derive_attr: AttrId,
derive_pos: u32,
db: &dyn db::DefDatabase, db: &dyn db::DefDatabase,
krate: CrateId, krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>, resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
) -> Result<MacroCallId, UnresolvedMacro> { ) -> Result<MacroCallId, UnresolvedMacro> {
let def: MacroDefId = resolver(item_attr.path.clone()) let def: MacroDefId = resolver(item_attr.path.clone())
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?; .ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
let last_segment = item_attr
.path
.segments()
.last()
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
let res = def.as_lazy_macro( let res = def.as_lazy_macro(
db.upcast(), db.upcast(),
krate, krate,
MacroCallKind::Derive { MacroCallKind::Derive {
ast_id: item_attr.ast_id, ast_id: item_attr.ast_id,
derive_name: last_segment.to_string().into_boxed_str(), derive_index: derive_pos,
derive_attr_index: derive_attr.ast_index, derive_attr_index: derive_attr.ast_index,
}, },
); );
@ -771,8 +767,6 @@ fn attr_macro_as_call_id(
krate: CrateId, krate: CrateId,
def: MacroDefId, def: MacroDefId,
) -> MacroCallId { ) -> MacroCallId {
let attr_path = &item_attr.path;
let last_segment = attr_path.segments().last().expect("empty attribute path");
let mut arg = match macro_attr.input.as_deref() { let mut arg = match macro_attr.input.as_deref() {
Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()), Some(attr::AttrInput::TokenTree(tt, map)) => (tt.clone(), map.clone()),
_ => Default::default(), _ => Default::default(),
@ -786,7 +780,6 @@ fn attr_macro_as_call_id(
krate, krate,
MacroCallKind::Attr { MacroCallKind::Attr {
ast_id: item_attr.ast_id, ast_id: item_attr.ast_id,
attr_name: last_segment.to_string().into_boxed_str(),
attr_args: Arc::new(arg), attr_args: Arc::new(arg),
invoc_attr_index: macro_attr.id.ast_index, invoc_attr_index: macro_attr.id.ast_index,
}, },

View File

@ -1036,6 +1036,9 @@ impl DefCollector<'_> {
fn resolve_macros(&mut self) -> ReachedFixedPoint { fn resolve_macros(&mut self) -> ReachedFixedPoint {
let mut macros = std::mem::take(&mut self.unresolved_macros); let mut macros = std::mem::take(&mut self.unresolved_macros);
let mut resolved = Vec::new(); let mut resolved = Vec::new();
let mut push_resolved = |directive: &MacroDirective, call_id| {
resolved.push((directive.module_id, directive.depth, directive.container, call_id));
};
let mut res = ReachedFixedPoint::Yes; let mut res = ReachedFixedPoint::Yes;
macros.retain(|directive| { macros.retain(|directive| {
let resolver = |path| { let resolver = |path| {
@ -1060,12 +1063,7 @@ impl DefCollector<'_> {
&mut |_err| (), &mut |_err| (),
); );
if let Ok(Ok(call_id)) = call_id { if let Ok(Ok(call_id)) = call_id {
resolved.push(( push_resolved(directive, call_id);
directive.module_id,
call_id,
directive.depth,
directive.container,
));
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
return false; return false;
} }
@ -1074,6 +1072,7 @@ impl DefCollector<'_> {
let call_id = derive_macro_as_call_id( let call_id = derive_macro_as_call_id(
ast_id, ast_id,
*derive_attr, *derive_attr,
*derive_pos as u32,
self.db, self.db,
self.def_map.krate, self.def_map.krate,
&resolver, &resolver,
@ -1086,12 +1085,7 @@ impl DefCollector<'_> {
*derive_pos, *derive_pos,
); );
resolved.push(( push_resolved(directive, call_id);
directive.module_id,
call_id,
directive.depth,
directive.container,
));
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
return false; return false;
} }
@ -1229,12 +1223,7 @@ impl DefCollector<'_> {
.scope .scope
.add_attr_macro_invoc(ast_id, call_id); .add_attr_macro_invoc(ast_id, call_id);
resolved.push(( push_resolved(directive, call_id);
directive.module_id,
call_id,
directive.depth,
directive.container,
));
res = ReachedFixedPoint::No; res = ReachedFixedPoint::No;
return false; return false;
} }
@ -1245,7 +1234,7 @@ impl DefCollector<'_> {
// Attribute resolution can add unresolved macro invocations, so concatenate the lists. // Attribute resolution can add unresolved macro invocations, so concatenate the lists.
self.unresolved_macros.extend(macros); self.unresolved_macros.extend(macros);
for (module_id, macro_call_id, depth, container) in resolved { for (module_id, depth, container, macro_call_id) in resolved {
self.collect_macro_expansion(module_id, macro_call_id, depth, container); self.collect_macro_expansion(module_id, macro_call_id, depth, container);
} }

View File

@ -336,6 +336,7 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy
ast::Item::cast(node.clone())? ast::Item::cast(node.clone())?
.attrs() .attrs()
.take(derive_attr_index as usize + 1) .take(derive_attr_index as usize + 1)
// FIXME
.filter(|attr| attr.simple_name().as_deref() == Some("derive")) .filter(|attr| attr.simple_name().as_deref() == Some("derive"))
.map(|it| it.syntax().clone()) .map(|it| it.syntax().clone())
.collect() .collect()

View File

@ -125,16 +125,16 @@ pub enum MacroCallKind {
}, },
Derive { Derive {
ast_id: AstId<ast::Adt>, ast_id: AstId<ast::Adt>,
derive_name: Box<str>,
/// Syntactical index of the invoking `#[derive]` attribute. /// Syntactical index of the invoking `#[derive]` attribute.
/// ///
/// Outer attributes are counted first, then inner attributes. This does not support /// Outer attributes are counted first, then inner attributes. This does not support
/// out-of-line modules, which may have attributes spread across 2 files! /// out-of-line modules, which may have attributes spread across 2 files!
derive_attr_index: u32, derive_attr_index: u32,
/// Index of the derive macro in the derive attribute
derive_index: u32,
}, },
Attr { Attr {
ast_id: AstId<ast::Item>, ast_id: AstId<ast::Item>,
attr_name: Box<str>,
attr_args: Arc<(tt::Subtree, mbe::TokenMap)>, attr_args: Arc<(tt::Subtree, mbe::TokenMap)>,
/// Syntactical index of the invoking `#[attribute]`. /// Syntactical index of the invoking `#[attribute]`.
/// ///