Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #21767 from Veykril/push-xxyxurxwwmvy
Remove crate from salsa `EditionedFileId`
38 files changed, 142 insertions, 397 deletions
diff --git a/crates/base-db/src/editioned_file_id.rs b/crates/base-db/src/editioned_file_id.rs index 13fb05d565..db3730bccd 100644 --- a/crates/base-db/src/editioned_file_id.rs +++ b/crates/base-db/src/editioned_file_id.rs @@ -1,305 +1,46 @@ //! Defines [`EditionedFileId`], an interned wrapper around [`span::EditionedFileId`] that //! is interned (so queries can take it) and remembers its crate. -use core::fmt; -use std::hash::{Hash, Hasher}; +use std::hash::Hash; +use salsa::Database; use span::Edition; use vfs::FileId; -use crate::{Crate, RootQueryDb}; - -#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub struct EditionedFileId( - salsa::Id, - std::marker::PhantomData<&'static salsa::plumbing::interned::Value<EditionedFileId>>, -); - -const _: () = { - use salsa::plumbing as zalsa_; - use zalsa_::interned as zalsa_struct_; - type Configuration_ = EditionedFileId; - - #[derive(Debug, Clone, PartialEq, Eq)] - pub struct EditionedFileIdData { - editioned_file_id: span::EditionedFileId, - krate: Crate, - } - - // FIXME: This poses an invalidation problem, if one constructs an `EditionedFileId` with a - // different crate then whatever the input of a memo used, it will invalidate the memo causing - // it to recompute even if the crate is not really used. - /// We like to include the origin crate in an `EditionedFileId` (for use in the item tree), - /// but this poses us a problem. - /// - /// Spans contain `EditionedFileId`s, and we don't want to make them store the crate too - /// because that will increase their size, which will increase memory usage significantly. - /// Furthermore, things using spans do not generally need the crate: they are using the - /// file id for queries like `ast_id_map` or `parse`, which do not care about the crate. - /// - /// To solve this, we hash **only the `span::EditionedFileId`**, but on still compare - /// the crate in equality check. This preserves the invariant of `Hash` and `Eq` - - /// although same hashes can be used for different items, same file ids used for multiple - /// crates is a rare thing, and different items always have different hashes. Then, - /// when we only have a `span::EditionedFileId`, we use the `intern()` method to - /// reuse existing file ids, and create new one only if needed. See [`from_span_guess_origin`]. - /// - /// See this for more info: https://rust-lang.zulipchat.com/#narrow/channel/185405-t-compiler.2Frust-analyzer/topic/Letting.20EditionedFileId.20know.20its.20crate/near/530189401 - /// - /// [`from_span_guess_origin`]: EditionedFileId::from_span_guess_origin - #[derive(Hash, PartialEq, Eq)] - struct WithoutCrate { - editioned_file_id: span::EditionedFileId, - } - - impl Hash for EditionedFileIdData { - #[inline] - fn hash<H: Hasher>(&self, state: &mut H) { - let EditionedFileIdData { editioned_file_id, krate: _ } = *self; - editioned_file_id.hash(state); - } - } - - impl zalsa_struct_::HashEqLike<WithoutCrate> for EditionedFileIdData { - #[inline] - fn hash<H: Hasher>(&self, state: &mut H) { - Hash::hash(self, state); - } - - #[inline] - fn eq(&self, data: &WithoutCrate) -> bool { - let EditionedFileIdData { editioned_file_id, krate: _ } = *self; - editioned_file_id == data.editioned_file_id - } - } - - impl zalsa_::HasJar for EditionedFileId { - type Jar = zalsa_struct_::JarImpl<EditionedFileId>; - const KIND: zalsa_::JarKind = zalsa_::JarKind::Struct; - } - - zalsa_::register_jar! { - zalsa_::ErasedJar::erase::<EditionedFileId>() - } - - impl zalsa_struct_::Configuration for EditionedFileId { - const LOCATION: salsa::plumbing::Location = - salsa::plumbing::Location { file: file!(), line: line!() }; - const DEBUG_NAME: &'static str = "EditionedFileId"; - const REVISIONS: std::num::NonZeroUsize = std::num::NonZeroUsize::MAX; - const PERSIST: bool = false; - - type Fields<'a> = EditionedFileIdData; - type Struct<'db> = EditionedFileId; - - fn serialize<S>(_: &Self::Fields<'_>, _: S) -> Result<S::Ok, S::Error> - where - S: zalsa_::serde::Serializer, - { - unimplemented!("attempted to serialize value that set `PERSIST` to false") - } - - fn deserialize<'de, D>(_: D) -> Result<Self::Fields<'static>, D::Error> - where - D: zalsa_::serde::Deserializer<'de>, - { - unimplemented!("attempted to deserialize value that cannot set `PERSIST` to false"); - } - } - - impl Configuration_ { - pub fn ingredient(zalsa: &zalsa_::Zalsa) -> &zalsa_struct_::IngredientImpl<Self> { - static CACHE: zalsa_::IngredientCache<zalsa_struct_::IngredientImpl<EditionedFileId>> = - zalsa_::IngredientCache::new(); - - // SAFETY: `lookup_jar_by_type` returns a valid ingredient index, and the only - // ingredient created by our jar is the struct ingredient. - unsafe { - CACHE.get_or_create(zalsa, || { - zalsa.lookup_jar_by_type::<zalsa_struct_::JarImpl<EditionedFileId>>() - }) - } - } - } - - impl zalsa_::AsId for EditionedFileId { - fn as_id(&self) -> salsa::Id { - self.0.as_id() - } - } - impl zalsa_::FromId for EditionedFileId { - fn from_id(id: salsa::Id) -> Self { - Self(<salsa::Id>::from_id(id), std::marker::PhantomData) - } - } - - unsafe impl Send for EditionedFileId {} - unsafe impl Sync for EditionedFileId {} - - impl std::fmt::Debug for EditionedFileId { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Self::default_debug_fmt(*self, f) - } - } - - impl zalsa_::SalsaStructInDb for EditionedFileId { - type MemoIngredientMap = salsa::plumbing::MemoIngredientSingletonIndex; - - fn lookup_ingredient_index(aux: &zalsa_::Zalsa) -> salsa::plumbing::IngredientIndices { - aux.lookup_jar_by_type::<zalsa_struct_::JarImpl<EditionedFileId>>().into() - } - - fn entries(zalsa: &zalsa_::Zalsa) -> impl Iterator<Item = zalsa_::DatabaseKeyIndex> + '_ { - let _ingredient_index = - zalsa.lookup_jar_by_type::<zalsa_struct_::JarImpl<EditionedFileId>>(); - <EditionedFileId>::ingredient(zalsa).entries(zalsa).map(|entry| entry.key()) - } - - #[inline] - fn cast(id: salsa::Id, type_id: std::any::TypeId) -> Option<Self> { - if type_id == std::any::TypeId::of::<EditionedFileId>() { - Some(<Self as salsa::plumbing::FromId>::from_id(id)) - } else { - None - } - } - - #[inline] - unsafe fn memo_table( - zalsa: &zalsa_::Zalsa, - id: zalsa_::Id, - current_revision: zalsa_::Revision, - ) -> zalsa_::MemoTableWithTypes<'_> { - // SAFETY: Guaranteed by caller. - unsafe { - zalsa.table().memos::<zalsa_struct_::Value<EditionedFileId>>(id, current_revision) - } - } - } - - unsafe impl zalsa_::Update for EditionedFileId { - unsafe fn maybe_update(old_pointer: *mut Self, new_value: Self) -> bool { - if unsafe { *old_pointer } != new_value { - unsafe { *old_pointer = new_value }; - true - } else { - false - } - } - } - - impl EditionedFileId { - pub fn from_span( - db: &(impl salsa::Database + ?Sized), - editioned_file_id: span::EditionedFileId, - krate: Crate, - ) -> Self { - let (zalsa, zalsa_local) = db.zalsas(); - Configuration_::ingredient(zalsa).intern( - zalsa, - zalsa_local, - EditionedFileIdData { editioned_file_id, krate }, - |_, data| data, - ) - } - - /// Guesses the crate for the file. - /// - /// Only use this if you cannot precisely determine the origin. This can happen in one of two cases: - /// - /// 1. The file is not in the module tree. - /// 2. You are latency sensitive and cannot afford calling the def map to precisely compute the origin - /// (e.g. on enter feature, folding, etc.). - pub fn from_span_guess_origin( - db: &dyn RootQueryDb, - editioned_file_id: span::EditionedFileId, - ) -> Self { - let (zalsa, zalsa_local) = db.zalsas(); - Configuration_::ingredient(zalsa).intern( - zalsa, - zalsa_local, - WithoutCrate { editioned_file_id }, - |_, _| { - // FileId not in the database. - let krate = db - .relevant_crates(editioned_file_id.file_id()) - .first() - .copied() - .or_else(|| db.all_crates().first().copied()) - .unwrap_or_else(|| { - // What we're doing here is a bit fishy. We rely on the fact that we only need - // the crate in the item tree, and we should not create an `EditionedFileId` - // without a crate except in cases where it does not matter. The chances that - // `all_crates()` will be empty are also very slim, but it can occur during startup. - // In the very unlikely case that there is a bug and we'll use this crate, Salsa - // will panic. - - // SAFETY: 0 is less than `Id::MAX_U32`. - salsa::plumbing::FromId::from_id(unsafe { salsa::Id::from_index(0) }) - }); - EditionedFileIdData { editioned_file_id, krate } - }, - ) - } - - pub fn editioned_file_id(self, db: &dyn salsa::Database) -> span::EditionedFileId { - let zalsa = db.zalsa(); - let fields = Configuration_::ingredient(zalsa).fields(zalsa, self); - fields.editioned_file_id - } - - pub fn krate(self, db: &dyn salsa::Database) -> Crate { - let zalsa = db.zalsa(); - let fields = Configuration_::ingredient(zalsa).fields(zalsa, self); - fields.krate - } +#[salsa::interned(debug, constructor = from_span_file_id, no_lifetime)] +#[derive(PartialOrd, Ord)] +pub struct EditionedFileId { + field: span::EditionedFileId, +} - /// Default debug formatting for this struct (may be useful if you define your own `Debug` impl) - pub fn default_debug_fmt(this: Self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - zalsa_::with_attached_database(|db| { - let zalsa = db.zalsa(); - let fields = Configuration_::ingredient(zalsa).fields(zalsa, this); - fmt::Debug::fmt(fields, f) - }) - .unwrap_or_else(|| { - f.debug_tuple("EditionedFileId").field(&zalsa_::AsId::as_id(&this)).finish() - }) - } +impl EditionedFileId { + #[inline] + pub fn new(db: &dyn Database, file_id: FileId, edition: Edition) -> Self { + Self::from_span_file_id(db, span::EditionedFileId::new(file_id, edition)) } -}; -impl EditionedFileId { #[inline] - pub fn new(db: &dyn salsa::Database, file_id: FileId, edition: Edition, krate: Crate) -> Self { - EditionedFileId::from_span(db, span::EditionedFileId::new(file_id, edition), krate) + pub fn current_edition(db: &dyn Database, file_id: FileId) -> Self { + Self::from_span_file_id(db, span::EditionedFileId::current_edition(file_id)) } - /// Attaches the current edition and guesses the crate for the file. - /// - /// Only use this if you cannot precisely determine the origin. This can happen in one of two cases: - /// - /// 1. The file is not in the module tree. - /// 2. You are latency sensitive and cannot afford calling the def map to precisely compute the origin - /// (e.g. on enter feature, folding, etc.). #[inline] - pub fn current_edition_guess_origin(db: &dyn RootQueryDb, file_id: FileId) -> Self { - Self::from_span_guess_origin(db, span::EditionedFileId::current_edition(file_id)) + pub fn file_id(self, db: &dyn Database) -> vfs::FileId { + self.field(db).file_id() } #[inline] - pub fn file_id(self, db: &dyn salsa::Database) -> vfs::FileId { - let id = self.editioned_file_id(db); - id.file_id() + pub fn span_file_id(self, db: &dyn Database) -> span::EditionedFileId { + self.field(db) } #[inline] - pub fn unpack(self, db: &dyn salsa::Database) -> (vfs::FileId, span::Edition) { - let id = self.editioned_file_id(db); - (id.file_id(), id.edition()) + pub fn unpack(self, db: &dyn Database) -> (vfs::FileId, span::Edition) { + self.field(db).unpack() } #[inline] - pub fn edition(self, db: &dyn salsa::Database) -> Edition { - self.editioned_file_id(db).edition() + pub fn edition(self, db: &dyn Database) -> Edition { + self.field(db).edition() } } diff --git a/crates/base-db/src/input.rs b/crates/base-db/src/input.rs index 151aba82a2..246c57edc2 100644 --- a/crates/base-db/src/input.rs +++ b/crates/base-db/src/input.rs @@ -870,7 +870,7 @@ impl CrateGraphBuilder { impl Crate { pub fn root_file_id(self, db: &dyn salsa::Database) -> EditionedFileId { let data = self.data(db); - EditionedFileId::new(db, data.root_file_id, data.edition, self) + EditionedFileId::new(db, data.root_file_id, data.edition) } } diff --git a/crates/hir-def/src/db.rs b/crates/hir-def/src/db.rs index ccd4bc9be8..b0b652a150 100644 --- a/crates/hir-def/src/db.rs +++ b/crates/hir-def/src/db.rs @@ -96,7 +96,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase { /// Computes an [`ItemTree`] for the given file or macro expansion. #[salsa::invoke(file_item_tree_query)] #[salsa::transparent] - fn file_item_tree(&self, file_id: HirFileId) -> &ItemTree; + fn file_item_tree(&self, file_id: HirFileId, krate: Crate) -> &ItemTree; /// Turns a MacroId into a MacroDefId, describing the macro's definition post name resolution. #[salsa::invoke(macro_def)] diff --git a/crates/hir-def/src/expr_store/tests/body/block.rs b/crates/hir-def/src/expr_store/tests/body/block.rs index d457a4ca7a..83594ee021 100644 --- a/crates/hir-def/src/expr_store/tests/body/block.rs +++ b/crates/hir-def/src/expr_store/tests/body/block.rs @@ -196,7 +196,7 @@ fn f() { ), block: Some( BlockId( - 4401, + 4801, ), ), }"#]], diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs index a1707f17be..9825dbfe1c 100644 --- a/crates/hir-def/src/item_tree.rs +++ b/crates/hir-def/src/item_tree.rs @@ -44,6 +44,7 @@ use std::{ }; use ast::{AstNode, StructKind}; +use base_db::Crate; use cfg::CfgOptions; use hir_expand::{ ExpandTo, HirFileId, @@ -121,21 +122,23 @@ fn lower_extra_crate_attrs<'a>( } #[salsa_macros::tracked(returns(deref))] -pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> { +pub(crate) fn file_item_tree_query( + db: &dyn DefDatabase, + file_id: HirFileId, + krate: Crate, +) -> Arc<ItemTree> { let _p = tracing::info_span!("file_item_tree_query", ?file_id).entered(); static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new(); - let ctx = lower::Ctx::new(db, file_id); + let ctx = lower::Ctx::new(db, file_id, krate); let syntax = db.parse_or_expand(file_id); let mut item_tree = match_ast! { match syntax { ast::SourceFile(file) => { - let krate = file_id.krate(db); let root_file_id = krate.root_file_id(db); let extra_top_attrs = (file_id == root_file_id).then(|| { parse_extra_crate_attrs(db, krate).map(|crate_attrs| { - let file_id = root_file_id.editioned_file_id(db); - lower_extra_crate_attrs(db, crate_attrs, file_id, &|| ctx.cfg_options()) + lower_extra_crate_attrs(db, crate_attrs, root_file_id.span_file_id(db), &|| ctx.cfg_options()) }) }).flatten(); let top_attrs = match extra_top_attrs { @@ -190,14 +193,18 @@ pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> } #[salsa_macros::tracked(returns(deref))] -pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc<ItemTree> { +pub(crate) fn block_item_tree_query( + db: &dyn DefDatabase, + block: BlockId, + krate: Crate, +) -> Arc<ItemTree> { let _p = tracing::info_span!("block_item_tree_query", ?block).entered(); static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new(); let loc = block.lookup(db); let block = loc.ast_id.to_node(db); - let ctx = lower::Ctx::new(db, loc.ast_id.file_id); + let ctx = lower::Ctx::new(db, loc.ast_id.file_id, krate); let mut item_tree = ctx.lower_block(&block); let ItemTree { top_level, top_attrs, attrs, vis, big_data, small_data } = &item_tree; if small_data.is_empty() @@ -356,10 +363,10 @@ impl TreeId { Self { file, block } } - pub(crate) fn item_tree<'db>(&self, db: &'db dyn DefDatabase) -> &'db ItemTree { + pub(crate) fn item_tree<'db>(&self, db: &'db dyn DefDatabase, krate: Crate) -> &'db ItemTree { match self.block { - Some(block) => block_item_tree_query(db, block), - None => file_item_tree_query(db, self.file), + Some(block) => block_item_tree_query(db, block, krate), + None => file_item_tree_query(db, self.file, krate), } } diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs index 3f19e00154..31c6ef867d 100644 --- a/crates/hir-def/src/item_tree/lower.rs +++ b/crates/hir-def/src/item_tree/lower.rs @@ -2,7 +2,7 @@ use std::cell::OnceCell; -use base_db::FxIndexSet; +use base_db::{Crate, FxIndexSet}; use cfg::CfgOptions; use hir_expand::{ HirFileId, @@ -36,12 +36,13 @@ pub(super) struct Ctx<'a> { span_map: OnceCell<SpanMap>, file: HirFileId, cfg_options: OnceCell<&'a CfgOptions>, + krate: Crate, top_level: Vec<ModItemId>, visibilities: FxIndexSet<RawVisibility>, } impl<'a> Ctx<'a> { - pub(super) fn new(db: &'a dyn DefDatabase, file: HirFileId) -> Self { + pub(super) fn new(db: &'a dyn DefDatabase, file: HirFileId, krate: Crate) -> Self { Self { db, tree: ItemTree::default(), @@ -51,12 +52,13 @@ impl<'a> Ctx<'a> { span_map: OnceCell::new(), visibilities: FxIndexSet::default(), top_level: Vec::new(), + krate, } } #[inline] pub(super) fn cfg_options(&self) -> &'a CfgOptions { - self.cfg_options.get_or_init(|| self.file.krate(self.db).cfg_options(self.db)) + self.cfg_options.get_or_init(|| self.krate.cfg_options(self.db)) } pub(super) fn span_map(&self) -> SpanMapRef<'_> { diff --git a/crates/hir-def/src/item_tree/tests.rs b/crates/hir-def/src/item_tree/tests.rs index 1926ed74e8..b71b25a1a5 100644 --- a/crates/hir-def/src/item_tree/tests.rs +++ b/crates/hir-def/src/item_tree/tests.rs @@ -6,7 +6,7 @@ use crate::{db::DefDatabase, test_db::TestDB}; fn check(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) { let (db, file_id) = TestDB::with_single_file(ra_fixture); - let item_tree = db.file_item_tree(file_id.into()); + let item_tree = db.file_item_tree(file_id.into(), db.test_crate()); let pretty = item_tree.pretty_print(&db, Edition::CURRENT); expect.assert_eq(&pretty); } diff --git a/crates/hir-def/src/macro_expansion_tests/mbe.rs b/crates/hir-def/src/macro_expansion_tests/mbe.rs index 7b5d0103e6..d93df7af6a 100644 --- a/crates/hir-def/src/macro_expansion_tests/mbe.rs +++ b/crates/hir-def/src/macro_expansion_tests/mbe.rs @@ -35,9 +35,9 @@ macro_rules! f { }; } -struct#0:MacroRules[BE8F, 0]@58..64#17408# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#17408# - map#0:MacroRules[BE8F, 0]@86..89#17408#:#0:MacroRules[BE8F, 0]@89..90#17408# #0:MacroRules[BE8F, 0]@89..90#17408#::#0:MacroRules[BE8F, 0]@91..93#17408#std#0:MacroRules[BE8F, 0]@93..96#17408#::#0:MacroRules[BE8F, 0]@96..98#17408#collections#0:MacroRules[BE8F, 0]@98..109#17408#::#0:MacroRules[BE8F, 0]@109..111#17408#HashSet#0:MacroRules[BE8F, 0]@111..118#17408#<#0:MacroRules[BE8F, 0]@118..119#17408#(#0:MacroRules[BE8F, 0]@119..120#17408#)#0:MacroRules[BE8F, 0]@120..121#17408#>#0:MacroRules[BE8F, 0]@121..122#17408#,#0:MacroRules[BE8F, 0]@122..123#17408# -}#0:MacroRules[BE8F, 0]@132..133#17408# +struct#0:MacroRules[BE8F, 0]@58..64#18432# MyTraitMap2#0:MacroCall[BE8F, 0]@31..42#ROOT2024# {#0:MacroRules[BE8F, 0]@72..73#18432# + map#0:MacroRules[BE8F, 0]@86..89#18432#:#0:MacroRules[BE8F, 0]@89..90#18432# #0:MacroRules[BE8F, 0]@89..90#18432#::#0:MacroRules[BE8F, 0]@91..93#18432#std#0:MacroRules[BE8F, 0]@93..96#18432#::#0:MacroRules[BE8F, 0]@96..98#18432#collections#0:MacroRules[BE8F, 0]@98..109#18432#::#0:MacroRules[BE8F, 0]@109..111#18432#HashSet#0:MacroRules[BE8F, 0]@111..118#18432#<#0:MacroRules[BE8F, 0]@118..119#18432#(#0:MacroRules[BE8F, 0]@119..120#18432#)#0:MacroRules[BE8F, 0]@120..121#18432#>#0:MacroRules[BE8F, 0]@121..122#18432#,#0:MacroRules[BE8F, 0]@122..123#18432# +}#0:MacroRules[BE8F, 0]@132..133#18432# "#]], ); } @@ -197,7 +197,7 @@ macro_rules! mk_struct { #[macro_use] mod foo; -struct#1:MacroRules[DB0C, 0]@59..65#17408# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#17408#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#17408#;#1:MacroRules[DB0C, 0]@75..76#17408# +struct#1:MacroRules[DB0C, 0]@59..65#18432# Foo#0:MacroCall[DB0C, 0]@32..35#ROOT2024#(#1:MacroRules[DB0C, 0]@70..71#18432#u32#0:MacroCall[DB0C, 0]@41..44#ROOT2024#)#1:MacroRules[DB0C, 0]@74..75#18432#;#1:MacroRules[DB0C, 0]@75..76#18432# "#]], ); } @@ -423,10 +423,10 @@ m! { foo, bar } macro_rules! m { ($($i:ident),*) => ( impl Bar { $(fn $i() {})* } ); } -impl#\17408# Bar#\17408# {#\17408# - fn#\17408# foo#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408# - fn#\17408# bar#\ROOT2024#(#\17408#)#\17408# {#\17408#}#\17408# -}#\17408# +impl#\18432# Bar#\18432# {#\18432# + fn#\18432# foo#\ROOT2024#(#\18432#)#\18432# {#\18432#}#\18432# + fn#\18432# bar#\ROOT2024#(#\18432#)#\18432# {#\18432#}#\18432# +}#\18432# "#]], ); } diff --git a/crates/hir-def/src/macro_expansion_tests/mod.rs b/crates/hir-def/src/macro_expansion_tests/mod.rs index c63f2c1d78..8ee93dcaa3 100644 --- a/crates/hir-def/src/macro_expansion_tests/mod.rs +++ b/crates/hir-def/src/macro_expansion_tests/mod.rs @@ -458,7 +458,7 @@ m!(g); "#; let (db, file_id) = TestDB::with_single_file(fixture); - let krate = file_id.krate(&db); + let krate = db.test_crate(); let def_map = crate_def_map(&db, krate); let source = def_map[def_map.root].definition_source(&db); let source_file = match source.value { diff --git a/crates/hir-def/src/nameres/collector.rs b/crates/hir-def/src/nameres/collector.rs index e672e83f01..9c101c127b 100644 --- a/crates/hir-def/src/nameres/collector.rs +++ b/crates/hir-def/src/nameres/collector.rs @@ -279,7 +279,7 @@ impl<'db> DefCollector<'db> { let _p = tracing::info_span!("seed_with_top_level").entered(); let file_id = self.def_map.krate.root_file_id(self.db); - let item_tree = self.db.file_item_tree(file_id.into()); + let item_tree = self.db.file_item_tree(file_id.into(), self.def_map.krate); let attrs = match item_tree.top_level_attrs() { AttrsOrCfg::Enabled { attrs } => attrs.as_ref(), AttrsOrCfg::CfgDisabled(it) => it.1.as_ref(), @@ -387,7 +387,7 @@ impl<'db> DefCollector<'db> { } fn seed_with_inner(&mut self, tree_id: TreeId) { - let item_tree = tree_id.item_tree(self.db); + let item_tree = tree_id.item_tree(self.db, self.def_map.krate); let is_cfg_enabled = matches!(item_tree.top_level_attrs(), AttrsOrCfg::Enabled { .. }); if is_cfg_enabled { self.inject_prelude(); @@ -1708,7 +1708,7 @@ impl<'db> DefCollector<'db> { } let file_id = macro_call_id.into(); - let item_tree = self.db.file_item_tree(file_id); + let item_tree = self.db.file_item_tree(file_id, self.def_map.krate); // Derive helpers that are in scope for an item are also in scope for attribute macro expansions // of that item (but not derive or fn like macros). @@ -2335,10 +2335,10 @@ impl ModCollector<'_, '_> { self.file_id(), &module.name, path_attr.as_deref(), - self.def_collector.def_map.krate, ) { Ok((file_id, is_mod_rs, mod_dir)) => { - let item_tree = db.file_item_tree(file_id.into()); + let item_tree = + db.file_item_tree(file_id.into(), self.def_collector.def_map.krate); match item_tree.top_level_attrs() { AttrsOrCfg::CfgDisabled(cfg) => { self.emit_unconfigured_diagnostic( @@ -2828,8 +2828,8 @@ foo!(KABOOM); let fixture = r#" //- /lib.rs crate:foo crate-attr:recursion_limit="4" crate-attr:no_core crate-attr:no_std crate-attr:feature(register_tool) "#; - let (db, file_id) = TestDB::with_single_file(fixture); - let def_map = crate_def_map(&db, file_id.krate(&db)); + let (db, _) = TestDB::with_single_file(fixture); + let def_map = crate_def_map(&db, db.test_crate()); assert_eq!(def_map.recursion_limit(), 4); assert!(def_map.is_no_core()); assert!(def_map.is_no_std()); diff --git a/crates/hir-def/src/nameres/mod_resolution.rs b/crates/hir-def/src/nameres/mod_resolution.rs index 140b77ac00..0c50f13edf 100644 --- a/crates/hir-def/src/nameres/mod_resolution.rs +++ b/crates/hir-def/src/nameres/mod_resolution.rs @@ -1,6 +1,6 @@ //! This module resolves `mod foo;` declaration to file. use arrayvec::ArrayVec; -use base_db::{AnchoredPath, Crate}; +use base_db::AnchoredPath; use hir_expand::{EditionedFileId, name::Name}; use crate::{HirFileId, db::DefDatabase}; @@ -62,7 +62,6 @@ impl ModDir { file_id: HirFileId, name: &Name, attr_path: Option<&str>, - krate: Crate, ) -> Result<(EditionedFileId, bool, ModDir), Box<[String]>> { let name = name.as_str(); @@ -92,7 +91,7 @@ impl ModDir { if let Some(mod_dir) = self.child(dir_path, !root_dir_owner) { return Ok(( // FIXME: Edition, is this rightr? - EditionedFileId::new(db, file_id, orig_file_id.edition(db), krate), + EditionedFileId::new(db, file_id, orig_file_id.edition(db)), is_mod_rs, mod_dir, )); diff --git a/crates/hir-def/src/nameres/tests/incremental.rs b/crates/hir-def/src/nameres/tests/incremental.rs index 225ba95863..7fedfa03bb 100644 --- a/crates/hir-def/src/nameres/tests/incremental.rs +++ b/crates/hir-def/src/nameres/tests/incremental.rs @@ -604,7 +604,7 @@ pub type Ty = (); execute_assert_events( &db, || { - db.file_item_tree(pos.file_id.into()); + db.file_item_tree(pos.file_id.into(), db.test_crate()); }, &[("file_item_tree_query", 1), ("parse", 1)], expect![[r#" @@ -624,7 +624,7 @@ pub type Ty = (); execute_assert_events( &db, || { - db.file_item_tree(pos.file_id.into()); + db.file_item_tree(pos.file_id.into(), db.test_crate()); }, &[("file_item_tree_query", 1), ("parse", 1)], expect![[r#" diff --git a/crates/hir-expand/src/builtin/fn_macro.rs b/crates/hir-expand/src/builtin/fn_macro.rs index 2de7290a21..b3572a1cef 100644 --- a/crates/hir-expand/src/builtin/fn_macro.rs +++ b/crates/hir-expand/src/builtin/fn_macro.rs @@ -774,7 +774,7 @@ fn relative_file( if res == call_site && !allow_recursion { Err(ExpandError::other(err_span, format!("recursive inclusion of `{path_str}`"))) } else { - Ok(EditionedFileId::new(db, res, lookup.krate.data(db).edition, lookup.krate)) + Ok(EditionedFileId::new(db, res, lookup.krate.data(db).edition)) } } diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 51767f87ff..363465fdda 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -162,7 +162,7 @@ fn syntax_context(db: &dyn ExpandDatabase, file: HirFileId, edition: Edition) -> } fn resolve_span(db: &dyn ExpandDatabase, Span { range, anchor, ctx: _ }: Span) -> FileRange { - let file_id = EditionedFileId::from_span_guess_origin(db, anchor.file_id); + let file_id = EditionedFileId::from_span_file_id(db, anchor.file_id); let anchor_offset = db.ast_id_map(file_id.into()).get_erased(anchor.ast_id).text_range().start(); FileRange { file_id, range: range + anchor_offset } diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index 05541e782e..4b2c75ed38 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -386,7 +386,7 @@ impl MacroCallKind { impl HirFileId { pub fn edition(self, db: &dyn ExpandDatabase) -> Edition { match self { - HirFileId::FileId(file_id) => file_id.editioned_file_id(db).edition(), + HirFileId::FileId(file_id) => file_id.edition(db), HirFileId::MacroFile(m) => db.lookup_intern_macro_call(m).def.edition, } } @@ -1118,14 +1118,6 @@ impl HirFileId { HirFileId::MacroFile(_) => None, } } - - #[inline] - pub fn krate(self, db: &dyn ExpandDatabase) -> Crate { - match self { - HirFileId::FileId(it) => it.krate(db), - HirFileId::MacroFile(it) => it.loc(db).krate, - } - } } impl PartialEq<EditionedFileId> for HirFileId { diff --git a/crates/hir-expand/src/span_map.rs b/crates/hir-expand/src/span_map.rs index 586b815294..71d0b880ca 100644 --- a/crates/hir-expand/src/span_map.rs +++ b/crates/hir-expand/src/span_map.rs @@ -135,7 +135,7 @@ pub(crate) fn real_span_map( }); Arc::new(RealSpanMap::from_file( - editioned_file_id.editioned_file_id(db), + editioned_file_id.span_file_id(db), pairs.into_boxed_slice(), tree.syntax().text_range().end(), )) diff --git a/crates/hir-ty/src/tests/incremental.rs b/crates/hir-ty/src/tests/incremental.rs index cf7ff6f7ec..faa7b80a89 100644 --- a/crates/hir-ty/src/tests/incremental.rs +++ b/crates/hir-ty/src/tests/incremental.rs @@ -31,6 +31,7 @@ fn foo() -> i32 { &[("InferenceResult::for_body_", 1)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -118,6 +119,7 @@ fn baz() -> i32 { &[("InferenceResult::for_body_", 3)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -237,6 +239,7 @@ $0", &[("TraitImpls::for_crate_", 1)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -311,6 +314,7 @@ $0", &[("TraitImpls::for_crate_", 1)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -386,6 +390,7 @@ $0", &[("TraitImpls::for_crate_", 1)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -462,6 +467,7 @@ $0", &[("TraitImpls::for_crate_", 1)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", @@ -562,6 +568,7 @@ fn main() { &[("trait_solve_shim", 0)], expect_test::expect![[r#" [ + "source_root_crates_shim", "crate_local_def_map", "file_item_tree_query", "ast_id_map_shim", diff --git a/crates/hir-ty/src/tests/macros.rs b/crates/hir-ty/src/tests/macros.rs index 2f41de64cb..28a688d4a3 100644 --- a/crates/hir-ty/src/tests/macros.rs +++ b/crates/hir-ty/src/tests/macros.rs @@ -9,16 +9,16 @@ use super::{check_infer, check_types}; fn cfg_impl_def() { check_types( r#" -//- /main.rs crate:main deps:foo cfg:test +//- /main.rs crate:main deps:foo cfg:some_cfg use foo::S as T; struct S; -#[cfg(test)] +#[cfg(some_cfg)] impl S { fn foo1(&self) -> i32 { 0 } } -#[cfg(not(test))] +#[cfg(not(some_cfg))] impl S { fn foo2(&self) -> i32 { 0 } } @@ -31,12 +31,12 @@ fn test() { //- /foo.rs crate:foo pub struct S; -#[cfg(not(test))] +#[cfg(not(some_cfg))] impl S { pub fn foo3(&self) -> i32 { 0 } } -#[cfg(test)] +#[cfg(some_cfg)] impl S { pub fn foo4(&self) -> i32 { 0 } } diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index 11d79e2d7b..0b3515fd04 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -1091,7 +1091,7 @@ fn macro_call_diagnostics<'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); - if Some(err.span().anchor.file_id) == file_id.file_id().map(|it| it.editioned_file_id(db)) { + if Some(err.span().anchor.file_id) == file_id.file_id().map(|it| it.span_file_id(db)) { range.value = err.span().range + db.ast_id_map(file_id).get_erased(err.span().anchor.ast_id).text_range().start(); } diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index 1cf3b98160..c816fe967c 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -472,12 +472,12 @@ impl<'db> SemanticsImpl<'db> { pub fn attach_first_edition_opt(&self, file: FileId) -> Option<EditionedFileId> { let krate = self.file_to_module_defs(file).next()?.krate(self.db); - Some(EditionedFileId::new(self.db, file, krate.edition(self.db), krate.id)) + Some(EditionedFileId::new(self.db, file, krate.edition(self.db))) } pub fn attach_first_edition(&self, file: FileId) -> EditionedFileId { self.attach_first_edition_opt(file) - .unwrap_or_else(|| EditionedFileId::current_edition_guess_origin(self.db, file)) + .unwrap_or_else(|| EditionedFileId::current_edition(self.db, file)) } pub fn parse_guess_edition(&self, file_id: FileId) -> ast::SourceFile { diff --git a/crates/hir/src/semantics/child_by_source.rs b/crates/hir/src/semantics/child_by_source.rs index c1f72debe5..143cc14c33 100644 --- a/crates/hir/src/semantics/child_by_source.rs +++ b/crates/hir/src/semantics/child_by_source.rs @@ -93,7 +93,6 @@ impl ChildBySource for ModuleId { impl ChildBySource for ItemScope { fn child_by_source_to(&self, db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId) { - let krate = file_id.krate(db); self.declarations().for_each(|item| add_module_def(db, res, file_id, item)); self.impls().for_each(|imp| insert_item_loc(db, res, file_id, imp, keys::IMPL)); self.extern_blocks().for_each(|extern_block| { @@ -123,6 +122,8 @@ impl ChildBySource for ItemScope { |(ast_id, calls)| { let adt = ast_id.to_node(db); calls.for_each(|(attr_id, call_id, calls)| { + // FIXME: Is this the right crate? + let krate = call_id.lookup(db).krate; // FIXME: Fix cfg_attr handling. let (attr, _, _, _) = attr_id.find_attr_range_with_source(db, krate, &adt); res[keys::DERIVE_MACRO_CALL] diff --git a/crates/ide-db/src/search.rs b/crates/ide-db/src/search.rs index 4196a13aa3..3822eaae2c 100644 --- a/crates/ide-db/src/search.rs +++ b/crates/ide-db/src/search.rs @@ -169,7 +169,7 @@ impl SearchScope { entries.extend( source_root .iter() - .map(|id| (EditionedFileId::new(db, id, crate_data.edition, krate), None)), + .map(|id| (EditionedFileId::new(db, id, crate_data.edition), None)), ); } SearchScope { entries } @@ -183,9 +183,11 @@ impl SearchScope { let source_root = db.file_source_root(root_file).source_root_id(db); let source_root = db.source_root(source_root).source_root(db); - entries.extend(source_root.iter().map(|id| { - (EditionedFileId::new(db, id, rev_dep.edition(db), rev_dep.into()), None) - })); + entries.extend( + source_root + .iter() + .map(|id| (EditionedFileId::new(db, id, rev_dep.edition(db)), None)), + ); } SearchScope { entries } } @@ -199,7 +201,7 @@ impl SearchScope { SearchScope { entries: source_root .iter() - .map(|id| (EditionedFileId::new(db, id, of.edition(db), of.into()), None)) + .map(|id| (EditionedFileId::new(db, id, of.edition(db)), None)) .collect(), } } diff --git a/crates/ide-db/src/test_data/test_doc_alias.txt b/crates/ide-db/src/test_data/test_doc_alias.txt index 0c28c312f8..fc98ebb069 100644 --- a/crates/ide-db/src/test_data/test_doc_alias.txt +++ b/crates/ide-db/src/test_data/test_doc_alias.txt @@ -2,7 +2,7 @@ ( Module { id: ModuleIdLt { - [salsa id]: Id(3800), + [salsa id]: Id(3400), }, }, [ diff --git a/crates/ide-db/src/test_data/test_symbol_index_collection.txt b/crates/ide-db/src/test_data/test_symbol_index_collection.txt index 4b588572d3..46d938b5a5 100644 --- a/crates/ide-db/src/test_data/test_symbol_index_collection.txt +++ b/crates/ide-db/src/test_data/test_symbol_index_collection.txt @@ -2,7 +2,7 @@ ( Module { id: ModuleIdLt { - [salsa id]: Id(3800), + [salsa id]: Id(3400), }, }, [ @@ -671,7 +671,7 @@ def: Module( Module { id: ModuleIdLt { - [salsa id]: Id(3801), + [salsa id]: Id(3401), }, }, ), @@ -706,7 +706,7 @@ def: Module( Module { id: ModuleIdLt { - [salsa id]: Id(3802), + [salsa id]: Id(3402), }, }, ), @@ -998,7 +998,7 @@ ( Module { id: ModuleIdLt { - [salsa id]: Id(3801), + [salsa id]: Id(3401), }, }, [ @@ -1044,7 +1044,7 @@ ( Module { id: ModuleIdLt { - [salsa id]: Id(3802), + [salsa id]: Id(3402), }, }, [ diff --git a/crates/ide-db/src/test_data/test_symbols_exclude_imports.txt b/crates/ide-db/src/test_data/test_symbols_exclude_imports.txt index 87f0c7d9a8..aff1d56c56 100644 --- a/crates/ide-db/src/test_data/test_symbols_exclude_imports.txt +++ b/crates/ide-db/src/test_data/test_symbols_exclude_imports.txt @@ -5,7 +5,7 @@ Struct( Struct { id: StructId( - 3c00, + 4000, ), }, ), diff --git a/crates/ide-db/src/test_data/test_symbols_with_imports.txt b/crates/ide-db/src/test_data/test_symbols_with_imports.txt index e96aa889ba..bf5d81cfb1 100644 --- a/crates/ide-db/src/test_data/test_symbols_with_imports.txt +++ b/crates/ide-db/src/test_data/test_symbols_with_imports.txt @@ -5,7 +5,7 @@ Struct( Struct { id: StructId( - 3c00, + 4000, ), }, ), @@ -42,7 +42,7 @@ Struct( Struct { id: StructId( - 3c00, + 4000, ), }, ), diff --git a/crates/ide-db/src/traits.rs b/crates/ide-db/src/traits.rs index 7200e7fbe5..60bdc2d82c 100644 --- a/crates/ide-db/src/traits.rs +++ b/crates/ide-db/src/traits.rs @@ -130,7 +130,8 @@ mod tests { database.apply_change(change_fixture.change); let (file_id, range_or_offset) = change_fixture.file_position.expect("expected a marker ($0)"); - let file_id = EditionedFileId::from_span_guess_origin(&database, file_id); + + let file_id = EditionedFileId::from_span_file_id(&database, file_id); let offset = range_or_offset.expect_offset(); (database, FilePosition { file_id, offset }) } diff --git a/crates/ide-ssr/src/from_comment.rs b/crates/ide-ssr/src/from_comment.rs index de26879c29..181cc74a51 100644 --- a/crates/ide-ssr/src/from_comment.rs +++ b/crates/ide-ssr/src/from_comment.rs @@ -17,7 +17,7 @@ pub fn ssr_from_comment( frange: FileRange, ) -> Option<(MatchFinder<'_>, TextRange)> { let comment = { - let file_id = EditionedFileId::current_edition_guess_origin(db, frange.file_id); + let file_id = EditionedFileId::current_edition(db, frange.file_id); let file = db.parse(file_id); file.tree().syntax().token_at_offset(frange.range.start()).find_map(ast::Comment::cast) diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index 930eaf2262..81a771fec8 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -339,8 +339,7 @@ impl Analysis { pub fn parse(&self, file_id: FileId) -> Cancellable<SourceFile> { // FIXME edition self.with_db(|db| { - let editioned_file_id_wrapper = - EditionedFileId::current_edition_guess_origin(&self.db, file_id); + let editioned_file_id_wrapper = EditionedFileId::current_edition(&self.db, file_id); db.parse(editioned_file_id_wrapper).tree() }) @@ -369,7 +368,7 @@ impl Analysis { /// supported). pub fn matching_brace(&self, position: FilePosition) -> Cancellable<Option<TextSize>> { self.with_db(|db| { - let file_id = EditionedFileId::current_edition_guess_origin(&self.db, position.file_id); + let file_id = EditionedFileId::current_edition(&self.db, position.file_id); let parse = db.parse(file_id); let file = parse.tree(); matching_brace::matching_brace(&file, position.offset) @@ -430,7 +429,7 @@ impl Analysis { pub fn join_lines(&self, config: &JoinLinesConfig, frange: FileRange) -> Cancellable<TextEdit> { self.with_db(|db| { let editioned_file_id_wrapper = - EditionedFileId::current_edition_guess_origin(&self.db, frange.file_id); + EditionedFileId::current_edition(&self.db, frange.file_id); let parse = db.parse(editioned_file_id_wrapper); join_lines::join_lines(config, &parse.tree(), frange.range) }) @@ -471,8 +470,7 @@ impl Analysis { ) -> Cancellable<Vec<StructureNode>> { // FIXME: Edition self.with_db(|db| { - let editioned_file_id_wrapper = - EditionedFileId::current_edition_guess_origin(&self.db, file_id); + let editioned_file_id_wrapper = EditionedFileId::current_edition(&self.db, file_id); let source_file = db.parse(editioned_file_id_wrapper).tree(); file_structure::file_structure(&source_file, config) }) @@ -503,8 +501,7 @@ impl Analysis { /// Returns the set of folding ranges. pub fn folding_ranges(&self, file_id: FileId) -> Cancellable<Vec<Fold>> { self.with_db(|db| { - let editioned_file_id_wrapper = - EditionedFileId::current_edition_guess_origin(&self.db, file_id); + let editioned_file_id_wrapper = EditionedFileId::current_edition(&self.db, file_id); folding_ranges::folding_ranges(&db.parse(editioned_file_id_wrapper).tree()) }) diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 102eb91b74..6464c47716 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -1151,10 +1151,7 @@ pub(super) struct Foo$0 { check_with_scope( code, Some(&mut |db| { - SearchScope::single_file(EditionedFileId::current_edition_guess_origin( - db, - FileId::from_raw(2), - )) + SearchScope::single_file(EditionedFileId::current_edition(db, FileId::from_raw(2))) }), expect![[r#" quux Function FileId(0) 19..35 26..30 diff --git a/crates/ide/src/signature_help.rs b/crates/ide/src/signature_help.rs index 9ab07565e9..f86974b4ec 100644 --- a/crates/ide/src/signature_help.rs +++ b/crates/ide/src/signature_help.rs @@ -1975,8 +1975,8 @@ trait Sub: Super + Super { fn f() -> impl Sub<$0 "#, expect![[r#" - trait Sub<SubTy = …, SuperTy = …> - ^^^^^^^^^ ----------- + trait Sub<SuperTy = …, SubTy = …> + ^^^^^^^^^^^ --------- "#]], ); } diff --git a/crates/ide/src/typing.rs b/crates/ide/src/typing.rs index f8b0dbfe62..11bfb982cf 100644 --- a/crates/ide/src/typing.rs +++ b/crates/ide/src/typing.rs @@ -17,10 +17,7 @@ mod on_enter; use either::Either; use hir::EditionedFileId; -use ide_db::{ - FilePosition, RootDatabase, - base_db::{RootQueryDb, SourceDatabase}, -}; +use ide_db::{FilePosition, RootDatabase, base_db::RootQueryDb}; use span::Edition; use std::iter; @@ -73,16 +70,13 @@ pub(crate) fn on_char_typed( if !TRIGGER_CHARS.contains(&char_typed) { return None; } - let edition = db - .source_root_crates(db.file_source_root(position.file_id).source_root_id(db)) + let krate = db + .relevant_crates(position.file_id) .first() - .map_or(Edition::CURRENT, |crates| crates.data(db).edition); - // FIXME: We are hitting the database here, if we are unlucky this call might block momentarily - // causing the editor to feel sluggish! We need to make this bail if it would block too long? - let editioned_file_id_wrapper = EditionedFileId::from_span_guess_origin( - db, - span::EditionedFileId::new(position.file_id, edition), - ); + .copied() + .unwrap_or_else(|| *db.all_crates().first().unwrap()); + let edition = krate.data(db).edition; + let editioned_file_id_wrapper = EditionedFileId::new(db, position.file_id, edition); let file = &db.parse(editioned_file_id_wrapper); let char_matches_position = file.tree().syntax().text().char_at(position.offset) == Some(char_typed); diff --git a/crates/ide/src/typing/on_enter.rs b/crates/ide/src/typing/on_enter.rs index 76a2802d29..fdc583a15c 100644 --- a/crates/ide/src/typing/on_enter.rs +++ b/crates/ide/src/typing/on_enter.rs @@ -51,7 +51,7 @@ use ide_db::text_edit::TextEdit; //  pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<TextEdit> { let editioned_file_id_wrapper = - ide_db::base_db::EditionedFileId::current_edition_guess_origin(db, position.file_id); + ide_db::base_db::EditionedFileId::current_edition(db, position.file_id); let parse = db.parse(editioned_file_id_wrapper); let file = parse.tree(); let token = file.syntax().token_at_offset(position.offset).left_biased()?; diff --git a/crates/ide/src/view_item_tree.rs b/crates/ide/src/view_item_tree.rs index e1a7e4e6ab..8d84eba7ab 100644 --- a/crates/ide/src/view_item_tree.rs +++ b/crates/ide/src/view_item_tree.rs @@ -10,6 +10,9 @@ use ide_db::{FileId, RootDatabase}; // | VS Code | **rust-analyzer: Debug ItemTree** | pub(crate) fn view_item_tree(db: &RootDatabase, file_id: FileId) -> String { let sema = Semantics::new(db); + let Some(krate) = sema.first_crate(file_id) else { + return String::new(); + }; let file_id = sema.attach_first_edition(file_id); - db.file_item_tree(file_id.into()).pretty_print(db, file_id.edition(db)) + db.file_item_tree(file_id.into(), krate.into()).pretty_print(db, file_id.edition(db)) } diff --git a/crates/load-cargo/src/lib.rs b/crates/load-cargo/src/lib.rs index b8ce3a8da4..95fcfce291 100644 --- a/crates/load-cargo/src/lib.rs +++ b/crates/load-cargo/src/lib.rs @@ -638,7 +638,7 @@ impl ProcMacroExpander for Expander { current_span = Span { range: resolved.range, anchor: SpanAnchor { - file_id: resolved.file_id.editioned_file_id(db), + file_id: resolved.file_id.span_file_id(db), ast_id: span::ROOT_ERASED_FILE_AST_ID, }, ctx: current_ctx, @@ -652,7 +652,7 @@ impl ProcMacroExpander for Expander { let resolved = db.resolve_span(current_span); Ok(SubResponse::SpanSourceResult { - file_id: resolved.file_id.editioned_file_id(db).as_u32(), + file_id: resolved.file_id.span_file_id(db).as_u32(), ast_id: span::ROOT_ERASED_FILE_AST_ID.into_raw(), start: u32::from(resolved.range.start()), end: u32::from(resolved.range.end()), @@ -684,7 +684,7 @@ impl ProcMacroExpander for Expander { .text_range(); let parent_span = Some(ParentSpan { - file_id: editioned_file_id.editioned_file_id(db).as_u32(), + file_id: editioned_file_id.span_file_id(db).as_u32(), ast_id: span::ROOT_ERASED_FILE_AST_ID.into_raw(), start: u32::from(range.start()), end: u32::from(range.end()), diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 1995d38898..caa35879aa 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -126,7 +126,7 @@ impl flags::AnalysisStats { let source_roots = krates .iter() .cloned() - .map(|krate| db.file_source_root(krate.root_file(db)).source_root_id(db)) + .map(|krate| (db.file_source_root(krate.root_file(db)).source_root_id(db), krate)) .unique(); let mut dep_loc = 0; @@ -137,7 +137,7 @@ impl flags::AnalysisStats { let mut workspace_item_stats = PrettyItemStats::default(); let mut dep_item_stats = PrettyItemStats::default(); - for source_root_id in source_roots { + for (source_root_id, krate) in source_roots { let source_root = db.source_root(source_root_id).source_root(db); for file_id in source_root.iter() { if let Some(p) = source_root.path_for_file(&file_id) @@ -148,7 +148,8 @@ impl flags::AnalysisStats { let length = db.file_text(file_id).text(db).lines().count(); let item_stats = db .file_item_tree( - EditionedFileId::current_edition_guess_origin(db, file_id).into(), + EditionedFileId::current_edition(db, file_id).into(), + krate.into(), ) .item_tree_stats() .into(); @@ -160,7 +161,8 @@ impl flags::AnalysisStats { let length = db.file_text(file_id).text(db).lines().count(); let item_stats = db .file_item_tree( - EditionedFileId::current_edition_guess_origin(db, file_id).into(), + EditionedFileId::current_edition(db, file_id).into(), + krate.into(), ) .item_tree_stats() .into(); @@ -492,7 +494,7 @@ impl flags::AnalysisStats { let mut sw = self.stop_watch(); for &file_id in file_ids { - let file_id = file_id.editioned_file_id(db); + let file_id = file_id.span_file_id(db); let sema = hir::Semantics::new(db); let display_target = match sema.first_crate(file_id.file_id()) { Some(krate) => krate.to_display_target(sema.db), diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 5c69bda723..6bc0792daa 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs @@ -74,7 +74,7 @@ impl flags::Search { let sr = db.source_root(root).source_root(db); for file_id in sr.iter() { for debug_info in match_finder.debug_where_text_equal( - EditionedFileId::current_edition_guess_origin(db, file_id), + EditionedFileId::current_edition(db, file_id), debug_snippet, ) { println!("{debug_info:#?}"); diff --git a/crates/test-fixture/src/lib.rs b/crates/test-fixture/src/lib.rs index ca68edd88c..e271c32c86 100644 --- a/crates/test-fixture/src/lib.rs +++ b/crates/test-fixture/src/lib.rs @@ -149,8 +149,8 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static { let fixture = ChangeFixture::parse(ra_fixture); fixture.change.apply(&mut db); assert_eq!(fixture.files.len(), 1, "Multiple file found in the fixture"); - let file = EditionedFileId::from_span_guess_origin(&db, fixture.files[0]); - (db, file) + let file_id = EditionedFileId::from_span_file_id(&db, fixture.files[0]); + (db, file_id) } /// See the trait documentation for more information on fixtures. @@ -165,7 +165,7 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static { let files = fixture .files .into_iter() - .map(|file| EditionedFileId::from_span_guess_origin(&db, file)) + .map(|file| EditionedFileId::from_span_file_id(&db, file)) .collect(); (db, files) } @@ -222,7 +222,7 @@ pub trait WithFixture: Default + ExpandDatabase + SourceDatabase + 'static { let (file_id, range_or_offset) = fixture .file_position .expect("Could not find file position in fixture. Did you forget to add an `$0`?"); - let file_id = EditionedFileId::from_span_guess_origin(&db, file_id); + let file_id = EditionedFileId::from_span_file_id(&db, file_id); (db, file_id, range_or_offset) } |