Unnamed repository; edit this file 'description' to name the repository.
22 files changed, 113 insertions, 123 deletions
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index d9be96174e..2ad0a9a8c8 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -118,11 +118,7 @@ fn include_macro_invoc( .modules .values() .flat_map(|m| m.scope.iter_macro_invoc()) - .filter_map(|invoc| { - db.lookup_intern_macro_call(*invoc.1) - .include_file_id(db, *invoc.1) - .map(|x| (*invoc.1, x)) - }) + .filter_map(|invoc| invoc.1.loc(db).include_file_id(db, *invoc.1).map(|x| (*invoc.1, x))) .collect() } diff --git a/crates/hir-def/src/expr_store/lower.rs b/crates/hir-def/src/expr_store/lower.rs index e683f06840..bd1dff8757 100644 --- a/crates/hir-def/src/expr_store/lower.rs +++ b/crates/hir-def/src/expr_store/lower.rs @@ -2951,7 +2951,7 @@ impl<'db> ExprCollector<'db> { None } else { hygiene_id.syntax_context().outer_expn(self.db).map(|expansion| { - let expansion = self.db.lookup_intern_macro_call(expansion.into()); + let expansion = hir_expand::MacroCallId::from(expansion).loc(self.db); (hygiene_id.syntax_context().parent(self.db), expansion.def) }) }; @@ -2981,7 +2981,7 @@ impl<'db> ExprCollector<'db> { hygiene_id = HygieneId::new(parent_ctx.opaque_and_semiopaque(self.db)); hygiene_info = parent_ctx.outer_expn(self.db).map(|expansion| { - let expansion = self.db.lookup_intern_macro_call(expansion.into()); + let expansion = hir_expand::MacroCallId::from(expansion).loc(self.db); (parent_ctx.parent(self.db), expansion.def) }); } diff --git a/crates/hir-def/src/expr_store/lower/path.rs b/crates/hir-def/src/expr_store/lower/path.rs index 579465e10f..236255c404 100644 --- a/crates/hir-def/src/expr_store/lower/path.rs +++ b/crates/hir-def/src/expr_store/lower/path.rs @@ -217,7 +217,7 @@ pub(super) fn lower_path( { let syn_ctxt = collector.expander.ctx_for_range(path.segment()?.syntax().text_range()); if let Some(macro_call_id) = syn_ctxt.outer_expn(collector.db) - && collector.db.lookup_intern_macro_call(macro_call_id.into()).def.local_inner + && hir_expand::MacroCallId::from(macro_call_id).loc(collector.db).def.local_inner { kind = match resolve_crate_root(collector.db, syn_ctxt) { Some(crate_root) => PathKind::DollarCrate(crate_root), diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index 1d9bf77b55..c98096769c 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -220,9 +220,9 @@ impl<N: AstIdNode> AstIdLoc for AssocItemLoc<N> { } macro_rules! impl_intern { - ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => { + ($id:ident, $loc:ident) => { impl_intern_key!($id, $loc); - impl_intern_lookup!(DefDatabase, $id, $loc, $intern, $lookup); + impl_intern_lookup!(DefDatabase, $id, $loc); }; } @@ -249,10 +249,10 @@ macro_rules! impl_loc { } type FunctionLoc = AssocItemLoc<ast::Fn>; -impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function); +impl_intern!(FunctionId, FunctionLoc); type StructLoc = ItemLoc<ast::Struct>; -impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct); +impl_intern!(StructId, StructLoc); impl StructId { pub fn fields(self, db: &dyn DefDatabase) -> &VariantFields { @@ -269,7 +269,7 @@ impl StructId { } pub type UnionLoc = ItemLoc<ast::Union>; -impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union); +impl_intern!(UnionId, UnionLoc); impl UnionId { pub fn fields(self, db: &dyn DefDatabase) -> &VariantFields { @@ -286,7 +286,7 @@ impl UnionId { } pub type EnumLoc = ItemLoc<ast::Enum>; -impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum); +impl_intern!(EnumId, EnumLoc); impl EnumId { #[inline] @@ -304,13 +304,13 @@ impl EnumId { } type ConstLoc = AssocItemLoc<ast::Const>; -impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const); +impl_intern!(ConstId, ConstLoc); pub type StaticLoc = AssocItemLoc<ast::Static>; -impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static); +impl_intern!(StaticId, StaticLoc); pub type TraitLoc = ItemLoc<ast::Trait>; -impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait); +impl_intern!(TraitId, TraitLoc); impl TraitId { #[inline] @@ -320,10 +320,10 @@ impl TraitId { } type TypeAliasLoc = AssocItemLoc<ast::TypeAlias>; -impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias); +impl_intern!(TypeAliasId, TypeAliasLoc); type ImplLoc = ItemLoc<ast::Impl>; -impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); +impl_intern!(ImplId, ImplLoc); impl ImplId { #[inline] @@ -353,13 +353,13 @@ pub struct BuiltinDeriveImplId { } type UseLoc = ItemLoc<ast::Use>; -impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use); +impl_intern!(UseId, UseLoc); type ExternCrateLoc = ItemLoc<ast::ExternCrate>; -impl_intern!(ExternCrateId, ExternCrateLoc, intern_extern_crate, lookup_intern_extern_crate); +impl_intern!(ExternCrateId, ExternCrateLoc); type ExternBlockLoc = ItemLoc<ast::ExternBlock>; -impl_intern!(ExternBlockId, ExternBlockLoc, intern_extern_block, lookup_intern_extern_block); +impl_intern!(ExternBlockId, ExternBlockLoc); impl ExternBlockId { pub fn abi(self, db: &dyn DefDatabase) -> ExternAbi { @@ -373,7 +373,7 @@ pub struct EnumVariantLoc { pub parent: EnumId, pub index: u32, } -impl_intern!(EnumVariantId, EnumVariantLoc, intern_enum_variant, lookup_intern_enum_variant); +impl_intern!(EnumVariantId, EnumVariantLoc); impl_loc!(EnumVariantLoc, id: Variant, parent: EnumId); impl EnumVariantId { @@ -398,7 +398,7 @@ pub struct Macro2Loc { pub allow_internal_unsafe: bool, pub edition: Edition, } -impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2); +impl_intern!(Macro2Id, Macro2Loc); impl_loc!(Macro2Loc, id: MacroDef, container: ModuleId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -409,7 +409,7 @@ pub struct MacroRulesLoc { pub flags: MacroRulesLocFlags, pub edition: Edition, } -impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules); +impl_intern!(MacroRulesId, MacroRulesLoc); impl_loc!(MacroRulesLoc, id: MacroRules, container: ModuleId); bitflags::bitflags! { @@ -437,7 +437,7 @@ pub struct ProcMacroLoc { pub kind: ProcMacroKind, pub edition: Edition, } -impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro); +impl_intern!(ProcMacroId, ProcMacroLoc); impl_loc!(ProcMacroLoc, id: Fn, container: ModuleId); #[derive(Debug, Hash, PartialEq, Eq, Clone)] @@ -446,7 +446,7 @@ pub struct BlockLoc { /// The containing module. pub module: ModuleId, } -impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block); +impl_intern!(BlockId, BlockLoc); #[salsa_macros::tracked(debug)] #[derive(PartialOrd, Ord)] diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs index 357da4e672..1d6812ad56 100644 --- a/crates/hir-def/src/macro_expansion_tests/mod.rs +++ b/crates/hir-def/src/macro_expansion_tests/mod.rs @@ -64,7 +64,7 @@ fn check_errors(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) .filter_map(|macro_call| { let errors = db.parse_macro_expansion_error(macro_call)?; let errors = errors.err.as_ref()?.render_to_string(&db); - let macro_loc = db.lookup_intern_macro_call(macro_call); + let macro_loc = macro_call.loc(&db); let ast_id = match macro_loc.kind { MacroCallKind::FnLike { ast_id, .. } => ast_id.map(|it| it.erase()), MacroCallKind::Derive { ast_id, .. } => ast_id.map(|it| it.erase()), diff --git a/crates/hir-def/src/nameres/assoc.rs b/crates/hir-def/src/nameres/assoc.rs index a1f1de90ef..b1d554738f 100644 --- a/crates/hir-def/src/nameres/assoc.rs +++ b/crates/hir-def/src/nameres/assoc.rs @@ -217,7 +217,7 @@ impl<'db> AssocItemCollector<'db> { attr_id, ) { Ok(ResolvedAttr::Macro(call_id)) => { - let loc = self.db.lookup_intern_macro_call(call_id); + let loc = call_id.loc(self.db); if let MacroDefKind::ProcMacro(_, exp, _) = loc.def.kind { // If there's no expander for the proc macro (e.g. the // proc macro is ignored, or building the proc macro diff --git a/crates/hir-def/src/resolver.rs b/crates/hir-def/src/resolver.rs index bb292ac1a6..8320221ffc 100644 --- a/crates/hir-def/src/resolver.rs +++ b/crates/hir-def/src/resolver.rs @@ -936,7 +936,7 @@ fn handle_macro_def_scope( // and use its parent expansion. *hygiene_id = HygieneId::new(parent_ctx.opaque_and_semiopaque(db)); *hygiene_info = parent_ctx.outer_expn(db).map(|expansion| { - let expansion = db.lookup_intern_macro_call(expansion.into()); + let expansion = hir_expand::MacroCallId::from(expansion).loc(db); (parent_ctx.parent(db), expansion.def) }); } @@ -950,7 +950,7 @@ fn hygiene_info( if !hygiene_id.is_root() { let ctx = hygiene_id.syntax_context(); ctx.outer_expn(db).map(|expansion| { - let expansion = db.lookup_intern_macro_call(expansion.into()); + let expansion = hir_expand::MacroCallId::from(expansion).loc(db); (ctx.parent(db), expansion.def) }) } else { diff --git a/crates/hir-expand/src/builtin/attr_macro.rs b/crates/hir-expand/src/builtin/attr_macro.rs index c94663ca0c..9b13f9fb00 100644 --- a/crates/hir-expand/src/builtin/attr_macro.rs +++ b/crates/hir-expand/src/builtin/attr_macro.rs @@ -122,7 +122,7 @@ fn derive_expand( tt: &tt::TopSubtree, span: Span, ) -> ExpandResult<tt::TopSubtree> { - let loc = db.lookup_intern_macro_call(id); + let loc = id.loc(db); let derives = match &loc.kind { MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => { attr_args diff --git a/crates/hir-expand/src/builtin/fn_macro.rs b/crates/hir-expand/src/builtin/fn_macro.rs index 9962677a9d..eb7175c686 100644 --- a/crates/hir-expand/src/builtin/fn_macro.rs +++ b/crates/hir-expand/src/builtin/fn_macro.rs @@ -357,7 +357,7 @@ fn cfg_select_expand( tt: &tt::TopSubtree, span: Span, ) -> ExpandResult<tt::TopSubtree> { - let loc = db.lookup_intern_macro_call(id); + let loc = id.loc(db); let cfg_options = loc.krate.cfg_options(db); let mut iter = tt.iter(); @@ -446,7 +446,7 @@ fn cfg_expand( tt: &tt::TopSubtree, span: Span, ) -> ExpandResult<tt::TopSubtree> { - let loc = db.lookup_intern_macro_call(id); + let loc = id.loc(db); let expr = CfgExpr::parse(tt); let enabled = loc.krate.cfg_options(db).check(&expr) != Some(false); let expanded = if enabled { quote!(span=>true) } else { quote!(span=>false) }; @@ -518,7 +518,7 @@ fn use_panic_2021(db: &dyn ExpandDatabase, span: Span) -> bool { let Some(expn) = span.ctx.outer_expn(db) else { break false; }; - let expn = db.lookup_intern_macro_call(expn.into()); + let expn = crate::MacroCallId::from(expn).loc(db); // FIXME: Record allow_internal_unstable in the macro def (not been done yet because it // would consume quite a bit extra memory for all call locs...) // if let Some(features) = expn.def.allow_internal_unstable { @@ -764,7 +764,7 @@ fn relative_file( allow_recursion: bool, err_span: Span, ) -> Result<EditionedFileId, ExpandError> { - let lookup = db.lookup_intern_macro_call(call_id); + let lookup = call_id.loc(db); let call_site = lookup.kind.file_id().original_file_respecting_includes(db).file_id(db); let path = AnchoredPath { anchor: call_site, path: path_str }; let res: FileId = db @@ -900,7 +900,7 @@ fn include_str_expand( } fn get_env_inner(db: &dyn ExpandDatabase, arg_id: MacroCallId, key: &Symbol) -> Option<String> { - let krate = db.lookup_intern_macro_call(arg_id).krate; + let krate = arg_id.loc(db).krate; krate.env(db).get(key.as_str()) } diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 4b26c1f0e5..beae6e843e 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -85,16 +85,6 @@ pub trait ExpandDatabase: SourceDatabase { #[salsa::transparent] fn real_span_map(&self, file_id: EditionedFileId) -> &RealSpanMap; - /// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the - /// reason why we use salsa at all. - /// - /// We encode macro definitions into ids of macro calls, this what allows us - /// to be incremental. - #[salsa::transparent] - fn intern_macro_call(&self, macro_call: MacroCallLoc) -> MacroCallId; - #[salsa::transparent] - fn lookup_intern_macro_call(&self, macro_call: MacroCallId) -> MacroCallLoc; - /// Lowers syntactic macro call to a token tree representation. That's a firewall /// query, only typing in the macro call itself changes the returned /// subtree. @@ -159,7 +149,7 @@ fn syntax_context(db: &dyn ExpandDatabase, file: HirFileId, edition: Edition) -> match file { HirFileId::FileId(_) => SyntaxContext::root(edition), HirFileId::MacroFile(m) => { - let kind = db.lookup_intern_macro_call(m).kind; + let kind = m.loc(db).kind; db.macro_arg_considering_derives(m, &kind).2.ctx } } @@ -182,7 +172,7 @@ pub fn expand_speculative( speculative_args: &SyntaxNode, token_to_map: SyntaxToken, ) -> Option<(SyntaxNode, Vec<(SyntaxToken, u8)>)> { - let loc = db.lookup_intern_macro_call(actual_macro_call); + let loc = actual_macro_call.loc(db); let (_, _, span) = *db.macro_arg_considering_derives(actual_macro_call, &loc.kind); let span_map = RealSpanMap::absolute(span.anchor.file_id); @@ -369,7 +359,7 @@ fn parse_macro_expansion( macro_file: MacroCallId, ) -> ExpandResult<(Parse<SyntaxNode>, ExpansionSpanMap)> { let _p = tracing::info_span!("parse_macro_expansion").entered(); - let loc = db.lookup_intern_macro_call(macro_file); + let loc = macro_file.loc(db); let expand_to = loc.expand_to(); let mbe::ValueResult { value: (tt, matched_arm), err } = macro_expand(db, macro_file, loc); @@ -423,7 +413,7 @@ fn macro_arg_considering_derives<'db>( #[salsa_macros::tracked(returns(ref))] fn macro_arg(db: &dyn ExpandDatabase, id: MacroCallId) -> MacroArgResult { - let loc = db.lookup_intern_macro_call(id); + let loc = id.loc(db); if let MacroCallLoc { def: MacroDefId { kind: MacroDefKind::BuiltInEager(..), .. }, @@ -642,7 +632,7 @@ fn proc_macro_span(db: &dyn ExpandDatabase, ast: AstId<ast::Fn>) -> Span { #[salsa_macros::tracked(returns(ref))] fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<tt::TopSubtree> { - let loc = db.lookup_intern_macro_call(id); + let loc = id.loc(db); let (macro_arg, undo_info, span) = db.macro_arg_considering_derives(id, &loc.kind); let (ast, expander) = match loc.def.kind { @@ -711,11 +701,3 @@ fn check_tt_count(tt: &tt::TopSubtree) -> Result<(), ExpandResult<()>> { }) } } - -fn intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallLoc) -> MacroCallId { - MacroCallId::new(db, macro_call) -} - -fn lookup_intern_macro_call(db: &dyn ExpandDatabase, macro_call: MacroCallId) -> MacroCallLoc { - macro_call.loc(db) -} diff --git a/crates/hir-expand/src/declarative.rs b/crates/hir-expand/src/declarative.rs index ead10a51a8..99db0dbcb9 100644 --- a/crates/hir-expand/src/declarative.rs +++ b/crates/hir-expand/src/declarative.rs @@ -36,7 +36,7 @@ impl DeclarativeMacroExpander { call_id: MacroCallId, span: Span, ) -> ExpandResult<(tt::TopSubtree, Option<u32>)> { - let loc = db.lookup_intern_macro_call(call_id); + let loc = call_id.loc(db); match self.mac.err() { Some(_) => ExpandResult::new( (tt::TopSubtree::empty(tt::DelimSpan { open: span, close: span }), None), @@ -120,8 +120,7 @@ impl DeclarativeMacroExpander { def_crate.data(db).edition } else { // UNWRAP-SAFETY: Only the root context has no outer expansion - let krate = - db.lookup_intern_macro_call(ctx.outer_expn(db).unwrap().into()).def.krate; + let krate = crate::MacroCallId::from(ctx.outer_expn(db).unwrap()).loc(db).def.krate; krate.data(db).edition } }; diff --git a/crates/hir-expand/src/eager.rs b/crates/hir-expand/src/eager.rs index dddef17ce7..a8868015ba 100644 --- a/crates/hir-expand/src/eager.rs +++ b/crates/hir-expand/src/eager.rs @@ -58,7 +58,7 @@ pub fn expand_eager_macro_input( kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None }, ctxt: call_site, }; - let arg_id = db.intern_macro_call(loc); + let arg_id = MacroCallId::new(db, loc); #[allow(deprecated)] // builtin eager macros are never derives let (_, _, span) = db.macro_arg(arg_id); let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } = @@ -113,7 +113,7 @@ pub fn expand_eager_macro_input( ctxt: call_site, }; - ExpandResult { value: Some(db.intern_macro_call(loc)), err } + ExpandResult { value: Some(MacroCallId::new(db, loc)), err } } fn lazy_expand<'db>( diff --git a/crates/hir-expand/src/files.rs b/crates/hir-expand/src/files.rs index d64090f913..a4c206156d 100644 --- a/crates/hir-expand/src/files.rs +++ b/crates/hir-expand/src/files.rs @@ -266,8 +266,10 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> { ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() { Some(parent) => Some(node.with_value(parent)), - None => db - .lookup_intern_macro_call(node.file_id.macro_file()?) + None => node + .file_id + .macro_file()? + .loc(db) .to_node_item(db) .syntax() .cloned() @@ -283,8 +285,10 @@ impl<SN: Borrow<SyntaxNode>> InFile<SN> { ) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ { let succ = move |node: &InFile<SyntaxNode>| match node.value.parent() { Some(parent) => Some(node.with_value(parent)), - None => db - .lookup_intern_macro_call(node.file_id.macro_file()?) + None => node + .file_id + .macro_file()? + .loc(db) .to_node_item(db) .syntax() .cloned() @@ -392,7 +396,7 @@ impl InFile<SyntaxToken> { } // Fall back to whole macro call. - let loc = db.lookup_intern_macro_call(mac_file); + let loc = mac_file.loc(db); loc.kind.original_call_range(db, loc.krate) } } @@ -438,7 +442,7 @@ impl InFile<TextRange> { match map_node_range_up(db, db.expansion_span_map(mac_file), self.value) { Some(it) => it, None => { - let loc = db.lookup_intern_macro_call(mac_file); + let loc = mac_file.loc(db); ( loc.kind.original_call_range(db, loc.krate), SyntaxContext::root(loc.def.edition), @@ -456,7 +460,7 @@ impl InFile<TextRange> { match map_node_range_up_rooted(db, db.expansion_span_map(mac_file), self.value) { Some(it) => it, _ => { - let loc = db.lookup_intern_macro_call(mac_file); + let loc = mac_file.loc(db); loc.kind.original_call_range(db, loc.krate) } } @@ -474,7 +478,7 @@ impl InFile<TextRange> { match map_node_range_up_rooted(db, db.expansion_span_map(mac_file), self.value) { Some(it) => it, _ => { - let loc = db.lookup_intern_macro_call(mac_file); + let loc = mac_file.loc(db); loc.kind.original_call_range_with_input(db) } } diff --git a/crates/hir-expand/src/hygiene.rs b/crates/hir-expand/src/hygiene.rs index ce7650d077..1cf8ce2a57 100644 --- a/crates/hir-expand/src/hygiene.rs +++ b/crates/hir-expand/src/hygiene.rs @@ -81,7 +81,7 @@ pub(super) fn apply_mark( return apply_mark_internal(db, ctxt, call_id, transparency, edition); } - let call_site_ctxt = db.lookup_intern_macro_call(call_id.into()).ctxt; + let call_site_ctxt = crate::MacroCallId::from(call_id).loc(db).ctxt; let mut call_site_ctxt = if transparency == Transparency::SemiOpaque { call_site_ctxt.normalize_to_macros_2_0(db) } else { diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 403e544bf8..0850d6156d 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -69,12 +69,12 @@ pub use tt; #[macro_export] macro_rules! impl_intern_lookup { - ($db:ident, $id:ident, $loc:ident, $intern:ident, $lookup:ident) => { + ($db:ident, $id:ident, $loc:ident) => { impl $crate::Intern for $loc { type Database = dyn $db; type ID = $id; fn intern(self, db: &Self::Database) -> Self::ID { - db.$intern(self) + $id::new(db, self) } } @@ -82,7 +82,7 @@ macro_rules! impl_intern_lookup { type Database = dyn $db; type Data = $loc; fn lookup(&self, db: &Self::Database) -> Self::Data { - db.$lookup(*self) + self.loc(db) } } }; @@ -101,13 +101,7 @@ pub trait Lookup { fn lookup(&self, db: &Self::Database) -> Self::Data; } -impl_intern_lookup!( - ExpandDatabase, - MacroCallId, - MacroCallLoc, - intern_macro_call, - lookup_intern_macro_call -); +impl_intern_lookup!(ExpandDatabase, MacroCallId, MacroCallLoc); pub type ExpandResult<T> = ValueResult<T, ExpandError>; @@ -386,7 +380,7 @@ impl HirFileId { pub fn edition(self, db: &dyn ExpandDatabase) -> Edition { match self { HirFileId::FileId(file_id) => file_id.edition(db), - HirFileId::MacroFile(m) => db.lookup_intern_macro_call(m).def.edition, + HirFileId::MacroFile(m) => m.loc(db).def.edition, } } pub fn original_file(self, db: &dyn ExpandDatabase) -> EditionedFileId { @@ -395,7 +389,7 @@ impl HirFileId { match file_id { HirFileId::FileId(id) => break id, HirFileId::MacroFile(macro_call_id) => { - file_id = db.lookup_intern_macro_call(macro_call_id).kind.file_id() + file_id = macro_call_id.loc(db).kind.file_id() } } } @@ -406,7 +400,7 @@ impl HirFileId { match self { HirFileId::FileId(id) => break id, HirFileId::MacroFile(file) => { - let loc = db.lookup_intern_macro_call(file); + let loc = file.loc(db); if loc.def.is_include() && let MacroCallKind::FnLike { eager: Some(eager), .. } = &loc.kind && let Ok(it) = include_input_to_file_id(db, file, &eager.arg) @@ -420,21 +414,21 @@ impl HirFileId { } pub fn original_call_node(self, db: &dyn ExpandDatabase) -> Option<InRealFile<SyntaxNode>> { - let mut call = db.lookup_intern_macro_call(self.macro_file()?).to_node(db); + let mut call = self.macro_file()?.loc(db).to_node(db); loop { match call.file_id { HirFileId::FileId(file_id) => { break Some(InRealFile { file_id, value: call.value }); } HirFileId::MacroFile(macro_call_id) => { - call = db.lookup_intern_macro_call(macro_call_id).to_node(db); + call = macro_call_id.loc(db).to_node(db); } } } } pub fn call_node(self, db: &dyn ExpandDatabase) -> Option<InFile<SyntaxNode>> { - Some(db.lookup_intern_macro_call(self.macro_file()?).to_node(db)) + Some(self.macro_file()?.loc(db).to_node(db)) } pub fn as_builtin_derive_attr_node( @@ -442,7 +436,7 @@ impl HirFileId { db: &dyn ExpandDatabase, ) -> Option<InFile<ast::Attr>> { let macro_file = self.macro_file()?; - let loc = db.lookup_intern_macro_call(macro_file); + let loc = macro_file.loc(db); let attr = match loc.def.kind { MacroDefKind::BuiltInDerive(..) => loc.to_node(db), _ => return None, @@ -471,13 +465,13 @@ pub enum MacroKind { impl MacroCallId { pub fn call_node(self, db: &dyn ExpandDatabase) -> InFile<SyntaxNode> { - db.lookup_intern_macro_call(self).to_node(db) + self.loc(db).to_node(db) } pub fn expansion_level(self, db: &dyn ExpandDatabase) -> u32 { let mut level = 0; let mut macro_file = self; loop { - let loc = db.lookup_intern_macro_call(macro_file); + let loc = macro_file.loc(db); level += 1; macro_file = match loc.kind.file_id() { @@ -487,7 +481,7 @@ impl MacroCallId { } } pub fn parent(self, db: &dyn ExpandDatabase) -> HirFileId { - db.lookup_intern_macro_call(self).kind.file_id() + self.loc(db).kind.file_id() } /// Return expansion information if it is a macro-expansion file @@ -496,7 +490,7 @@ impl MacroCallId { } pub fn kind(self, db: &dyn ExpandDatabase) -> MacroKind { - match db.lookup_intern_macro_call(self).def.kind { + match self.loc(db).def.kind { MacroDefKind::Declarative(..) => MacroKind::Declarative, MacroDefKind::BuiltIn(..) | MacroDefKind::BuiltInEager(..) => { MacroKind::DeclarativeBuiltIn @@ -510,24 +504,24 @@ impl MacroCallId { } pub fn is_include_macro(self, db: &dyn ExpandDatabase) -> bool { - db.lookup_intern_macro_call(self).def.is_include() + self.loc(db).def.is_include() } pub fn is_include_like_macro(self, db: &dyn ExpandDatabase) -> bool { - db.lookup_intern_macro_call(self).def.is_include_like() + self.loc(db).def.is_include_like() } pub fn is_env_or_option_env(self, db: &dyn ExpandDatabase) -> bool { - db.lookup_intern_macro_call(self).def.is_env_or_option_env() + self.loc(db).def.is_env_or_option_env() } pub fn is_eager(self, db: &dyn ExpandDatabase) -> bool { - let loc = db.lookup_intern_macro_call(self); + let loc = self.loc(db); matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) } pub fn eager_arg(self, db: &dyn ExpandDatabase) -> Option<MacroCallId> { - let loc = db.lookup_intern_macro_call(self); + let loc = self.loc(db); match &loc.kind { MacroCallKind::FnLike { eager, .. } => eager.as_ref().map(|it| it.arg_id), _ => None, @@ -535,7 +529,7 @@ impl MacroCallId { } pub fn is_derive_attr_pseudo_expansion(self, db: &dyn ExpandDatabase) -> bool { - let loc = db.lookup_intern_macro_call(self); + let loc = self.loc(db); loc.def.is_attribute_derive() } } @@ -548,7 +542,7 @@ impl MacroDefId { kind: MacroCallKind, ctxt: SyntaxContext, ) -> MacroCallId { - db.intern_macro_call(MacroCallLoc { def: self, krate, kind, ctxt }) + MacroCallId::new(db, MacroCallLoc { def: self, krate, kind, ctxt }) } pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile<TextRange> { @@ -725,7 +719,7 @@ impl MacroCallKind { let file_id = loop { match kind.file_id() { HirFileId::MacroFile(file) => { - kind = db.lookup_intern_macro_call(file).kind; + kind = file.loc(db).kind; } HirFileId::FileId(file_id) => break file_id, } @@ -750,7 +744,7 @@ impl MacroCallKind { let file_id = loop { match kind.file_id() { HirFileId::MacroFile(file) => { - kind = db.lookup_intern_macro_call(file).kind; + kind = file.loc(db).kind; } HirFileId::FileId(file_id) => break file_id, } @@ -920,7 +914,7 @@ impl<'db> ExpansionInfo<'db> { pub fn new(db: &'db dyn ExpandDatabase, macro_file: MacroCallId) -> ExpansionInfo<'db> { let _p = tracing::info_span!("ExpansionInfo::new").entered(); - let loc = db.lookup_intern_macro_call(macro_file); + let loc = macro_file.loc(db); let arg_tt = loc.kind.arg(db); let arg_map = db.span_map(arg_tt.file_id); @@ -1054,6 +1048,11 @@ impl ExpandTo { intern::impl_internable!(ModPath); +/// Macro ids. That's probably the tricksiest bit in rust-analyzer, and the +/// reason why we use salsa at all. +/// +/// We encode macro definitions into ids of macro calls, this what allows us +/// to be incremental. #[salsa_macros::interned(no_lifetime, debug, revisions = usize::MAX)] #[doc(alias = "MacroFileId")] pub struct MacroCallId { diff --git a/crates/hir-expand/src/mod_path.rs b/crates/hir-expand/src/mod_path.rs index 3e108f72be..9142ad8b92 100644 --- a/crates/hir-expand/src/mod_path.rs +++ b/crates/hir-expand/src/mod_path.rs @@ -332,7 +332,7 @@ fn convert_path( { let syn_ctx = span_for_range(segment.syntax().text_range()); if let Some(macro_call_id) = syn_ctx.outer_expn(db) - && db.lookup_intern_macro_call(macro_call_id.into()).def.local_inner + && crate::MacroCallId::from(macro_call_id).loc(db).def.local_inner { mod_path.kind = match resolve_crate_root(db, syn_ctx) { Some(crate_root) => PathKind::DollarCrate(crate_root), @@ -406,7 +406,7 @@ pub fn resolve_crate_root(db: &dyn ExpandDatabase, mut ctxt: SyntaxContext) -> O result_mark = Some(mark); } - result_mark.map(|call| db.lookup_intern_macro_call(call.into()).def.krate) + result_mark.map(|call| crate::MacroCallId::from(call).loc(db).def.krate) } pub use crate::name as __name; diff --git a/crates/hir-expand/src/prettify_macro_expansion_.rs b/crates/hir-expand/src/prettify_macro_expansion_.rs index 6431d46d39..79e6f0f5b7 100644 --- a/crates/hir-expand/src/prettify_macro_expansion_.rs +++ b/crates/hir-expand/src/prettify_macro_expansion_.rs @@ -29,7 +29,7 @@ pub fn prettify_macro_expansion( let macro_call_id = ctx .outer_expn(db) .expect("`$crate` cannot come from `SyntaxContextId::ROOT`"); - let macro_call = db.lookup_intern_macro_call(macro_call_id.into()); + let macro_call = crate::MacroCallId::from(macro_call_id).loc(db); let macro_def_crate = macro_call.def.krate; // First, if this is the same crate as the macro, nothing will work but `crate`. // If not, if the target trait has the macro's crate as a dependency, using the dependency name diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 3f83d068bb..549ea019f6 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1130,7 +1130,7 @@ fn macro_call_diagnostics<'db>( }; let ValueResult { value: parse_errors, err } = e; if let Some(err) = err { - let loc = db.lookup_intern_macro_call(macro_call_id); + let loc = macro_call_id.loc(db); let file_id = loc.kind.file_id(); let mut range = precise_macro_call_location(&loc.kind, db, loc.krate); let RenderedExpandError { message, error, kind } = err.render_to_string(db); @@ -1142,7 +1142,7 @@ fn macro_call_diagnostics<'db>( } if !parse_errors.is_empty() { - let loc = db.lookup_intern_macro_call(macro_call_id); + let loc = macro_call_id.loc(db); let range = precise_macro_call_location(&loc.kind, db, loc.krate); acc.push(MacroExpansionParseError { range, errors: parse_errors.clone() }.into()) } @@ -7455,5 +7455,16 @@ fn empty_param_env<'db>(krate: base_db::Crate) -> ParamEnvAndCrate<'db> { ParamEnvAndCrate { param_env: ParamEnv::empty(), krate } } +// FIXME: We probably don't want to expose this. +pub trait MacroCallIdExt { + fn loc(self, db: &dyn HirDatabase) -> hir_expand::MacroCallLoc; +} +impl MacroCallIdExt for span::MacroCallId { + #[inline] + fn loc(self, db: &dyn HirDatabase) -> hir_expand::MacroCallLoc { + hir_expand::MacroCallId::from(self).loc(db) + } +} + pub use hir_ty::next_solver; pub use hir_ty::setup_tracing; diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 889b0e05af..a1bbe47188 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -530,7 +530,7 @@ impl<'db> SemanticsImpl<'db> { } } HirFileId::MacroFile(macro_file) => { - let node = self.db.lookup_intern_macro_call(macro_file).to_node(self.db); + let node = macro_file.loc(self.db).to_node(self.db); let root = find_root(&node.value); self.cache(root, node.file_id); Some(node) @@ -582,7 +582,7 @@ impl<'db> SemanticsImpl<'db> { macro_call: &ast::MacroCall, ) -> Option<ExpandResult<SyntaxNode>> { let file_id = self.to_def(macro_call)?; - let macro_call = self.db.lookup_intern_macro_call(file_id); + let macro_call = file_id.loc(self.db); let skip = matches!( macro_call.def.kind, @@ -1322,7 +1322,7 @@ impl<'db> SemanticsImpl<'db> { }) .map(|(call_id, item)| { let item_range = item.syntax().text_range(); - let loc = db.lookup_intern_macro_call(call_id); + let loc = call_id.loc(db); let text_range = match loc.kind { hir_expand::MacroCallKind::Attr { censored_attr_ids: attr_ids, @@ -2590,7 +2590,7 @@ fn macro_call_to_macro_id( macro_call_id: MacroCallId, ) -> Option<MacroId> { let db: &dyn ExpandDatabase = ctx.db; - let loc = db.lookup_intern_macro_call(macro_call_id); + let loc = macro_call_id.loc(db); match loc.def.ast_id() { Either::Left(it) => { diff --git a/crates/ide-diagnostics/src/lib.rs b/crates/ide-diagnostics/src/lib.rs index 300b5e824d..ff2fd1a04a 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -102,7 +102,8 @@ mod tests; use std::sync::LazyLock; use hir::{ - Crate, DisplayTarget, InFile, Semantics, db::ExpandDatabase, diagnostics::AnyDiagnostic, + Crate, DisplayTarget, InFile, MacroCallIdExt, Semantics, db::ExpandDatabase, + diagnostics::AnyDiagnostic, }; use ide_db::{ FileId, FileRange, FxHashMap, FxHashSet, RootDatabase, Severity, SnippetCap, @@ -579,7 +580,7 @@ fn handle_diag_from_macros( let mut spans = span_map.spans_for_range(node.text_range()); if spans.any(|span| { span.ctx.outer_expn(sema.db).is_some_and(|expansion| { - let macro_call = sema.db.lookup_intern_macro_call(expansion.into()); + let macro_call = expansion.loc(sema.db); // We don't want to show diagnostics for non-local macros at all, but proc macros authors // seem to rely on being able to emit non-warning-free code, so we don't want to show warnings // for them even when the proc macro comes from the same workspace (in rustc that's not a diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs index 03af25a9c0..f70bb3353f 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -6,8 +6,7 @@ use arrayvec::ArrayVec; use either::Either; use hir::{ AssocItem, Crate, FieldSource, HasContainer, HasCrate, HasSource, HirDisplay, HirFileId, - InFile, LocalSource, ModuleSource, Name, Semantics, Symbol, db::ExpandDatabase, sym, - symbols::FileSymbol, + InFile, LocalSource, ModuleSource, Name, Semantics, Symbol, sym, symbols::FileSymbol, }; use ide_db::{ FileId, FileRange, RootDatabase, SymbolKind, @@ -939,10 +938,9 @@ pub(crate) fn orig_range_with_focus_r( ) -> UpmappingResult<(FileRange, Option<TextRange>)> { let Some(name) = focus_range else { return orig_range_r(db, hir_file, value) }; - let call = || db.lookup_intern_macro_call(hir_file.macro_file().unwrap()); + let call = || hir_file.macro_file().unwrap().loc(db); - let def_range = - || db.lookup_intern_macro_call(hir_file.macro_file().unwrap()).def.definition_range(db); + let def_range = || hir_file.macro_file().unwrap().loc(db).def.definition_range(db); // FIXME: Also make use of the syntax context to determine which site we are at? let value_range = InFile::new(hir_file, value).original_node_file_range_opt(db); diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs index 839df18159..801eaeaea9 100644 --- a/crates/load-cargo/src/lib.rs +++ b/crates/load-cargo/src/lib.rs @@ -657,7 +657,7 @@ impl ProcMacroExpander for Expander { let mut current_ctx = span.ctx; while let Some(macro_call_id) = current_ctx.outer_expn(db) { - let macro_call_loc = db.lookup_intern_macro_call(macro_call_id.into()); + let macro_call_loc = hir_expand::MacroCallId::from(macro_call_id).loc(db); let call_site_file = macro_call_loc.kind.file_id(); @@ -701,7 +701,7 @@ impl ProcMacroExpander for Expander { }; if let Some(macro_call_id) = span.ctx.outer_expn(db) { - let macro_call_loc = db.lookup_intern_macro_call(macro_call_id.into()); + let macro_call_loc = hir_expand::MacroCallId::from(macro_call_id).loc(db); let call_site_file = macro_call_loc.kind.file_id(); let call_site_ast_id = macro_call_loc.kind.erased_ast_id(); |