From ea8555b1552f6c08043f84885e47a196320da376 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 19 May 2021 20:19:08 +0200 Subject: Simplify eager macro representation --- crates/hir_expand/src/builtin_derive.rs | 25 +++---- crates/hir_expand/src/builtin_macro.rs | 81 ++++++++++----------- crates/hir_expand/src/db.rs | 120 ++++++++++--------------------- crates/hir_expand/src/eager.rs | 32 ++++----- crates/hir_expand/src/hygiene.rs | 34 ++++----- crates/hir_expand/src/input.rs | 4 +- crates/hir_expand/src/lib.rs | 124 ++++++++------------------------ 7 files changed, 156 insertions(+), 264 deletions(-) (limited to 'crates/hir_expand/src') diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index b6a6d602f..fe9497b50 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs @@ -8,7 +8,7 @@ use syntax::{ match_ast, }; -use crate::{db::AstDatabase, name, quote, AstId, CrateId, LazyMacroId, MacroDefId, MacroDefKind}; +use crate::{db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind}; macro_rules! register_builtin { ( $($trait:ident => $expand:ident),* ) => { @@ -21,7 +21,7 @@ macro_rules! register_builtin { pub fn expand( &self, db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let expander = match *self { @@ -164,7 +164,7 @@ fn expand_simple_derive( Ok(expanded) } -fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree { +fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree { // FIXME: make hygiene works for builtin derive macro // such that $crate can be used here. let cg = db.crate_graph(); @@ -184,7 +184,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree { fn copy_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -193,7 +193,7 @@ fn copy_expand( fn clone_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -202,7 +202,7 @@ fn clone_expand( fn default_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -211,7 +211,7 @@ fn default_expand( fn debug_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -220,7 +220,7 @@ fn debug_expand( fn hash_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -229,7 +229,7 @@ fn hash_expand( fn eq_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -238,7 +238,7 @@ fn eq_expand( fn partial_eq_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -247,7 +247,7 @@ fn partial_eq_expand( fn ord_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -256,7 +256,7 @@ fn ord_expand( fn partial_ord_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> Result { let krate = find_builtin_crate(db, id); @@ -317,6 +317,7 @@ $0 local_inner: false, }, krate: CrateId(0), + eager: None, kind: MacroCallKind::Derive { ast_id, derive_name: name.to_string(), diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 280c25f11..09ac16700 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -1,7 +1,7 @@ //! Builtin macro use crate::{ - db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId, - MacroCallLoc, MacroDefId, MacroDefKind, TextSize, + db::AstDatabase, name, quote, AstId, CrateId, MacroCallId, MacroCallLoc, MacroDefId, + MacroDefKind, TextSize, }; use base_db::{AnchoredPath, Edition, FileId}; @@ -27,7 +27,7 @@ macro_rules! register_builtin { pub fn expand( &self, db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { let expander = match *self { @@ -41,7 +41,7 @@ macro_rules! register_builtin { pub fn expand( &self, db: &dyn AstDatabase, - arg_id: EagerMacroId, + arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let expander = match *self { @@ -128,7 +128,7 @@ register_builtin! { fn module_path_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // Just return a dummy result. @@ -137,7 +137,7 @@ fn module_path_expand( fn line_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // dummy implementation for type-checking purposes @@ -151,7 +151,7 @@ fn line_expand( fn stringify_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { let loc = db.lookup_intern_macro(id); @@ -176,7 +176,7 @@ fn stringify_expand( fn column_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // dummy implementation for type-checking purposes @@ -190,7 +190,7 @@ fn column_expand( fn assert_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { // A hacky implementation for goto def and hover @@ -214,7 +214,7 @@ fn assert_expand( fn file_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // FIXME: RA purposefully lacks knowledge of absolute file names @@ -230,7 +230,7 @@ fn file_expand( fn format_args_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { // We expand `format_args!("", a1, a2)` to @@ -265,7 +265,7 @@ fn format_args_expand( fn asm_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // both asm and llvm_asm don't return anything, so we can expand them to nothing, @@ -278,7 +278,7 @@ fn asm_expand( fn global_asm_expand( _db: &dyn AstDatabase, - _id: LazyMacroId, + _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult { // Expand to nothing (at item-level) @@ -287,7 +287,7 @@ fn global_asm_expand( fn cfg_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { let loc = db.lookup_intern_macro(id); @@ -299,7 +299,7 @@ fn cfg_expand( fn panic_expand( db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult { let loc: MacroCallLoc = db.lookup_intern_macro(id); @@ -324,7 +324,7 @@ fn unquote_str(lit: &tt::Literal) -> Option { fn compile_error_expand( _db: &dyn AstDatabase, - _id: EagerMacroId, + _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let err = match &*tt.token_trees { @@ -345,7 +345,7 @@ fn compile_error_expand( fn concat_expand( _db: &dyn AstDatabase, - _arg_id: EagerMacroId, + _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let mut err = None; @@ -376,7 +376,7 @@ fn concat_expand( fn concat_idents_expand( _db: &dyn AstDatabase, - _arg_id: EagerMacroId, + _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let mut err = None; @@ -427,7 +427,7 @@ fn parse_string(tt: &tt::Subtree) -> Result { fn include_expand( db: &dyn AstDatabase, - arg_id: EagerMacroId, + arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let res = (|| { @@ -457,7 +457,7 @@ fn include_expand( fn include_bytes_expand( _db: &dyn AstDatabase, - _arg_id: EagerMacroId, + _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { if let Err(e) = parse_string(tt) { @@ -477,7 +477,7 @@ fn include_bytes_expand( fn include_str_expand( db: &dyn AstDatabase, - arg_id: EagerMacroId, + arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let path = match parse_string(tt) { @@ -502,14 +502,14 @@ fn include_str_expand( ExpandResult::ok(Some(ExpandedEager::new(quote!(#text), FragmentKind::Expr))) } -fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option { - let krate = db.lookup_intern_eager_expansion(arg_id).krate; +fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option { + let krate = db.lookup_intern_macro(arg_id).krate; db.crate_graph()[krate].env.get(key) } fn env_expand( db: &dyn AstDatabase, - arg_id: EagerMacroId, + arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let key = match parse_string(tt) { @@ -540,7 +540,7 @@ fn env_expand( fn option_env_expand( db: &dyn AstDatabase, - arg_id: EagerMacroId, + arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult> { let key = match parse_string(tt) { @@ -560,7 +560,7 @@ fn option_env_expand( mod tests { use super::*; use crate::{ - name::AsName, test_db::TestDB, AstNode, EagerCallLoc, MacroCallId, MacroCallKind, + name::AsName, test_db::TestDB, AstNode, EagerCallInfo, MacroCallId, MacroCallKind, MacroCallLoc, }; use base_db::{fixture::WithFixture, SourceDatabase}; @@ -599,6 +599,7 @@ mod tests { let loc = MacroCallLoc { def, krate, + eager: None, kind: MacroCallKind::FnLike { ast_id: AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)), fragment: FragmentKind::Expr, @@ -620,28 +621,28 @@ mod tests { let parsed_args = mbe::ast_to_token_tree(&args).0; let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); - let arg_id = db.intern_eager_expansion({ - EagerCallLoc { - def, - fragment: FragmentKind::Expr, - subtree: Arc::new(parsed_args.clone()), - krate, - call: call_id, + let arg_id = db.intern_macro(MacroCallLoc { + def, + krate, + eager: Some(EagerCallInfo { + expansion: Arc::new(parsed_args.clone()), included_file: None, - } + }), + kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr }, }); let expanded = expander.expand(&db, arg_id, &parsed_args).value.unwrap(); - let eager = EagerCallLoc { + let loc = MacroCallLoc { def, - fragment: expanded.fragment, - subtree: Arc::new(expanded.subtree), krate, - call: call_id, - included_file: expanded.included_file, + eager: Some(EagerCallInfo { + expansion: Arc::new(expanded.subtree), + included_file: expanded.included_file, + }), + kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment }, }; - let id: MacroCallId = db.intern_eager_expansion(eager).into(); + let id: MacroCallId = db.intern_macro(loc).into(); id.as_file() } }; diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index c43d382ad..7a82400a8 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -13,8 +13,8 @@ use syntax::{ use crate::{ ast_id_map::AstIdMap, hygiene::HygieneFrame, input::process_macro_input, BuiltinDeriveExpander, - BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId, HirFileId, HirFileIdRepr, LazyMacroId, - MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFile, ProcMacroExpander, + BuiltinFnLikeExpander, HirFileId, HirFileIdRepr, MacroCallId, MacroCallLoc, MacroDefId, + MacroDefKind, MacroFile, ProcMacroExpander, }; /// Total limit on the number of tokens produced by any macro invocation. @@ -41,7 +41,7 @@ impl TokenExpander { fn expand( &self, db: &dyn AstDatabase, - id: LazyMacroId, + id: MacroCallId, tt: &tt::Subtree, ) -> mbe::ExpandResult { match self { @@ -101,11 +101,7 @@ pub trait AstDatabase: SourceDatabase { /// We encode macro definitions into ids of macro calls, this what allows us /// to be incremental. #[salsa::interned] - fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId; - /// Certain built-in macros are eager (`format!(concat!("file: ", file!(), "{}"")), 92`). - /// For them, we actually want to encode the whole token tree as an argument. - #[salsa::interned] - fn intern_eager_expansion(&self, eager: EagerCallLoc) -> EagerMacroId; + fn intern_macro(&self, macro_call: MacroCallLoc) -> MacroCallId; /// Lowers syntactic macro call to a token tree representation. #[salsa::transparent] @@ -146,17 +142,12 @@ pub fn expand_hypothetical( token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; let token_id = tmap_1.token_by_range(range)?; - let lazy_id = match actual_macro_call { - MacroCallId::LazyMacro(id) => id, - MacroCallId::EagerMacro(_) => return None, - }; - let macro_def = { - let loc = db.lookup_intern_macro(lazy_id); + let loc: MacroCallLoc = db.lookup_intern_macro(actual_macro_call); db.macro_def(loc.def)? }; - let hypothetical_expansion = macro_def.expand(db, lazy_id, &tt); + let hypothetical_expansion = macro_def.expand(db, actual_macro_call, &tt); let fragment_kind = macro_fragment_kind(db, actual_macro_call); @@ -194,30 +185,22 @@ fn parse_macro_expansion( // Note: // The final goal we would like to make all parse_macro success, // such that the following log will not call anyway. - match macro_file.macro_call_id { - MacroCallId::LazyMacro(id) => { - let loc: MacroCallLoc = db.lookup_intern_macro(id); - let node = loc.kind.node(db); - - // collect parent information for warning log - let parents = std::iter::successors(loc.kind.file_id().call_node(db), |it| { - it.file_id.call_node(db) - }) + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + let node = loc.kind.node(db); + + // collect parent information for warning log + let parents = + std::iter::successors(loc.kind.file_id().call_node(db), |it| it.file_id.call_node(db)) .map(|n| format!("{:#}", n.value)) .collect::>() .join("\n"); - log::warn!( - "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}", - err, - node.value, - parents - ); - } - _ => { - log::warn!("fail on macro_parse: (reason: {:?})", err); - } - } + log::warn!( + "fail on macro_parse: (reason: {:?} macro_call: {:#}) parents: {}", + err, + node.value, + parents + ); } let tt = match result.value { Some(tt) => tt, @@ -269,25 +252,16 @@ fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option Option { - let id = match id { - MacroCallId::LazyMacro(id) => id, - MacroCallId::EagerMacro(_id) => { - // FIXME: support macro_arg for eager macro - return None; - } - }; let loc = db.lookup_intern_macro(id); let arg = loc.kind.arg(db)?; let arg = process_macro_input(db, arg, id); @@ -347,24 +321,21 @@ fn macro_expand_with_arg( arg: Option>, ) -> ExpandResult>> { let _p = profile::span("macro_expand"); - let lazy_id = match id { - MacroCallId::LazyMacro(id) => id, - MacroCallId::EagerMacro(id) => { - if arg.is_some() { - return ExpandResult::str_err( - "hypothetical macro expansion not implemented for eager macro".to_owned(), - ); - } else { - return ExpandResult { - value: Some(db.lookup_intern_eager_expansion(id).subtree), - // FIXME: There could be errors here! - err: None, - }; - } + let loc: MacroCallLoc = db.lookup_intern_macro(id); + if let Some(eager) = &loc.eager { + if arg.is_some() { + return ExpandResult::str_err( + "hypothetical macro expansion not implemented for eager macro".to_owned(), + ); + } else { + return ExpandResult { + value: Some(eager.expansion.clone()), + // FIXME: There could be errors here! + err: None, + }; } - }; + } - let loc = db.lookup_intern_macro(lazy_id); let macro_arg = match arg.or_else(|| db.macro_arg(id)) { Some(it) => it, None => return ExpandResult::str_err("Fail to args in to tt::TokenTree".into()), @@ -374,7 +345,7 @@ fn macro_expand_with_arg( Some(it) => it, None => return ExpandResult::str_err("Fail to find macro definition".into()), }; - let ExpandResult { value: tt, err } = macro_rules.expand(db, lazy_id, ¯o_arg.0); + let ExpandResult { value: tt, err } = macro_rules.expand(db, id, ¯o_arg.0); // Set a hard limit for the expanded tt let count = tt.count(); if count > TOKEN_LIMIT { @@ -391,12 +362,7 @@ fn expand_proc_macro( db: &dyn AstDatabase, id: MacroCallId, ) -> Result { - let lazy_id = match id { - MacroCallId::LazyMacro(id) => id, - MacroCallId::EagerMacro(_) => unreachable!(), - }; - - let loc = db.lookup_intern_macro(lazy_id); + let loc: MacroCallLoc = db.lookup_intern_macro(id); let macro_arg = match db.macro_arg(id) { Some(it) => it, None => { @@ -436,14 +402,6 @@ fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc } fn macro_fragment_kind(db: &dyn AstDatabase, id: MacroCallId) -> FragmentKind { - match id { - MacroCallId::LazyMacro(id) => { - let loc: MacroCallLoc = db.lookup_intern_macro(id); - loc.kind.fragment_kind() - } - MacroCallId::EagerMacro(id) => { - let loc: EagerCallLoc = db.lookup_intern_eager_expansion(id); - loc.fragment - } - } + let loc: MacroCallLoc = db.lookup_intern_macro(id); + loc.kind.fragment_kind() } diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 85491fe8b..5e30ab930 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -22,7 +22,7 @@ use crate::{ ast::{self, AstNode}, db::AstDatabase, - EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind, + EagerCallInfo, InFile, MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, }; use base_db::CrateId; @@ -105,7 +105,7 @@ pub fn expand_eager_macro( def: MacroDefId, resolver: &dyn Fn(ast::Path) -> Option, mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), -) -> Result { +) -> Result { let parsed_args = diagnostic_sink.option_with( || Some(mbe::ast_to_token_tree(¯o_call.value.token_tree()?).0), || err("malformed macro invocation"), @@ -118,15 +118,14 @@ pub fn expand_eager_macro( // 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 // for that purpose. - let arg_id = db.intern_eager_expansion({ - EagerCallLoc { - def, - fragment: FragmentKind::Expr, - subtree: Arc::new(parsed_args.clone()), - krate, - call: call_id, + let arg_id = db.intern_macro(MacroCallLoc { + def, + krate, + eager: Some(EagerCallInfo { + expansion: Arc::new(parsed_args.clone()), included_file: None, - } + }), + kind: MacroCallKind::FnLike { ast_id: call_id, fragment: FragmentKind::Expr }, }); let arg_file_id: MacroCallId = arg_id.into(); @@ -146,16 +145,17 @@ pub fn expand_eager_macro( let res = eager.expand(db, arg_id, &subtree); let expanded = diagnostic_sink.expand_result_option(res)?; - let eager = EagerCallLoc { + let loc = MacroCallLoc { def, - fragment: expanded.fragment, - subtree: Arc::new(expanded.subtree), krate, - call: call_id, - included_file: expanded.included_file, + eager: Some(EagerCallInfo { + expansion: Arc::new(expanded.subtree), + included_file: expanded.included_file, + }), + kind: MacroCallKind::FnLike { ast_id: call_id, fragment: expanded.fragment }, }; - Ok(db.intern_eager_expansion(eager)) + Ok(db.intern_macro(loc)) } else { panic!("called `expand_eager_macro` on non-eager macro def {:?}", def); } diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs index aca69e35a..38e09fdd4 100644 --- a/crates/hir_expand/src/hygiene.rs +++ b/crates/hir_expand/src/hygiene.rs @@ -14,7 +14,7 @@ use syntax::{ast, AstNode, SyntaxNode, TextRange, TextSize}; use crate::{ db::{self, AstDatabase}, name::{AsName, Name}, - HirFileId, HirFileIdRepr, InFile, MacroCallId, MacroCallLoc, MacroDefKind, MacroFile, + HirFileId, HirFileIdRepr, InFile, MacroCallLoc, MacroDefKind, MacroFile, }; #[derive(Clone, Debug)] @@ -140,10 +140,7 @@ impl HygieneInfo { let (token_id, origin) = self.macro_def.map_id_up(token_id); let (token_map, tt) = match origin { mbe::Origin::Call => { - let call_id = match self.file.macro_call_id { - MacroCallId::LazyMacro(lazy) => lazy, - MacroCallId::EagerMacro(_) => unreachable!(), - }; + let call_id = self.file.macro_call_id; let loc: MacroCallLoc = db.lookup_intern_macro(call_id); let arg_start = loc.kind.arg(db)?.text_range().start(); (&self.macro_arg.1, InFile::new(loc.kind.file_id(), arg_start)) @@ -186,23 +183,20 @@ impl HygieneFrame { pub(crate) fn new(db: &dyn AstDatabase, file_id: HirFileId) -> HygieneFrame { let (info, krate, local_inner) = match file_id.0 { HirFileIdRepr::FileId(_) => (None, None, false), - HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id { - MacroCallId::EagerMacro(_id) => (None, None, false), - MacroCallId::LazyMacro(id) => { - let loc = db.lookup_intern_macro(id); - let info = make_hygiene_info(db, macro_file, &loc) - .map(|info| (loc.kind.file_id(), info)); - match loc.def.kind { - MacroDefKind::Declarative(_) => { - (info, Some(loc.def.krate), loc.def.local_inner) - } - MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false), - MacroDefKind::BuiltInDerive(..) => (info, None, false), - MacroDefKind::BuiltInEager(..) => (info, None, false), - MacroDefKind::ProcMacro(..) => (info, None, false), + HirFileIdRepr::MacroFile(macro_file) => { + let loc = db.lookup_intern_macro(macro_file.macro_call_id); + let info = + make_hygiene_info(db, macro_file, &loc).map(|info| (loc.kind.file_id(), info)); + match loc.def.kind { + MacroDefKind::Declarative(_) => { + (info, Some(loc.def.krate), loc.def.local_inner) } + MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false), + MacroDefKind::BuiltInDerive(..) => (info, None, false), + MacroDefKind::BuiltInEager(..) => (info, None, false), + MacroDefKind::ProcMacro(..) => (info, None, false), } - }, + } }; let (calling_file, info) = match info { diff --git a/crates/hir_expand/src/input.rs b/crates/hir_expand/src/input.rs index 112216859..fe4790e7b 100644 --- a/crates/hir_expand/src/input.rs +++ b/crates/hir_expand/src/input.rs @@ -8,13 +8,13 @@ use syntax::{ use crate::{ db::AstDatabase, name::{name, AsName}, - LazyMacroId, MacroCallKind, MacroCallLoc, + MacroCallId, MacroCallKind, MacroCallLoc, }; pub(crate) fn process_macro_input( db: &dyn AstDatabase, node: SyntaxNode, - id: LazyMacroId, + id: MacroCallId, ) -> SyntaxNode { let loc: MacroCallLoc = db.lookup_intern_macro(id); diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 88cb16ca4..edd5f9db2 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -80,19 +80,10 @@ impl HirFileId { match self.0 { HirFileIdRepr::FileId(file_id) => file_id, HirFileIdRepr::MacroFile(macro_file) => { - let file_id = match macro_file.macro_call_id { - MacroCallId::LazyMacro(id) => { - let loc = db.lookup_intern_macro(id); - loc.kind.file_id() - } - MacroCallId::EagerMacro(id) => { - let loc = db.lookup_intern_eager_expansion(id); - if let Some(included_file) = loc.included_file { - return included_file; - } else { - loc.call.file_id - } - } + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + let file_id = match &loc.eager { + Some(EagerCallInfo { included_file: Some(file), .. }) => (*file).into(), + _ => loc.kind.file_id(), }; file_id.original_file(db) } @@ -103,17 +94,10 @@ impl HirFileId { let mut level = 0; let mut curr = self; while let HirFileIdRepr::MacroFile(macro_file) = curr.0 { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + level += 1; - curr = match macro_file.macro_call_id { - MacroCallId::LazyMacro(id) => { - let loc = db.lookup_intern_macro(id); - loc.kind.file_id() - } - MacroCallId::EagerMacro(id) => { - let loc = db.lookup_intern_eager_expansion(id); - loc.call.file_id - } - }; + curr = loc.kind.file_id(); } level } @@ -122,16 +106,10 @@ impl HirFileId { pub fn call_node(self, db: &dyn db::AstDatabase) -> Option> { match self.0 { HirFileIdRepr::FileId(_) => None, - 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())) - } - }, + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + Some(loc.kind.node(db)) + } } } @@ -140,14 +118,7 @@ impl HirFileId { 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 expansion_info for eager macro - return None; - } - }; - let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); let arg_tt = loc.kind.arg(db)?; @@ -180,13 +151,7 @@ impl HirFileId { 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) => { - return None; - } - }; - let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id); + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); let item = match loc.def.kind { MacroDefKind::BuiltInDerive(..) => loc.kind.node(db), _ => return None, @@ -199,16 +164,12 @@ impl HirFileId { /// Return whether this file is an include macro pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool { match self.0 { - HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id { - MacroCallId::EagerMacro(id) => { - let loc = db.lookup_intern_eager_expansion(id); - return loc.included_file.is_some(); - } - _ => {} - }, - _ => {} + HirFileIdRepr::MacroFile(macro_file) => { + let loc: MacroCallLoc = db.lookup_intern_macro(macro_file.macro_call_id); + matches!(loc.eager, Some(EagerCallInfo { included_file: Some(_), .. })) + } + _ => false, } - false } } @@ -220,29 +181,8 @@ pub struct MacroFile { /// `MacroCallId` identifies a particular macro invocation, like /// `println!("Hello, {}", world)`. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum MacroCallId { - LazyMacro(LazyMacroId), - EagerMacro(EagerMacroId), -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct LazyMacroId(salsa::InternId); -impl_intern_key!(LazyMacroId); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct EagerMacroId(salsa::InternId); -impl_intern_key!(EagerMacroId); - -impl From for MacroCallId { - fn from(it: LazyMacroId) -> Self { - MacroCallId::LazyMacro(it) - } -} -impl From for MacroCallId { - fn from(it: EagerMacroId) -> Self { - MacroCallId::EagerMacro(it) - } -} +pub struct MacroCallId(salsa::InternId); +impl_intern_key!(MacroCallId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroDefId { @@ -258,8 +198,8 @@ impl MacroDefId { db: &dyn db::AstDatabase, krate: CrateId, kind: MacroCallKind, - ) -> LazyMacroId { - db.intern_macro(MacroCallLoc { def: self, krate, kind }) + ) -> MacroCallId { + db.intern_macro(MacroCallLoc { def: self, krate, eager: None, kind }) } pub fn ast_id(&self) -> Either, AstId> { @@ -288,10 +228,18 @@ pub enum MacroDefKind { ProcMacro(ProcMacroExpander, AstId), } +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +struct EagerCallInfo { + /// NOTE: This can be *either* the expansion result, *or* the argument to the eager macro! + expansion: Arc, + included_file: Option, +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MacroCallLoc { pub(crate) def: MacroDefId, pub(crate) krate: CrateId, + eager: Option, pub kind: MacroCallKind, } @@ -313,6 +261,7 @@ pub enum MacroCallKind { } impl MacroCallKind { + /// Returns the file containing the macro invocation. fn file_id(&self) -> HirFileId { match self { MacroCallKind::FnLike { ast_id, .. } => ast_id.file_id, @@ -354,17 +303,6 @@ impl MacroCallId { } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct EagerCallLoc { - pub(crate) def: MacroDefId, - pub(crate) fragment: FragmentKind, - pub(crate) subtree: Arc, - pub(crate) krate: CrateId, - pub(crate) call: AstId, - // The included file ID of the include macro. - pub(crate) included_file: Option, -} - /// ExpansionInfo mainly describes how to map text range between src and expanded macro #[derive(Debug, Clone, PartialEq, Eq)] pub struct ExpansionInfo { -- cgit v1.2.3