Use named fields in MacroCallKind

This commit is contained in:
Jonas Schievink 2021-04-08 20:43:07 +02:00
parent 5f279d57f0
commit 86b7861612
7 changed files with 40 additions and 33 deletions

View file

@ -690,7 +690,9 @@ fn macro_call_as_call_id(
) )
.map(MacroCallId::from) .map(MacroCallId::from)
} else { } else {
Ok(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(call.ast_id)).into()) Ok(def
.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike { ast_id: call.ast_id })
.into())
}; };
Ok(res) Ok(res)
} }
@ -707,7 +709,10 @@ fn derive_macro_as_call_id(
.as_lazy_macro( .as_lazy_macro(
db.upcast(), db.upcast(),
krate, krate,
MacroCallKind::Derive(item_attr.ast_id, last_segment.to_string()), MacroCallKind::Derive {
ast_id: item_attr.ast_id,
derive_name: last_segment.to_string(),
},
) )
.into(); .into();
Ok(res) Ok(res)

View file

@ -613,12 +613,12 @@ mod diagnostics {
DiagnosticKind::UnresolvedProcMacro { ast } => { DiagnosticKind::UnresolvedProcMacro { ast } => {
let mut precise_location = None; let mut precise_location = None;
let (file, ast, name) = match ast { let (file, ast, name) = match ast {
MacroCallKind::FnLike(ast) => { MacroCallKind::FnLike { ast_id } => {
let node = ast.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None) (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)), None)
} }
MacroCallKind::Derive(ast, name) => { MacroCallKind::Derive { ast_id, derive_name } => {
let node = ast.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.
@ -639,7 +639,7 @@ mod diagnostics {
}); });
for token in tokens { for token in tokens {
if token.kind() == SyntaxKind::IDENT if token.kind() == SyntaxKind::IDENT
&& token.text() == name.as_str() && token.text() == derive_name.as_str()
{ {
precise_location = Some(token.text_range()); precise_location = Some(token.text_range());
break 'outer; break 'outer;
@ -648,9 +648,9 @@ mod diagnostics {
} }
( (
ast.file_id, ast_id.file_id,
SyntaxNodePtr::from(AstPtr::new(&node)), SyntaxNodePtr::from(AstPtr::new(&node)),
Some(name.clone()), Some(derive_name.clone()),
) )
} }
}; };
@ -669,13 +669,13 @@ mod diagnostics {
DiagnosticKind::MacroError { ast, message } => { DiagnosticKind::MacroError { ast, message } => {
let (file, ast) = match ast { let (file, ast) = match ast {
MacroCallKind::FnLike(ast) => { MacroCallKind::FnLike { ast_id, .. } => {
let node = ast.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
} }
MacroCallKind::Derive(ast, _) => { MacroCallKind::Derive { ast_id, .. } => {
let node = ast.to_node(db.upcast()); let node = ast_id.to_node(db.upcast());
(ast.file_id, SyntaxNodePtr::from(AstPtr::new(&node))) (ast_id.file_id, SyntaxNodePtr::from(AstPtr::new(&node)))
} }
}; };
sink.push(MacroError { file, node: ast, message: message.clone() }); sink.push(MacroError { file, node: ast, message: message.clone() });

View file

@ -1520,7 +1520,7 @@ impl ModCollector<'_, '_> {
// Built-in macro failed eager expansion. // Built-in macro failed eager expansion.
self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error( self.def_collector.def_map.diagnostics.push(DefDiagnostic::macro_error(
self.module_id, self.module_id,
MacroCallKind::FnLike(ast_id.ast_id), MacroCallKind::FnLike { ast_id: ast_id.ast_id },
error.unwrap().to_string(), error.unwrap().to_string(),
)); ));
return; return;

View file

@ -308,7 +308,7 @@ $0
let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap(); let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0])); let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
let loc = MacroCallLoc { let loc = MacroCallLoc {
def: MacroDefId { def: MacroDefId {
@ -317,7 +317,7 @@ $0
local_inner: false, local_inner: false,
}, },
krate: CrateId(0), krate: CrateId(0),
kind: MacroCallKind::Derive(attr_id, name.to_string()), kind: MacroCallKind::Derive { ast_id, derive_name: name.to_string() },
}; };
let id: MacroCallId = db.intern_macro(loc).into(); let id: MacroCallId = db.intern_macro(loc).into();

View file

@ -566,10 +566,9 @@ mod tests {
let loc = MacroCallLoc { let loc = MacroCallLoc {
def, def,
krate, krate,
kind: MacroCallKind::FnLike(AstId::new( kind: MacroCallKind::FnLike {
file_id.into(), ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call)),
ast_id_map.ast_id(&macro_call), },
)),
}; };
let id: MacroCallId = db.intern_macro(loc).into(); let id: MacroCallId = db.intern_macro(loc).into();

View file

@ -174,8 +174,9 @@ fn lazy_expand(
) -> ExpandResult<Option<InFile<SyntaxNode>>> { ) -> ExpandResult<Option<InFile<SyntaxNode>>> {
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value); let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
let id: MacroCallId = let id: MacroCallId = def
def.as_lazy_macro(db, krate, MacroCallKind::FnLike(macro_call.with_value(ast_id))).into(); .as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id) })
.into();
let err = db.macro_expand_error(id); let err = db.macro_expand_error(id);
let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node)); let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node));

View file

@ -290,22 +290,24 @@ pub struct MacroCallLoc {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MacroCallKind { pub enum MacroCallKind {
FnLike(AstId<ast::MacroCall>), FnLike { ast_id: AstId<ast::MacroCall> },
Derive(AstId<ast::Item>, String), Derive { ast_id: AstId<ast::Item>, derive_name: String },
} }
impl MacroCallKind { impl MacroCallKind {
fn file_id(&self) -> HirFileId { fn file_id(&self) -> HirFileId {
match self { match self {
MacroCallKind::FnLike(ast_id) => ast_id.file_id, MacroCallKind::FnLike { ast_id, .. } => ast_id.file_id,
MacroCallKind::Derive(ast_id, _) => ast_id.file_id, MacroCallKind::Derive { ast_id, .. } => ast_id.file_id,
} }
} }
fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { fn node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> {
match self { match self {
MacroCallKind::FnLike(ast_id) => ast_id.with_value(ast_id.to_node(db).syntax().clone()), MacroCallKind::FnLike { ast_id, .. } => {
MacroCallKind::Derive(ast_id, _) => { ast_id.with_value(ast_id.to_node(db).syntax().clone())
}
MacroCallKind::Derive { ast_id, .. } => {
ast_id.with_value(ast_id.to_node(db).syntax().clone()) ast_id.with_value(ast_id.to_node(db).syntax().clone())
} }
} }
@ -313,10 +315,10 @@ impl MacroCallKind {
fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> {
match self { match self {
MacroCallKind::FnLike(ast_id) => { MacroCallKind::FnLike { ast_id, .. } => {
Some(ast_id.to_node(db).token_tree()?.syntax().clone()) Some(ast_id.to_node(db).token_tree()?.syntax().clone())
} }
MacroCallKind::Derive(ast_id, _) => Some(ast_id.to_node(db).syntax().clone()), MacroCallKind::Derive { ast_id, .. } => Some(ast_id.to_node(db).syntax().clone()),
} }
} }
} }