Unnamed repository; edit this file 'description' to name the repository.
32 files changed, 129 insertions, 387 deletions
diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index d9be96174e..11e5c54246 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -7,77 +7,16 @@ use hir_expand::{ use triomphe::Arc; use crate::{ - AssocItemId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, EnumId, EnumLoc, EnumVariantId, - EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId, - FunctionLoc, ImplId, ImplLoc, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId, - MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId, - StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, + AssocItemId, AttrDefId, Macro2Loc, MacroExpander, MacroId, MacroRulesLoc, MacroRulesLocFlags, + TraitId, attrs::AttrFlags, item_tree::{ItemTree, file_item_tree}, nameres::crate_def_map, visibility::{self, Visibility}, }; -use salsa::plumbing::AsId; - -#[query_group::query_group(InternDatabaseStorage)] -pub trait InternDatabase: SourceDatabase { - // region: items - #[salsa::interned] - fn intern_use(&self, loc: UseLoc) -> UseId; - - #[salsa::interned] - fn intern_extern_crate(&self, loc: ExternCrateLoc) -> ExternCrateId; - - #[salsa::interned] - fn intern_function(&self, loc: FunctionLoc) -> FunctionId; - - #[salsa::interned] - fn intern_struct(&self, loc: StructLoc) -> StructId; - - #[salsa::interned] - fn intern_union(&self, loc: UnionLoc) -> UnionId; - - #[salsa::interned] - fn intern_enum(&self, loc: EnumLoc) -> EnumId; - - #[salsa::interned] - fn intern_enum_variant(&self, loc: EnumVariantLoc) -> EnumVariantId; - - #[salsa::interned] - fn intern_const(&self, loc: ConstLoc) -> ConstId; - - #[salsa::interned] - fn intern_static(&self, loc: StaticLoc) -> StaticId; - - #[salsa::interned] - fn intern_trait(&self, loc: TraitLoc) -> TraitId; - - #[salsa::interned] - fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId; - - #[salsa::interned] - fn intern_impl(&self, loc: ImplLoc) -> ImplId; - - #[salsa::interned] - fn intern_extern_block(&self, loc: ExternBlockLoc) -> ExternBlockId; - - #[salsa::interned] - fn intern_macro2(&self, loc: Macro2Loc) -> Macro2Id; - - #[salsa::interned] - fn intern_proc_macro(&self, loc: ProcMacroLoc) -> ProcMacroId; - - #[salsa::interned] - fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId; - // endregion: items - - #[salsa::interned] - fn intern_block(&self, loc: BlockLoc) -> BlockId; -} - #[query_group::query_group] -pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase { +pub trait DefDatabase: ExpandDatabase + SourceDatabase { /// Whether to expand procedural macros during name resolution. #[salsa::input] fn expand_proc_attr_macros(&self) -> bool; @@ -118,11 +57,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 6060a5a234..8818096500 100644 --- a/crates/hir-def/src/expr_store/lower.rs +++ b/crates/hir-def/src/expr_store/lower.rs @@ -2501,7 +2501,7 @@ impl<'db> ExprCollector<'db> { ) -> ExprId { let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| { let ast_id = self.expander.in_file(file_local_id); - self.db.intern_block(BlockLoc { ast_id, module: self.module }) + BlockId::new(self.db, BlockLoc { ast_id, module: self.module }) }); let (module, def_map) = @@ -2953,7 +2953,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) }) }; @@ -2983,7 +2983,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 01a1244629..a19f58709b 100644 --- a/crates/hir-expand/src/eager.rs +++ b/crates/hir-expand/src/eager.rs @@ -60,7 +60,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 } = @@ -115,7 +115,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-ty/src/db.rs b/crates/hir-ty/src/db.rs index 352f717454..c24a5b943d 100644 --- a/crates/hir-ty/src/db.rs +++ b/crates/hir-ty/src/db.rs @@ -17,7 +17,6 @@ use hir_def::{ signatures::{ConstSignature, StaticSignature}, }; use la_arena::ArenaMap; -use salsa::plumbing::AsId; use span::Edition; use stdx::impl_from; use triomphe::Arc; @@ -285,10 +284,6 @@ pub trait HirDatabase: DefDatabase + std::fmt::Debug { type_alias: TypeAliasId, ) -> EarlyBinder<'db, &'db [Clause<'db>]>; - // Interned IDs for solver integration - #[salsa::interned] - fn intern_impl_trait_id(&self, id: ImplTraitId) -> InternedOpaqueTyId; - #[salsa::invoke(crate::variance::variances_of)] #[salsa::transparent] fn variances_of<'db>(&'db self, def: GenericDefId) -> VariancesOf<'db>; diff --git a/crates/hir-ty/src/display.rs b/crates/hir-ty/src/display.rs index 0728509432..bc726b652f 100644 --- a/crates/hir-ty/src/display.rs +++ b/crates/hir-ty/src/display.rs @@ -1494,7 +1494,7 @@ impl<'db> HirDisplay<'db> for Ty<'db> { DisplaySourceCodeError::OpaqueType, )); } - let impl_trait_id = db.lookup_intern_impl_trait_id(opaque_ty_id); + let impl_trait_id = opaque_ty_id.loc(db); let data = impl_trait_id.predicates(db); let bounds = data .iter_instantiated_copied(interner, alias_ty.args.as_slice()) diff --git a/crates/hir-ty/src/drop.rs b/crates/hir-ty/src/drop.rs index d4a948c7b2..61e6720a29 100644 --- a/crates/hir-ty/src/drop.rs +++ b/crates/hir-ty/src/drop.rs @@ -22,9 +22,9 @@ use crate::{ #[salsa::tracked] pub fn destructor(db: &dyn HirDatabase, adt: AdtId) -> Option<ImplId> { let module = match adt { - AdtId::EnumId(id) => db.lookup_intern_enum(id).container, - AdtId::StructId(id) => db.lookup_intern_struct(id).container, - AdtId::UnionId(id) => db.lookup_intern_union(id).container, + AdtId::EnumId(id) => id.loc(db).container, + AdtId::StructId(id) => id.loc(db).container, + AdtId::UnionId(id) => id.loc(db).container, }; let interner = DbInterner::new_with(db, module.krate(db)); let drop_trait = interner.lang_items().Drop?; diff --git a/crates/hir-ty/src/dyn_compatibility.rs b/crates/hir-ty/src/dyn_compatibility.rs index 5ffb27af26..34858212cb 100644 --- a/crates/hir-ty/src/dyn_compatibility.rs +++ b/crates/hir-ty/src/dyn_compatibility.rs @@ -525,7 +525,7 @@ fn contains_illegal_impl_trait_in_trait<'db>( // Since we haven't implemented RPITIT in proper way like rustc yet, // just check whether `ret` contains RPIT for now for opaque_ty in visitor.0 { - let impl_trait_id = db.lookup_intern_impl_trait_id(opaque_ty); + let impl_trait_id = opaque_ty.loc(db); if matches!(impl_trait_id, ImplTraitId::ReturnTypeImplTrait(..)) { return Some(MethodViolationCode::ReferencesImplTraitInTrait); } diff --git a/crates/hir-ty/src/lower.rs b/crates/hir-ty/src/lower.rs index 61329f6c88..67cc34a404 100644 --- a/crates/hir-ty/src/lower.rs +++ b/crates/hir-ty/src/lower.rs @@ -496,7 +496,7 @@ impl<'db, 'a> TyLoweringContext<'db, 'a> { |f| ImplTraitId::ReturnTypeImplTrait(f, idx), |a| ImplTraitId::TypeAliasImplTrait(a, idx), ); - let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id); + let opaque_ty_id = InternedOpaqueTyId::new(self.db, impl_trait_id); // We don't want to lower the bounds inside the binders // we're currently in, because they don't end up inside diff --git a/crates/hir-ty/src/next_solver/generics.rs b/crates/hir-ty/src/next_solver/generics.rs index 2f8c7dc160..a798582cb9 100644 --- a/crates/hir-ty/src/next_solver/generics.rs +++ b/crates/hir-ty/src/next_solver/generics.rs @@ -12,7 +12,7 @@ pub(crate) fn generics(interner: DbInterner<'_>, def: SolverDefId) -> Generics<' let db = interner.db; let def = match (def.try_into(), def) { (Ok(def), _) => def, - (_, SolverDefId::InternedOpaqueTyId(id)) => match db.lookup_intern_impl_trait_id(id) { + (_, SolverDefId::InternedOpaqueTyId(id)) => match id.loc(db) { crate::ImplTraitId::ReturnTypeImplTrait(function_id, _) => function_id.into(), crate::ImplTraitId::TypeAliasImplTrait(type_alias_id, _) => type_alias_id.into(), }, diff --git a/crates/hir-ty/src/next_solver/interner.rs b/crates/hir-ty/src/next_solver/interner.rs index d840024277..46c03e5ac1 100644 --- a/crates/hir-ty/src/next_solver/interner.rs +++ b/crates/hir-ty/src/next_solver/interner.rs @@ -2043,7 +2043,7 @@ impl<'db> Interner for DbInterner<'db> { self, opaque: Self::LocalOpaqueTyId, ) -> EarlyBinder<Self, Self::Ty> { - let impl_trait_id = self.db().lookup_intern_impl_trait_id(opaque.0); + let impl_trait_id = opaque.0.loc(self.db); match impl_trait_id { crate::ImplTraitId::ReturnTypeImplTrait(func, idx) => { crate::opaques::rpit_hidden_types(self.db, func)[idx].get() diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 0423a8fc4f..63b834a8d1 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 aa8dc02de6..49b3234a11 100644 --- a/crates/ide-diagnostics/src/lib.rs +++ b/crates/ide-diagnostics/src/lib.rs @@ -103,7 +103,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, @@ -581,7 +582,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(); diff --git a/crates/query-group-macro/src/lib.rs b/crates/query-group-macro/src/lib.rs index 277cc0b269..6e89e0875e 100644 --- a/crates/query-group-macro/src/lib.rs +++ b/crates/query-group-macro/src/lib.rs @@ -6,18 +6,15 @@ use std::vec; use proc_macro::TokenStream; use proc_macro2::Span; use queries::{ - GeneratedInputStruct, InputQuery, InputSetter, InputSetterWithDurability, Intern, Lookup, - Queries, SetterKind, TrackedQuery, Transparent, + GeneratedInputStruct, InputQuery, InputSetter, InputSetterWithDurability, Queries, SetterKind, + TrackedQuery, Transparent, }; use quote::{ToTokens, format_ident, quote}; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::visit_mut::VisitMut; -use syn::{ - Attribute, FnArg, ItemTrait, Path, Token, TraitItem, TraitItemFn, parse_quote, - parse_quote_spanned, -}; +use syn::{Attribute, FnArg, ItemTrait, Path, Token, TraitItem, parse_quote, parse_quote_spanned}; mod queries; @@ -106,7 +103,6 @@ enum QueryKind { Tracked, TrackedWithSalsaStruct, Transparent, - Interned, } #[derive(Default, Debug, Clone)] @@ -190,7 +186,6 @@ pub(crate) fn query_group_impl( let mut trait_methods = vec![]; let mut setter_trait_methods = vec![]; let mut lookup_signatures = vec![]; - let mut lookup_methods = vec![]; for item in &mut item_trait.items { if let syn::TraitItem::Fn(method) = item { @@ -202,7 +197,6 @@ pub(crate) fn query_group_impl( let mut query_kind = QueryKind::TrackedWithSalsaStruct; let mut invoke = None; let mut cycle = None; - let mut interned_struct_path = None; let mut lru = None; let params: Vec<FnArg> = signature.inputs.clone().into_iter().collect(); @@ -230,22 +224,6 @@ pub(crate) fn query_group_impl( } query_kind = QueryKind::Input; } - "interned" => { - let syn::ReturnType::Type(_, ty) = &signature.output else { - return Err(syn::Error::new( - span, - "interned queries must have return type", - )); - }; - let syn::Type::Path(path) = &**ty else { - return Err(syn::Error::new( - span, - "interned queries must have return type", - )); - }; - interned_struct_path = Some(path.path.clone()); - query_kind = QueryKind::Interned; - } "invoke_interned" => { let path = syn::parse::<Parenthesized<Path>>(tts)?; invoke = Some(path.0.clone()); @@ -317,28 +295,6 @@ pub(crate) fn query_group_impl( }; setter_trait_methods.push(SetterKind::WithDurability(setter)); } - (QueryKind::Interned, None) => { - let interned_struct_path = interned_struct_path.unwrap(); - let method = Intern { - signature: signature.clone(), - pat_and_tys: pat_and_tys.clone(), - interned_struct_path: interned_struct_path.clone(), - }; - - trait_methods.push(Queries::Intern(method)); - - let mut method = Lookup { - signature: signature.clone(), - pat_and_tys: pat_and_tys.clone(), - return_ty: *return_ty, - interned_struct_path, - }; - method.prepare_signature(); - - lookup_signatures - .push(TraitItem::Fn(make_trait_method(method.signature.clone()))); - lookup_methods.push(method); - } // tracked function. it might have an invoke, or might not. (QueryKind::Tracked, invoke) => { let method = TrackedQuery { @@ -380,13 +336,6 @@ pub(crate) fn query_group_impl( }; trait_methods.push(Queries::Transparent(method)); } - // error/invalid constructions - (QueryKind::Interned, Some(path)) => { - return Err(syn::Error::new( - path.span(), - "Interned queries cannot be used with an `#[invoke]`".to_string(), - )); - } (QueryKind::Input, Some(path)) => { return Err(syn::Error::new( path.span(), @@ -451,8 +400,6 @@ pub(crate) fn query_group_impl( #(#trait_methods)* #(#setter_methods)* - - #(#lookup_methods)* } }; RemoveAttrsFromTraitMethods.visit_item_trait_mut(&mut item_trait); @@ -485,15 +432,6 @@ where } } -fn make_trait_method(sig: syn::Signature) -> TraitItemFn { - TraitItemFn { - attrs: vec![], - sig: sig.clone(), - semi_token: Some(syn::Token)), - default: None, - } -} - struct RemoveAttrsFromTraitMethods; impl VisitMut for RemoveAttrsFromTraitMethods { diff --git a/crates/query-group-macro/src/queries.rs b/crates/query-group-macro/src/queries.rs index 83ce8902d0..4221068828 100644 --- a/crates/query-group-macro/src/queries.rs +++ b/crates/query-group-macro/src/queries.rs @@ -1,7 +1,7 @@ //! The IR of the `#[query_group]` macro. use quote::{ToTokens, format_ident, quote, quote_spanned}; -use syn::{FnArg, Ident, PatType, Path, Receiver, ReturnType, Type, parse_quote, spanned::Spanned}; +use syn::{FnArg, Ident, PatType, Path, Receiver, ReturnType, parse_quote, spanned::Spanned}; use crate::Cycle; @@ -266,80 +266,11 @@ impl ToTokens for Transparent { method.to_tokens(tokens); } } -pub(crate) struct Intern { - pub(crate) signature: syn::Signature, - pub(crate) pat_and_tys: Vec<PatType>, - pub(crate) interned_struct_path: Path, -} - -impl ToTokens for Intern { - fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { - let sig = &self.signature; - - let ty = self.pat_and_tys.to_vec(); - - let interned_pat = ty.first().expect("at least one pat; this is a bug"); - let interned_pat = &interned_pat.pat; - - let wrapper_struct = self.interned_struct_path.to_token_stream(); - - let method = quote! { - #sig { - #wrapper_struct::new(self, #interned_pat) - } - }; - - method.to_tokens(tokens); - } -} - -pub(crate) struct Lookup { - pub(crate) signature: syn::Signature, - pub(crate) pat_and_tys: Vec<PatType>, - pub(crate) return_ty: Type, - pub(crate) interned_struct_path: Path, -} - -impl Lookup { - pub(crate) fn prepare_signature(&mut self) { - let sig = &self.signature; - - let ident = format_ident!("lookup_{}", sig.ident); - - let ty = self.pat_and_tys.to_vec(); - - let interned_key = &self.return_ty; - - let interned_pat = ty.first().expect("at least one pat; this is a bug"); - let interned_return_ty = &interned_pat.ty; - - self.signature = parse_quote!( - fn #ident(&self, id: #interned_key) -> #interned_return_ty - ); - } -} - -impl ToTokens for Lookup { - fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { - let sig = &self.signature; - - let wrapper_struct = self.interned_struct_path.to_token_stream(); - let method = quote! { - #sig { - let zalsa = self.zalsa(); - #wrapper_struct::ingredient(zalsa).data(zalsa, id.as_id()).0.clone() - } - }; - - method.to_tokens(tokens); - } -} #[allow(clippy::large_enum_variant)] pub(crate) enum Queries { TrackedQuery(TrackedQuery), InputQuery(InputQuery), - Intern(Intern), Transparent(Transparent), } @@ -349,7 +280,6 @@ impl ToTokens for Queries { Queries::TrackedQuery(tracked_query) => tracked_query.to_tokens(tokens), Queries::InputQuery(input_query) => input_query.to_tokens(tokens), Queries::Transparent(transparent) => transparent.to_tokens(tokens), - Queries::Intern(intern) => intern.to_tokens(tokens), } } } diff --git a/crates/query-group-macro/tests/interned.rs b/crates/query-group-macro/tests/interned.rs deleted file mode 100644 index f738185b1f..0000000000 --- a/crates/query-group-macro/tests/interned.rs +++ /dev/null @@ -1,50 +0,0 @@ -use query_group_macro::query_group; - -use expect_test::expect; -use salsa::plumbing::AsId; - -mod logger_db; -use logger_db::LoggerDb; - -#[salsa_macros::interned(no_lifetime)] -pub struct InternedString { - data: String, -} - -#[query_group] -pub trait InternedDB: salsa::Database { - #[salsa::interned] - fn intern_string(&self, data: String) -> InternedString; - - fn interned_len(&self, id: InternedString) -> usize; -} - -fn interned_len(db: &dyn InternedDB, id: InternedString) -> usize { - db.lookup_intern_string(id).len() -} - -#[test] -fn intern_round_trip() { - let db = LoggerDb::default(); - - let id = db.intern_string(String::from("Hello, world!")); - let s = db.lookup_intern_string(id); - - assert_eq!(s.len(), 13); - db.assert_logs(expect![[r#"[]"#]]); -} - -#[test] -fn intern_with_query() { - let db = LoggerDb::default(); - - let id = db.intern_string(String::from("Hello, world!")); - let len = db.interned_len(id); - - assert_eq!(len, 13); - db.assert_logs(expect![[r#" - [ - "salsa_event(WillCheckCancellation)", - "salsa_event(WillExecute { database_key: interned_len_shim(Id(0)) })", - ]"#]]); -} |