diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
commit | 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch) | |
tree | 3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /src/tools/rust-analyzer/crates/hir-expand | |
parent | Releasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip |
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/rust-analyzer/crates/hir-expand')
11 files changed, 169 insertions, 134 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs b/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs index 5c04f8e8b..8d1e88725 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs @@ -10,7 +10,7 @@ use smallvec::{smallvec, SmallVec}; use syntax::{ast, match_ast, AstNode, SmolStr, SyntaxNode}; use crate::{ - db::AstDatabase, + db::ExpandDatabase, hygiene::Hygiene, mod_path::{ModPath, PathKind}, name::AsName, @@ -38,7 +38,7 @@ impl ops::Deref for RawAttrs { impl RawAttrs { pub const EMPTY: Self = Self { entries: None }; - pub fn new(db: &dyn AstDatabase, owner: &dyn ast::HasAttrs, hygiene: &Hygiene) -> Self { + pub fn new(db: &dyn ExpandDatabase, owner: &dyn ast::HasAttrs, hygiene: &Hygiene) -> Self { let entries = collect_attrs(owner) .filter_map(|(id, attr)| match attr { Either::Left(attr) => { @@ -55,7 +55,7 @@ impl RawAttrs { Self { entries: if entries.is_empty() { None } else { Some(entries) } } } - pub fn from_attrs_owner(db: &dyn AstDatabase, owner: InFile<&dyn ast::HasAttrs>) -> Self { + pub fn from_attrs_owner(db: &dyn ExpandDatabase, owner: InFile<&dyn ast::HasAttrs>) -> Self { let hygiene = Hygiene::new(db, owner.file_id); Self::new(db, owner.value, &hygiene) } @@ -87,7 +87,7 @@ impl RawAttrs { /// Processes `cfg_attr`s, returning the resulting semantic `Attrs`. // FIXME: This should return a different type - pub fn filter(self, db: &dyn AstDatabase, krate: CrateId) -> RawAttrs { + pub fn filter(self, db: &dyn ExpandDatabase, krate: CrateId) -> RawAttrs { let has_cfg_attrs = self .iter() .any(|attr| attr.path.as_ident().map_or(false, |name| *name == crate::name![cfg_attr])); @@ -199,7 +199,7 @@ impl fmt::Display for AttrInput { impl Attr { fn from_src( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, ast: ast::Meta, hygiene: &Hygiene, id: AttrId, @@ -221,7 +221,7 @@ impl Attr { } fn from_tt( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, tt: &tt::Subtree, hygiene: &Hygiene, id: AttrId, diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_attr_macro.rs b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_attr_macro.rs index 906ca991d..277ecd939 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_attr_macro.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_attr_macro.rs @@ -1,6 +1,6 @@ //! Builtin attributes. -use crate::{db::AstDatabase, name, tt, ExpandResult, MacroCallId, MacroCallKind}; +use crate::{db::ExpandDatabase, name, tt, ExpandResult, MacroCallId, MacroCallKind}; macro_rules! register_builtin { ( $(($name:ident, $variant:ident) => $expand:ident),* ) => { @@ -12,7 +12,7 @@ macro_rules! register_builtin { impl BuiltinAttrExpander { pub fn expand( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -60,7 +60,7 @@ pub fn find_builtin_attr(ident: &name::Name) -> Option<BuiltinAttrExpander> { } fn dummy_attr_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -90,7 +90,7 @@ fn dummy_attr_expand( /// So this hacky approach is a lot more friendly for us, though it does require a bit of support in /// [`hir::Semantics`] to make this work. fn derive_attr_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_derive_macro.rs b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_derive_macro.rs index 060a68054..5c1a75132 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_derive_macro.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_derive_macro.rs @@ -9,7 +9,7 @@ use syntax::{ match_ast, }; -use crate::{db::AstDatabase, name, quote, ExpandError, ExpandResult, MacroCallId}; +use crate::{db::ExpandDatabase, name, quote, ExpandError, ExpandResult, MacroCallId}; macro_rules! register_builtin { ( $($trait:ident => $expand:ident),* ) => { @@ -21,7 +21,7 @@ macro_rules! register_builtin { impl BuiltinDeriveExpander { pub fn expand( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -141,7 +141,7 @@ fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResu ExpandResult::ok(expanded) } -fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree { +fn find_builtin_crate(db: &dyn ExpandDatabase, id: MacroCallId) -> tt::TokenTree { // FIXME: make hygiene works for builtin derive macro // such that $crate can be used here. let cg = db.crate_graph(); @@ -158,7 +158,7 @@ fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree { } fn copy_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -167,7 +167,7 @@ fn copy_expand( } fn clone_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -176,7 +176,7 @@ fn clone_expand( } fn default_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -185,7 +185,7 @@ fn default_expand( } fn debug_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -194,7 +194,7 @@ fn debug_expand( } fn hash_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -202,13 +202,17 @@ fn hash_expand( expand_simple_derive(tt, quote! { #krate::hash::Hash }) } -fn eq_expand(db: &dyn AstDatabase, id: MacroCallId, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> { +fn eq_expand( + db: &dyn ExpandDatabase, + id: MacroCallId, + tt: &tt::Subtree, +) -> ExpandResult<tt::Subtree> { let krate = find_builtin_crate(db, id); expand_simple_derive(tt, quote! { #krate::cmp::Eq }) } fn partial_eq_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -217,7 +221,7 @@ fn partial_eq_expand( } fn ord_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -226,7 +230,7 @@ fn ord_expand( } fn partial_ord_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_fn_macro.rs b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_fn_macro.rs index 9f3fa73d4..44510f2b7 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/builtin_fn_macro.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/builtin_fn_macro.rs @@ -10,7 +10,7 @@ use syntax::{ }; use crate::{ - db::AstDatabase, name, quote, tt, ExpandError, ExpandResult, MacroCallId, MacroCallLoc, + db::ExpandDatabase, name, quote, tt, ExpandError, ExpandResult, MacroCallId, MacroCallLoc, }; macro_rules! register_builtin { @@ -28,7 +28,7 @@ macro_rules! register_builtin { impl BuiltinFnLikeExpander { pub fn expand( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -42,7 +42,7 @@ macro_rules! register_builtin { impl EagerExpander { pub fn expand( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -121,7 +121,7 @@ const DOLLAR_CRATE: tt::Ident = tt::Ident { text: SmolStr::new_inline("$crate"), span: tt::TokenId::unspecified() }; fn module_path_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -130,7 +130,7 @@ fn module_path_expand( } fn line_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -144,7 +144,7 @@ fn line_expand( } fn log_syntax_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -152,7 +152,7 @@ fn log_syntax_expand( } fn trace_macros_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -160,7 +160,7 @@ fn trace_macros_expand( } fn stringify_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -174,7 +174,7 @@ fn stringify_expand( } fn column_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -188,7 +188,7 @@ fn column_expand( } fn assert_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -206,7 +206,7 @@ fn assert_expand( let cond = cond.clone(); let panic_args = itertools::Itertools::intersperse(panic_args.iter().cloned(), comma); quote! {{ - if !#cond { + if !(#cond) { #DOLLAR_CRATE::panic!(##panic_args); } }} @@ -218,7 +218,7 @@ fn assert_expand( } fn file_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -234,7 +234,7 @@ fn file_expand( } fn format_args_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -276,7 +276,7 @@ fn format_args_expand( } fn asm_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -304,7 +304,7 @@ fn asm_expand( } fn global_asm_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, _tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -313,7 +313,7 @@ fn global_asm_expand( } fn cfg_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -325,7 +325,7 @@ fn cfg_expand( } fn panic_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -343,7 +343,7 @@ fn panic_expand( } fn unreachable_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -379,7 +379,7 @@ fn unquote_byte_string(lit: &tt::Literal) -> Option<Vec<u8>> { } fn compile_error_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -395,7 +395,7 @@ fn compile_error_expand( } fn concat_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -441,7 +441,7 @@ fn concat_expand( } fn concat_bytes_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -507,7 +507,7 @@ fn concat_bytes_expand_subtree( } fn concat_idents_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -529,7 +529,7 @@ fn concat_idents_expand( } fn relative_file( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, call_id: MacroCallId, path_str: &str, allow_recursion: bool, @@ -558,7 +558,7 @@ fn parse_string(tt: &tt::Subtree) -> Result<String, ExpandError> { } fn include_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -583,7 +583,7 @@ fn include_expand( } fn include_bytes_expand( - _db: &dyn AstDatabase, + _db: &dyn ExpandDatabase, _arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -606,7 +606,7 @@ fn include_bytes_expand( } fn include_str_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -637,13 +637,13 @@ fn include_str_expand( ExpandResult::ok(ExpandedEager::new(quote!(#text))) } -fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> { +fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &str) -> Option<String> { let krate = db.lookup_intern_macro_call(arg_id).krate; db.crate_graph()[krate].env.get(key) } fn env_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { @@ -679,7 +679,7 @@ fn env_expand( } fn option_env_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, arg_id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<ExpandedEager> { diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/db.rs b/src/tools/rust-analyzer/crates/hir-expand/src/db.rs index 76016274f..45572499e 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/db.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/db.rs @@ -44,7 +44,7 @@ pub enum TokenExpander { impl TokenExpander { fn expand( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, tt: &tt::Subtree, ) -> ExpandResult<tt::Subtree> { @@ -83,9 +83,8 @@ impl TokenExpander { } } -// FIXME: rename to ExpandDatabase -#[salsa::query_group(AstDatabaseStorage)] -pub trait AstDatabase: SourceDatabase { +#[salsa::query_group(ExpandDatabaseStorage)] +pub trait ExpandDatabase: SourceDatabase { fn ast_id_map(&self, file_id: HirFileId) -> Arc<AstIdMap>; /// Main public API -- parses a hir file, not caring whether it's a real @@ -138,7 +137,7 @@ pub trait AstDatabase: SourceDatabase { /// token. The `token_to_map` mapped down into the expansion, with the mapped /// token returned. pub fn expand_speculative( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, actual_macro_call: MacroCallId, speculative_args: &SyntaxNode, token_to_map: SyntaxToken, @@ -211,7 +210,7 @@ pub fn expand_speculative( let mut speculative_expansion = match loc.def.kind { MacroDefKind::ProcMacro(expander, ..) => { tt.delimiter = tt::Delimiter::unspecified(); - expander.expand(db, loc.krate, &tt, attr_arg.as_ref()) + expander.expand(db, loc.def.krate, loc.krate, &tt, attr_arg.as_ref()) } MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => { pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?) @@ -236,12 +235,12 @@ pub fn expand_speculative( Some((node.syntax_node(), token)) } -fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> { +fn ast_id_map(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<AstIdMap> { let map = db.parse_or_expand(file_id).map(|it| AstIdMap::from_source(&it)).unwrap_or_default(); Arc::new(map) } -fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNode> { +fn parse_or_expand(db: &dyn ExpandDatabase, file_id: HirFileId) -> Option<SyntaxNode> { match file_id.repr() { HirFileIdRepr::FileId(file_id) => Some(db.parse(file_id).tree().syntax().clone()), HirFileIdRepr::MacroFile(macro_file) => { @@ -253,13 +252,13 @@ fn parse_or_expand(db: &dyn AstDatabase, file_id: HirFileId) -> Option<SyntaxNod } fn parse_macro_expansion( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, macro_file: MacroFile, ) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> { let _p = profile::span("parse_macro_expansion"); - let result = db.macro_expand(macro_file.macro_call_id); + let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id); - if let Some(err) = &result.err { + if let Some(err) = &err { // Note: // The final goal we would like to make all parse_macro success, // such that the following log will not call anyway. @@ -280,9 +279,9 @@ fn parse_macro_expansion( parents ); } - let tt = match result.value { + let tt = match value { Some(tt) => tt, - None => return ExpandResult { value: None, err: result.err }, + None => return ExpandResult { value: None, err }, }; let expand_to = macro_expand_to(db, macro_file.macro_call_id); @@ -292,11 +291,11 @@ fn parse_macro_expansion( let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to); - ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: result.err } + ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err } } fn macro_arg( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, id: MacroCallId, ) -> Option<Arc<(tt::Subtree, mbe::TokenMap, fixup::SyntaxFixupUndoInfo)>> { let arg = db.macro_arg_text(id)?; @@ -357,7 +356,7 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet<Sy .unwrap_or_default() } -fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> { +fn macro_arg_text(db: &dyn ExpandDatabase, id: MacroCallId) -> Option<GreenNode> { let loc = db.lookup_intern_macro_call(id); let arg = loc.kind.arg(db)?; if matches!(loc.kind, MacroCallKind::FnLike { .. }) { @@ -380,7 +379,10 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> { Some(arg.green().into()) } -fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Result<Arc<TokenExpander>, mbe::ParseError> { +fn macro_def( + db: &dyn ExpandDatabase, + id: MacroDefId, +) -> Result<Arc<TokenExpander>, mbe::ParseError> { match id.kind { MacroDefKind::Declarative(ast_id) => { let (mac, def_site_token_map) = match ast_id.to_node(db) { @@ -419,7 +421,10 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Result<Arc<TokenExpander>, } } -fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Arc<tt::Subtree>>> { +fn macro_expand( + db: &dyn ExpandDatabase, + id: MacroCallId, +) -> ExpandResult<Option<Arc<tt::Subtree>>> { let _p = profile::span("macro_expand"); let loc: MacroCallLoc = db.lookup_intern_macro_call(id); if let Some(eager) = &loc.eager { @@ -469,11 +474,11 @@ fn macro_expand(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<Option<Ar ExpandResult { value: Some(Arc::new(tt)), err } } -fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<ExpandError> { +fn macro_expand_error(db: &dyn ExpandDatabase, macro_call: MacroCallId) -> Option<ExpandError> { db.macro_expand(macro_call).err } -fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> { +fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> { let loc: MacroCallLoc = db.lookup_intern_macro_call(id); let macro_arg = match db.macro_arg(id) { Some(it) => it, @@ -499,14 +504,14 @@ fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt:: _ => None, }; - expander.expand(db, loc.krate, ¯o_arg.0, attr_arg.as_ref()) + expander.expand(db, loc.def.krate, loc.krate, ¯o_arg.0, attr_arg.as_ref()) } -fn hygiene_frame(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<HygieneFrame> { +fn hygiene_frame(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<HygieneFrame> { Arc::new(HygieneFrame::new(db, file_id)) } -fn macro_expand_to(db: &dyn AstDatabase, id: MacroCallId) -> ExpandTo { +fn macro_expand_to(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandTo { let loc: MacroCallLoc = db.lookup_intern_macro_call(id); loc.kind.expand_to() } diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/eager.rs b/src/tools/rust-analyzer/crates/hir-expand/src/eager.rs index dfab7ec92..aca41b11f 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/eager.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/eager.rs @@ -25,7 +25,7 @@ use syntax::{ted, SyntaxNode}; use crate::{ ast::{self, AstNode}, - db::AstDatabase, + db::ExpandDatabase, hygiene::Hygiene, mod_path::ModPath, EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind, @@ -96,7 +96,7 @@ impl ErrorSink for &'_ mut dyn FnMut(ExpandError) { } pub fn expand_eager_macro( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, krate: CrateId, macro_call: InFile<ast::MacroCall>, def: MacroDefId, @@ -172,7 +172,7 @@ fn to_subtree(node: &SyntaxNode) -> crate::tt::Subtree { } fn lazy_expand( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, def: &MacroDefId, macro_call: InFile<ast::MacroCall>, krate: CrateId, @@ -193,7 +193,7 @@ fn lazy_expand( } fn eager_macro_recur( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, hygiene: &Hygiene, curr: InFile<SyntaxNode>, krate: CrateId, diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs b/src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs index c811d1c66..b273f2176 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/fixup.rs @@ -636,9 +636,8 @@ fn foo() { if {} } "#, - // the {} gets parsed as the condition, I think? expect![[r#" -fn foo () {if {} {}} +fn foo () {if __ra_fixup {} {}} "#]], ) } diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/hygiene.rs b/src/tools/rust-analyzer/crates/hir-expand/src/hygiene.rs index 2300ee9d0..2eb56fc9e 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/hygiene.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/hygiene.rs @@ -14,7 +14,7 @@ use syntax::{ }; use crate::{ - db::{self, AstDatabase}, + db::{self, ExpandDatabase}, fixup, name::{AsName, Name}, HirFileId, InFile, MacroCallKind, MacroCallLoc, MacroDefKind, MacroFile, @@ -26,7 +26,7 @@ pub struct Hygiene { } impl Hygiene { - pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene { + pub fn new(db: &dyn ExpandDatabase, file_id: HirFileId) -> Hygiene { Hygiene { frames: Some(HygieneFrames::new(db, file_id)) } } @@ -37,7 +37,7 @@ impl Hygiene { // FIXME: this should just return name pub fn name_ref_to_name( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, name_ref: ast::NameRef, ) -> Either<Name, CrateId> { if let Some(frames) = &self.frames { @@ -51,7 +51,7 @@ impl Hygiene { Either::Left(name_ref.as_name()) } - pub fn local_inner_macros(&self, db: &dyn AstDatabase, path: ast::Path) -> Option<CrateId> { + pub fn local_inner_macros(&self, db: &dyn ExpandDatabase, path: ast::Path) -> Option<CrateId> { let mut token = path.syntax().first_token()?.text_range(); let frames = self.frames.as_ref()?; let mut current = &frames.0; @@ -87,13 +87,13 @@ pub struct HygieneFrame { } impl HygieneFrames { - fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Self { + fn new(db: &dyn ExpandDatabase, file_id: HirFileId) -> Self { // Note that this intentionally avoids the `hygiene_frame` query to avoid blowing up memory // usage. The query is only helpful for nested `HygieneFrame`s as it avoids redundant work. HygieneFrames(Arc::new(HygieneFrame::new(db, file_id))) } - fn root_crate(&self, db: &dyn AstDatabase, node: &SyntaxNode) -> Option<CrateId> { + fn root_crate(&self, db: &dyn ExpandDatabase, node: &SyntaxNode) -> Option<CrateId> { let mut token = node.first_token()?.text_range(); let mut result = self.0.krate; let mut current = self.0.clone(); @@ -136,7 +136,7 @@ struct HygieneInfo { impl HygieneInfo { fn map_ident_up( &self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, token: TextRange, ) -> Option<(InFile<TextRange>, Origin)> { let token_id = self.exp_map.token_by_range(token)?; @@ -175,7 +175,7 @@ impl HygieneInfo { } fn make_hygiene_info( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, macro_file: MacroFile, loc: &MacroCallLoc, ) -> Option<HygieneInfo> { @@ -215,7 +215,7 @@ fn make_hygiene_info( } impl HygieneFrame { - pub(crate) fn new(db: &dyn AstDatabase, file_id: HirFileId) -> HygieneFrame { + pub(crate) fn new(db: &dyn ExpandDatabase, file_id: HirFileId) -> HygieneFrame { let (info, krate, local_inner) = match file_id.macro_file() { None => (None, None, false), Some(macro_file) => { diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/lib.rs b/src/tools/rust-analyzer/crates/hir-expand/src/lib.rs index a52716cc0..5e99eacc1 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/lib.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/lib.rs @@ -198,7 +198,7 @@ impl HirFileId { /// For macro-expansion files, returns the file original source file the /// expansion originated from. - pub fn original_file(self, db: &dyn db::AstDatabase) -> FileId { + pub fn original_file(self, db: &dyn db::ExpandDatabase) -> FileId { let mut file_id = self; loop { match file_id.repr() { @@ -214,7 +214,7 @@ impl HirFileId { } } - pub fn expansion_level(self, db: &dyn db::AstDatabase) -> u32 { + pub fn expansion_level(self, db: &dyn db::ExpandDatabase) -> u32 { let mut level = 0; let mut curr = self; while let Some(macro_file) = curr.macro_file() { @@ -227,14 +227,14 @@ impl HirFileId { } /// If this is a macro call, returns the syntax node of the call. - pub fn call_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> { + pub fn call_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> { let macro_file = self.macro_file()?; let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); Some(loc.kind.to_node(db)) } /// If this is a macro call, returns the syntax node of the very first macro call this file resides in. - pub fn original_call_node(self, db: &dyn db::AstDatabase) -> Option<(FileId, SyntaxNode)> { + pub fn original_call_node(self, db: &dyn db::ExpandDatabase) -> Option<(FileId, SyntaxNode)> { let mut call = db.lookup_intern_macro_call(self.macro_file()?.macro_call_id).kind.to_node(db); loop { @@ -248,7 +248,7 @@ impl HirFileId { } /// Return expansion information if it is a macro-expansion file - pub fn expansion_info(self, db: &dyn db::AstDatabase) -> Option<ExpansionInfo> { + pub fn expansion_info(self, db: &dyn db::ExpandDatabase) -> Option<ExpansionInfo> { let macro_file = self.macro_file()?; let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -294,7 +294,7 @@ impl HirFileId { } /// Indicate it is macro file generated for builtin derive - pub fn is_builtin_derive(&self, db: &dyn db::AstDatabase) -> Option<InFile<ast::Attr>> { + pub fn is_builtin_derive(&self, db: &dyn db::ExpandDatabase) -> Option<InFile<ast::Attr>> { let macro_file = self.macro_file()?; let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); let attr = match loc.def.kind { @@ -304,7 +304,7 @@ impl HirFileId { Some(attr.with_value(ast::Attr::cast(attr.value.clone())?)) } - pub fn is_custom_derive(&self, db: &dyn db::AstDatabase) -> bool { + pub fn is_custom_derive(&self, db: &dyn db::ExpandDatabase) -> bool { match self.macro_file() { Some(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -315,7 +315,7 @@ impl HirFileId { } /// Return whether this file is an include macro - pub fn is_include_macro(&self, db: &dyn db::AstDatabase) -> bool { + pub fn is_include_macro(&self, db: &dyn db::ExpandDatabase) -> bool { match self.macro_file() { Some(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -326,7 +326,7 @@ impl HirFileId { } /// Return whether this file is an attr macro - pub fn is_attr_macro(&self, db: &dyn db::AstDatabase) -> bool { + pub fn is_attr_macro(&self, db: &dyn db::ExpandDatabase) -> bool { match self.macro_file() { Some(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -338,7 +338,7 @@ impl HirFileId { /// Return whether this file is the pseudo expansion of the derive attribute. /// See [`crate::builtin_attr_macro::derive_attr_expand`]. - pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::AstDatabase) -> bool { + pub fn is_derive_attr_pseudo_expansion(&self, db: &dyn db::ExpandDatabase) -> bool { match self.macro_file() { Some(macro_file) => { let loc: MacroCallLoc = db.lookup_intern_macro_call(macro_file.macro_call_id); @@ -384,7 +384,7 @@ impl HirFileId { impl MacroDefId { pub fn as_lazy_macro( self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, krate: CrateId, kind: MacroCallKind, ) -> MacroCallId { @@ -427,7 +427,7 @@ impl MacroCallKind { } } - pub fn to_node(&self, db: &dyn db::AstDatabase) -> InFile<SyntaxNode> { + pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> InFile<SyntaxNode> { match self { MacroCallKind::FnLike { ast_id, .. } => { ast_id.with_value(ast_id.to_node(db).syntax().clone()) @@ -465,7 +465,7 @@ impl MacroCallKind { /// Returns the original file range that best describes the location of this macro call. /// /// Unlike `MacroCallKind::original_call_range`, this also spans the item of attributes and derives. - pub fn original_call_range_with_body(self, db: &dyn db::AstDatabase) -> FileRange { + pub fn original_call_range_with_body(self, db: &dyn db::ExpandDatabase) -> FileRange { let mut kind = self; let file_id = loop { match kind.file_id().repr() { @@ -490,7 +490,7 @@ impl MacroCallKind { /// Here we try to roughly match what rustc does to improve diagnostics: fn-like macros /// get the whole `ast::MacroCall`, attribute macros get the attribute's range, and derives /// get only the specific derive that is being referred to. - pub fn original_call_range(self, db: &dyn db::AstDatabase) -> FileRange { + pub fn original_call_range(self, db: &dyn db::ExpandDatabase) -> FileRange { let mut kind = self; let file_id = loop { match kind.file_id().repr() { @@ -529,7 +529,7 @@ impl MacroCallKind { FileRange { range, file_id } } - fn arg(&self, db: &dyn db::AstDatabase) -> Option<SyntaxNode> { + fn arg(&self, db: &dyn db::ExpandDatabase) -> Option<SyntaxNode> { match self { MacroCallKind::FnLike { ast_id, .. } => { Some(ast_id.to_node(db).token_tree()?.syntax().clone()) @@ -597,7 +597,7 @@ impl ExpansionInfo { /// Both of these only have one simple call site input so no special handling is required here. pub fn map_token_down( &self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, item: Option<ast::Item>, token: InFile<&SyntaxToken>, ) -> Option<impl Iterator<Item = InFile<SyntaxToken>> + '_> { @@ -666,7 +666,7 @@ impl ExpansionInfo { /// Map a token up out of the expansion it resides in into the arguments of the macro call of the expansion. pub fn map_token_up( &self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, token: InFile<&SyntaxToken>, ) -> Option<(InFile<SyntaxToken>, Origin)> { // Fetch the id through its text range, @@ -717,7 +717,7 @@ impl ExpansionInfo { pub type AstId<N> = InFile<FileAstId<N>>; impl<N: AstNode> AstId<N> { - pub fn to_node(&self, db: &dyn db::AstDatabase) -> N { + pub fn to_node(&self, db: &dyn db::ExpandDatabase) -> N { let root = db.parse_or_expand(self.file_id).unwrap(); db.ast_id_map(self.file_id).get(self.value).to_node(&root) } @@ -753,7 +753,7 @@ impl<T> InFile<T> { self.with_value(&self.value) } - pub fn file_syntax(&self, db: &dyn db::AstDatabase) -> SyntaxNode { + pub fn file_syntax(&self, db: &dyn db::ExpandDatabase) -> SyntaxNode { db.parse_or_expand(self.file_id).expect("source created from invalid file") } } @@ -771,10 +771,19 @@ impl<T> InFile<Option<T>> { } } +impl<L, R> InFile<Either<L, R>> { + pub fn transpose(self) -> Either<InFile<L>, InFile<R>> { + match self.value { + Either::Left(l) => Either::Left(InFile::new(self.file_id, l)), + Either::Right(r) => Either::Right(InFile::new(self.file_id, r)), + } + } +} + impl<'a> InFile<&'a SyntaxNode> { pub fn ancestors_with_macros( self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, ) -> impl Iterator<Item = InFile<SyntaxNode>> + Clone + '_ { iter::successors(Some(self.cloned()), move |node| match node.value.parent() { Some(parent) => Some(node.with_value(parent)), @@ -785,7 +794,7 @@ impl<'a> InFile<&'a SyntaxNode> { /// Skips the attributed item that caused the macro invocation we are climbing up pub fn ancestors_with_macros_skip_attr_item( self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() { Some(parent) => Some(node.with_value(parent)), @@ -806,8 +815,8 @@ impl<'a> InFile<&'a SyntaxNode> { /// Falls back to the macro call range if the node cannot be mapped up fully. /// /// For attributes and derives, this will point back to the attribute only. - /// For the entire item `InFile::use original_file_range_full`. - pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange { + /// For the entire item use [`InFile::original_file_range_full`]. + pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange { match self.file_id.repr() { HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() }, HirFileIdRepr::MacroFile(mac_file) => { @@ -821,8 +830,23 @@ impl<'a> InFile<&'a SyntaxNode> { } } + /// Falls back to the macro call range if the node cannot be mapped up fully. + pub fn original_file_range_full(self, db: &dyn db::ExpandDatabase) -> FileRange { + match self.file_id.repr() { + HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() }, + HirFileIdRepr::MacroFile(mac_file) => { + if let Some(res) = self.original_file_range_opt(db) { + return res; + } + // Fall back to whole macro call. + let loc = db.lookup_intern_macro_call(mac_file.macro_call_id); + loc.kind.original_call_range_with_body(db) + } + } + } + /// Attempts to map the syntax node back up its macro calls. - pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> { + pub fn original_file_range_opt(self, db: &dyn db::ExpandDatabase) -> Option<FileRange> { match ascend_node_border_tokens(db, self) { Some(InFile { file_id, value: (first, last) }) => { let original_file = file_id.original_file(db); @@ -841,7 +865,7 @@ impl<'a> InFile<&'a SyntaxNode> { } } - pub fn original_syntax_node(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxNode>> { + pub fn original_syntax_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxNode>> { // This kind of upmapping can only be achieved in attribute expanded files, // as we don't have node inputs otherwise and therefore can't find an `N` node in the input if !self.file_id.is_macro() { @@ -868,13 +892,13 @@ impl<'a> InFile<&'a SyntaxNode> { } impl InFile<SyntaxToken> { - pub fn upmap(self, db: &dyn db::AstDatabase) -> Option<InFile<SyntaxToken>> { + pub fn upmap(self, db: &dyn db::ExpandDatabase) -> Option<InFile<SyntaxToken>> { let expansion = self.file_id.expansion_info(db)?; expansion.map_token_up(db, self.as_ref()).map(|(it, _)| it) } /// Falls back to the macro call range if the node cannot be mapped up fully. - pub fn original_file_range(self, db: &dyn db::AstDatabase) -> FileRange { + pub fn original_file_range(self, db: &dyn db::ExpandDatabase) -> FileRange { match self.file_id.repr() { HirFileIdRepr::FileId(file_id) => FileRange { file_id, range: self.value.text_range() }, HirFileIdRepr::MacroFile(mac_file) => { @@ -889,7 +913,7 @@ impl InFile<SyntaxToken> { } /// Attempts to map the syntax node back up its macro calls. - pub fn original_file_range_opt(self, db: &dyn db::AstDatabase) -> Option<FileRange> { + pub fn original_file_range_opt(self, db: &dyn db::ExpandDatabase) -> Option<FileRange> { match self.file_id.repr() { HirFileIdRepr::FileId(file_id) => { Some(FileRange { file_id, range: self.value.text_range() }) @@ -908,7 +932,7 @@ impl InFile<SyntaxToken> { pub fn ancestors_with_macros( self, - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { self.value.parent().into_iter().flat_map({ let file_id = self.file_id; @@ -918,7 +942,7 @@ impl InFile<SyntaxToken> { } fn ascend_node_border_tokens( - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, InFile { file_id, value: node }: InFile<&SyntaxNode>, ) -> Option<InFile<(SyntaxToken, SyntaxToken)>> { let expansion = file_id.expansion_info(db)?; @@ -934,7 +958,7 @@ fn ascend_node_border_tokens( } fn ascend_call_token( - db: &dyn db::AstDatabase, + db: &dyn db::ExpandDatabase, expansion: &ExpansionInfo, token: InFile<SyntaxToken>, ) -> Option<InFile<SyntaxToken>> { @@ -953,7 +977,7 @@ impl<N: AstNode> InFile<N> { self.value.syntax().descendants().filter_map(T::cast).map(move |n| self.with_value(n)) } - pub fn original_ast_node(self, db: &dyn db::AstDatabase) -> Option<InFile<N>> { + pub fn original_ast_node(self, db: &dyn db::ExpandDatabase) -> Option<InFile<N>> { // This kind of upmapping can only be achieved in attribute expanded files, // as we don't have node inputs otherwise and therefore can't find an `N` node in the input if !self.file_id.is_macro() { diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/mod_path.rs b/src/tools/rust-analyzer/crates/hir-expand/src/mod_path.rs index d7586d129..e9393cc89 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/mod_path.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/mod_path.rs @@ -6,7 +6,7 @@ use std::{ }; use crate::{ - db::AstDatabase, + db::ExpandDatabase, hygiene::Hygiene, name::{known, Name}, }; @@ -37,7 +37,11 @@ pub enum PathKind { } impl ModPath { - pub fn from_src(db: &dyn AstDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> { + pub fn from_src( + db: &dyn ExpandDatabase, + path: ast::Path, + hygiene: &Hygiene, + ) -> Option<ModPath> { convert_path(db, None, path, hygiene) } @@ -162,7 +166,7 @@ impl From<Name> for ModPath { } fn convert_path( - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene, diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs b/src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs index 3f4d2540c..d758e9302 100644 --- a/src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs +++ b/src/tools/rust-analyzer/crates/hir-expand/src/proc_macro.rs @@ -3,22 +3,20 @@ use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind}; use stdx::never; -use crate::{db::AstDatabase, tt, ExpandError, ExpandResult}; +use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult}; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub struct ProcMacroExpander { - krate: CrateId, proc_macro_id: Option<ProcMacroId>, } impl ProcMacroExpander { - pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self { - Self { krate, proc_macro_id: Some(proc_macro_id) } + pub fn new(proc_macro_id: ProcMacroId) -> Self { + Self { proc_macro_id: Some(proc_macro_id) } } - pub fn dummy(krate: CrateId) -> Self { - // FIXME: Should store the name for better errors - Self { krate, proc_macro_id: None } + pub fn dummy() -> Self { + Self { proc_macro_id: None } } pub fn is_dummy(&self) -> bool { @@ -27,7 +25,8 @@ impl ProcMacroExpander { pub fn expand( self, - db: &dyn AstDatabase, + db: &dyn ExpandDatabase, + def_crate: CrateId, calling_crate: CrateId, tt: &tt::Subtree, attr_arg: Option<&tt::Subtree>, @@ -35,7 +34,7 @@ impl ProcMacroExpander { match self.proc_macro_id { Some(id) => { let krate_graph = db.crate_graph(); - let proc_macros = match &krate_graph[self.krate].proc_macro { + let proc_macros = match &krate_graph[def_crate].proc_macro { Ok(proc_macros) => proc_macros, Err(_) => { never!("Non-dummy expander even though there are no proc macros"); @@ -84,7 +83,7 @@ impl ProcMacroExpander { } None => ExpandResult::with_err( tt::Subtree::empty(), - ExpandError::UnresolvedProcMacro(self.krate), + ExpandError::UnresolvedProcMacro(def_crate), ), } } |