Skip to content

Commit 493e29d

Browse files
Merge #7000
7000: Store invocation site for eager macros r=edwin0cheng a=jonas-schievink Fixes #6992 r? @edwin0cheng I'm not sure if this is totally correct, it looks like we create **two** `EagerCallLoc`s per macro invocation, one for the arguments (?), and one for the actual macro call. I gave both the same `AstId`, hopefully that's correct. Co-authored-by: Jonas Schievink <[email protected]>
2 parents 4a2f60c + 26f604b commit 493e29d

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

crates/hir_expand/src/builtin_macro.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,15 @@ mod tests {
563563

564564
let args = macro_call.token_tree().unwrap();
565565
let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0;
566+
let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_call));
566567

567568
let arg_id = db.intern_eager_expansion({
568569
EagerCallLoc {
569570
def,
570571
fragment: FragmentKind::Expr,
571572
subtree: Arc::new(parsed_args.clone()),
572573
krate,
573-
file_id: file_id.into(),
574+
call: call_id,
574575
}
575576
});
576577

@@ -580,7 +581,7 @@ mod tests {
580581
fragment,
581582
subtree: Arc::new(subtree),
582583
krate,
583-
file_id: file_id.into(),
584+
call: call_id,
584585
};
585586

586587
let id: MacroCallId = db.intern_eager_expansion(eager).into();

crates/hir_expand/src/db.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use base_db::{salsa, SourceDatabase};
66
use mbe::{ExpandError, ExpandResult, MacroRules};
77
use parser::FragmentKind;
8-
use syntax::{algo::diff, AstNode, GreenNode, Parse, SyntaxKind::*, SyntaxNode};
8+
use syntax::{algo::diff, ast::NameOwner, AstNode, GreenNode, Parse, SyntaxKind::*, SyntaxNode};
99

1010
use crate::{
1111
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId,
@@ -129,19 +129,20 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
129129
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
130130
match id.kind {
131131
MacroDefKind::Declarative => {
132-
let macro_call = match id.ast_id?.to_node(db) {
132+
let macro_rules = match id.ast_id?.to_node(db) {
133133
syntax::ast::Macro::MacroRules(mac) => mac,
134134
syntax::ast::Macro::MacroDef(_) => return None,
135135
};
136-
let arg = macro_call.token_tree()?;
136+
let arg = macro_rules.token_tree()?;
137137
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
138138
log::warn!("fail on macro_def to token tree: {:#?}", arg);
139139
None
140140
})?;
141141
let rules = match MacroRules::parse(&tt) {
142142
Ok(it) => it,
143143
Err(err) => {
144-
log::warn!("fail on macro_def parse: error: {:#?} {:#?}", err, tt);
144+
let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default();
145+
log::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
145146
return None;
146147
}
147148
};

crates/hir_expand/src/eager.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ pub fn expand_eager_macro(
110110
|| err("malformed macro invocation"),
111111
)?;
112112

113+
let ast_map = db.ast_id_map(macro_call.file_id);
114+
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
115+
113116
// Note:
114117
// When `lazy_expand` is called, its *parent* file must be already exists.
115118
// Here we store an eager macro id for the argument expanded subtree here
@@ -120,7 +123,7 @@ pub fn expand_eager_macro(
120123
fragment: FragmentKind::Expr,
121124
subtree: Arc::new(parsed_args.clone()),
122125
krate,
123-
file_id: macro_call.file_id,
126+
call: call_id,
124127
}
125128
});
126129
let arg_file_id: MacroCallId = arg_id.into();
@@ -141,13 +144,8 @@ pub fn expand_eager_macro(
141144
let res = eager.expand(db, arg_id, &subtree);
142145

143146
let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?;
144-
let eager = EagerCallLoc {
145-
def,
146-
fragment,
147-
subtree: Arc::new(subtree),
148-
krate,
149-
file_id: macro_call.file_id,
150-
};
147+
let eager =
148+
EagerCallLoc { def, fragment, subtree: Arc::new(subtree), krate, call: call_id };
151149

152150
Ok(db.intern_eager_expansion(eager))
153151
} else {

crates/hir_expand/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl HirFileId {
8383
}
8484
MacroCallId::EagerMacro(id) => {
8585
let loc = db.lookup_intern_eager_expansion(id);
86-
loc.file_id
86+
loc.call.file_id
8787
}
8888
};
8989
file_id.original_file(db)
@@ -103,7 +103,7 @@ impl HirFileId {
103103
}
104104
MacroCallId::EagerMacro(id) => {
105105
let loc = db.lookup_intern_eager_expansion(id);
106-
loc.file_id
106+
loc.call.file_id
107107
}
108108
};
109109
}
@@ -114,17 +114,16 @@ impl HirFileId {
114114
pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> {
115115
match self.0 {
116116
HirFileIdRepr::FileId(_) => None,
117-
HirFileIdRepr::MacroFile(macro_file) => {
118-
let lazy_id = match macro_file.macro_call_id {
119-
MacroCallId::LazyMacro(id) => id,
120-
MacroCallId::EagerMacro(_id) => {
121-
// FIXME: handle call node for eager macro
122-
return None;
123-
}
124-
};
125-
let loc = db.lookup_intern_macro(lazy_id);
126-
Some(loc.kind.node(db))
127-
}
117+
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
118+
MacroCallId::LazyMacro(lazy_id) => {
119+
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
120+
Some(loc.kind.node(db))
121+
}
122+
MacroCallId::EagerMacro(id) => {
123+
let loc: EagerCallLoc = db.lookup_intern_eager_expansion(id);
124+
Some(loc.call.with_value(loc.call.to_node(db).syntax().clone()))
125+
}
126+
},
128127
}
129128
}
130129

@@ -304,7 +303,7 @@ pub struct EagerCallLoc {
304303
pub(crate) fragment: FragmentKind,
305304
pub(crate) subtree: Arc<tt::Subtree>,
306305
pub(crate) krate: CrateId,
307-
pub(crate) file_id: HirFileId,
306+
pub(crate) call: AstId<ast::MacroCall>,
308307
}
309308

310309
/// ExpansionInfo mainly describes how to map text range between src and expanded macro

crates/ide/src/goto_definition.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,31 @@ fn test() {
749749
);
750750
}
751751

752+
#[test]
753+
fn goto_through_included_file() {
754+
check(
755+
r#"
756+
//- /main.rs
757+
#[rustc_builtin_macro]
758+
macro_rules! include {}
759+
760+
include!("foo.rs");
761+
//^^^^^^^^^^^^^^^^^^^
762+
763+
fn f() {
764+
foo<|>();
765+
}
766+
767+
mod confuse_index {
768+
pub fn foo() {}
769+
}
770+
771+
//- /foo.rs
772+
fn foo() {}
773+
"#,
774+
);
775+
}
776+
752777
#[test]
753778
fn goto_for_type_param() {
754779
check(

0 commit comments

Comments
 (0)