Skip to content

Commit cc6d7cf

Browse files
committed
macro_arg_considering_derives is now in ExpandDatabase and now takes in the MacroCallKind
1 parent 8b78a18 commit cc6d7cf

File tree

1 file changed

+20
-11
lines changed
  • crates/hir-expand/src

1 file changed

+20
-11
lines changed

crates/hir-expand/src/db.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use crate::{
2424
HirFileId, HirFileIdRepr, MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
2525
MacroFileId,
2626
};
27-
27+
/// This is just to ensure the types of smart_macro_arg and macro_arg are the same
28+
type MacroArgResult = (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span);
2829
/// Total limit on the number of tokens produced by any macro invocation.
2930
///
3031
/// If an invocation produces more tokens than this limit, it will not be stored in the database and
@@ -98,7 +99,13 @@ pub trait ExpandDatabase: SourceDatabase {
9899
/// Lowers syntactic macro call to a token tree representation. That's a firewall
99100
/// query, only typing in the macro call itself changes the returned
100101
/// subtree.
101-
fn macro_arg(&self, id: MacroCallId) -> (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span);
102+
fn macro_arg(&self, id: MacroCallId) -> MacroArgResult;
103+
#[salsa::transparent]
104+
fn macro_arg_considering_derives(
105+
&self,
106+
id: MacroCallId,
107+
kind: &MacroCallKind,
108+
) -> MacroArgResult;
102109
/// Fetches the expander for this macro.
103110
#[salsa::transparent]
104111
#[salsa::invoke(TokenExpander::macro_expander)]
@@ -339,20 +346,21 @@ pub(crate) fn parse_with_map(
339346
}
340347
}
341348
}
342-
/// This is just to ensure the types of smart_macro_arg and macro_arg are the same
343-
type MacroArgResult = (Arc<tt::Subtree>, SyntaxFixupUndoInfo, Span);
349+
344350
/// Imagine the word smart in quotes.
345351
///
346352
/// This resolves the [MacroCallId] to check if it is a derive macro if so get the [macro_arg] for the derive.
347353
/// Other wise return the [macro_arg] for the macro_call_id.
348354
///
349355
/// This is not connected to the database so it does not cached the result. However, the inner [macro_arg] query is
350-
fn macro_arg_considering_derives(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult {
351-
let macro_call_kind = db.lookup_intern_macro_call(id).kind;
352-
// FIXME: We called lookup_intern_macro_call twice.
353-
match macro_call_kind {
356+
fn macro_arg_considering_derives(
357+
db: &dyn ExpandDatabase,
358+
id: MacroCallId,
359+
kind: &MacroCallKind,
360+
) -> MacroArgResult {
361+
match kind {
354362
// Get the macro arg for the derive macro
355-
MacroCallKind::Derive { derive_macro_id, .. } => db.macro_arg(derive_macro_id),
363+
MacroCallKind::Derive { derive_macro_id, .. } => db.macro_arg(*derive_macro_id),
356364
// Normal macro arg
357365
_ => db.macro_arg(id),
358366
}
@@ -542,7 +550,8 @@ fn macro_expand(
542550
let (ExpandResult { value: tt, err }, span) = match loc.def.kind {
543551
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
544552
_ => {
545-
let (macro_arg, undo_info, span) = macro_arg_considering_derives(db, macro_call_id);
553+
let (macro_arg, undo_info, span) =
554+
db.macro_arg_considering_derives(macro_call_id, &loc.kind);
546555

547556
let arg = &*macro_arg;
548557
let res =
@@ -619,7 +628,7 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId<ast::Fn>) -> Span {
619628

620629
fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<Arc<tt::Subtree>> {
621630
let loc = db.lookup_intern_macro_call(id);
622-
let (macro_arg, undo_info, span) = macro_arg_considering_derives(db, id);
631+
let (macro_arg, undo_info, span) = db.macro_arg_considering_derives(id, &loc.kind.clone());
623632

624633
let (expander, ast) = match loc.def.kind {
625634
MacroDefKind::ProcMacro(expander, _, ast) => (expander, ast),

0 commit comments

Comments
 (0)