Skip to content

Store invocation site for eager macros #7000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/hir_expand/src/builtin_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,15 @@ mod tests {

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

let arg_id = db.intern_eager_expansion({
EagerCallLoc {
def,
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
krate,
file_id: file_id.into(),
call: call_id,
}
});

Expand All @@ -580,7 +581,7 @@ mod tests {
fragment,
subtree: Arc::new(subtree),
krate,
file_id: file_id.into(),
call: call_id,
};

let id: MacroCallId = db.intern_eager_expansion(eager).into();
Expand Down
9 changes: 5 additions & 4 deletions crates/hir_expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use base_db::{salsa, SourceDatabase};
use mbe::{ExpandError, ExpandResult, MacroRules};
use parser::FragmentKind;
use syntax::{algo::diff, AstNode, GreenNode, Parse, SyntaxKind::*, SyntaxNode};
use syntax::{algo::diff, ast::NameOwner, AstNode, GreenNode, Parse, SyntaxKind::*, SyntaxNode};

use crate::{
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId,
Expand Down Expand Up @@ -129,19 +129,20 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
match id.kind {
MacroDefKind::Declarative => {
let macro_call = match id.ast_id?.to_node(db) {
let macro_rules = match id.ast_id?.to_node(db) {
syntax::ast::Macro::MacroRules(mac) => mac,
syntax::ast::Macro::MacroDef(_) => return None,
};
let arg = macro_call.token_tree()?;
let arg = macro_rules.token_tree()?;
let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| {
log::warn!("fail on macro_def to token tree: {:#?}", arg);
None
})?;
let rules = match MacroRules::parse(&tt) {
Ok(it) => it,
Err(err) => {
log::warn!("fail on macro_def parse: error: {:#?} {:#?}", err, tt);
let name = macro_rules.name().map(|n| n.to_string()).unwrap_or_default();
log::warn!("fail on macro_def parse ({}): {:?} {:#?}", name, err, tt);
return None;
}
};
Expand Down
14 changes: 6 additions & 8 deletions crates/hir_expand/src/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ pub fn expand_eager_macro(
|| err("malformed macro invocation"),
)?;

let ast_map = db.ast_id_map(macro_call.file_id);
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));

// Note:
// When `lazy_expand` is called, its *parent* file must be already exists.
// Here we store an eager macro id for the argument expanded subtree here
Expand All @@ -120,7 +123,7 @@ pub fn expand_eager_macro(
fragment: FragmentKind::Expr,
subtree: Arc::new(parsed_args.clone()),
krate,
file_id: macro_call.file_id,
call: call_id,
}
});
let arg_file_id: MacroCallId = arg_id.into();
Expand All @@ -141,13 +144,8 @@ pub fn expand_eager_macro(
let res = eager.expand(db, arg_id, &subtree);

let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?;
let eager = EagerCallLoc {
def,
fragment,
subtree: Arc::new(subtree),
krate,
file_id: macro_call.file_id,
};
let eager =
EagerCallLoc { def, fragment, subtree: Arc::new(subtree), krate, call: call_id };

Ok(db.intern_eager_expansion(eager))
} else {
Expand Down
27 changes: 13 additions & 14 deletions crates/hir_expand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl HirFileId {
}
MacroCallId::EagerMacro(id) => {
let loc = db.lookup_intern_eager_expansion(id);
loc.file_id
loc.call.file_id
}
};
file_id.original_file(db)
Expand All @@ -103,7 +103,7 @@ impl HirFileId {
}
MacroCallId::EagerMacro(id) => {
let loc = db.lookup_intern_eager_expansion(id);
loc.file_id
loc.call.file_id
}
};
}
Expand All @@ -114,17 +114,16 @@ impl HirFileId {
pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> {
match self.0 {
HirFileIdRepr::FileId(_) => None,
HirFileIdRepr::MacroFile(macro_file) => {
let lazy_id = match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => id,
MacroCallId::EagerMacro(_id) => {
// FIXME: handle call node for eager macro
return None;
}
};
let loc = db.lookup_intern_macro(lazy_id);
Some(loc.kind.node(db))
}
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
MacroCallId::LazyMacro(lazy_id) => {
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
Some(loc.kind.node(db))
}
MacroCallId::EagerMacro(id) => {
let loc: EagerCallLoc = db.lookup_intern_eager_expansion(id);
Some(loc.call.with_value(loc.call.to_node(db).syntax().clone()))
}
},
}
}

Expand Down Expand Up @@ -304,7 +303,7 @@ pub struct EagerCallLoc {
pub(crate) fragment: FragmentKind,
pub(crate) subtree: Arc<tt::Subtree>,
pub(crate) krate: CrateId,
pub(crate) file_id: HirFileId,
pub(crate) call: AstId<ast::MacroCall>,
}

/// ExpansionInfo mainly describes how to map text range between src and expanded macro
Expand Down
25 changes: 25 additions & 0 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,31 @@ fn test() {
);
}

#[test]
fn goto_through_included_file() {
check(
r#"
//- /main.rs
#[rustc_builtin_macro]
macro_rules! include {}

include!("foo.rs");
//^^^^^^^^^^^^^^^^^^^

fn f() {
foo<|>();
}

mod confuse_index {
pub fn foo() {}
}

//- /foo.rs
fn foo() {}
"#,
);
}

#[test]
fn goto_for_type_param() {
check(
Expand Down