refactor: De-arc defmap queries

This commit is contained in:
Lukas Wirth 2025-04-28 13:30:54 +02:00
parent b1bd478029
commit 581646236e
49 changed files with 498 additions and 455 deletions

View file

@ -56,7 +56,7 @@ use crate::{
item_scope::BuiltinShadowMode,
item_tree::FieldsShape,
lang_item::LangItem,
nameres::{DefMap, LocalDefMap, MacroSubNs},
nameres::{DefMap, LocalDefMap, MacroSubNs, block_def_map},
type_ref::{
ArrayType, ConstRef, FnType, LifetimeRef, LifetimeRefId, Mutability, PathId, Rawness,
RefType, TraitBoundModifier, TraitRef, TypeBound, TypeRef, TypeRefId, UseArgRef,
@ -436,8 +436,8 @@ pub struct ExprCollector<'db> {
db: &'db dyn DefDatabase,
cfg_options: &'db CfgOptions,
expander: Expander,
def_map: Arc<DefMap>,
local_def_map: Arc<LocalDefMap>,
def_map: &'db DefMap,
local_def_map: &'db LocalDefMap,
module: ModuleId,
pub store: ExpressionStoreBuilder,
pub(crate) source_map: ExpressionStoreSourceMap,
@ -544,7 +544,7 @@ impl ExprCollector<'_> {
current_file_id: HirFileId,
) -> ExprCollector<'_> {
let (def_map, local_def_map) = module.local_def_map(db);
let expander = Expander::new(db, current_file_id, &def_map);
let expander = Expander::new(db, current_file_id, def_map);
ExprCollector {
db,
cfg_options: module.krate().cfg_options(db),
@ -1947,7 +1947,7 @@ impl ExprCollector<'_> {
let resolver = |path: &_| {
self.def_map
.resolve_path(
&self.local_def_map,
self.local_def_map,
self.db,
module,
path,
@ -2163,12 +2163,12 @@ impl ExprCollector<'_> {
};
let (module, def_map) =
match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
Some((def_map, block_id)) => {
self.store.block_scopes.push(block_id);
(def_map.module_id(DefMap::ROOT), def_map)
}
None => (self.module, self.def_map.clone()),
None => (self.module, self.def_map),
};
let prev_def_map = mem::replace(&mut self.def_map, def_map);
let prev_local_module = mem::replace(&mut self.module, module);
@ -2247,7 +2247,7 @@ impl ExprCollector<'_> {
// This could also be a single-segment path pattern. To
// decide that, we need to try resolving the name.
let (resolved, _) = self.def_map.resolve_path(
&self.local_def_map,
self.local_def_map,
self.db,
self.module.local_id,
&name.clone().into(),

View file

@ -4,7 +4,6 @@ use syntax::ast::{self, make};
use test_fixture::WithFixture;
use crate::{
db::DefDatabase,
expr_store::{
ExpressionStore,
lower::{
@ -14,13 +13,15 @@ use crate::{
path::Path,
pretty,
},
nameres::crate_def_map,
test_db::TestDB,
};
fn lower_path(path: ast::Path) -> (TestDB, ExpressionStore, Option<Path>) {
let (db, file_id) = TestDB::with_single_file("");
let krate = db.fetch_test_crate();
let mut ctx = ExprCollector::new(&db, db.crate_def_map(krate).root_module_id(), file_id.into());
let mut ctx =
ExprCollector::new(&db, crate_def_map(&db, krate).root_module_id(), file_id.into());
let lowered_path = ctx.lower_path(path, &mut ExprCollector::impl_trait_allocator);
let store = ctx.store.finish();
(db, store, lowered_path)

View file

@ -324,11 +324,13 @@ mod tests {
use test_fixture::WithFixture;
use test_utils::{assert_eq_text, extract_offset};
use crate::{FunctionId, ModuleDefId, db::DefDatabase, test_db::TestDB};
use crate::{
FunctionId, ModuleDefId, db::DefDatabase, nameres::crate_def_map, test_db::TestDB,
};
fn find_function(db: &TestDB, file_id: FileId) -> FunctionId {
let krate = db.test_crate();
let crate_def_map = db.crate_def_map(krate);
let crate_def_map = crate_def_map(db, krate);
let module = crate_def_map.modules_for_file(db, file_id).next().unwrap();
let (_, def) = crate_def_map[module].scope.entries().next().unwrap();

View file

@ -1,9 +1,10 @@
mod block;
use crate::{DefWithBodyId, ModuleDefId, hir::MatchArm, test_db::TestDB};
use crate::{DefWithBodyId, ModuleDefId, hir::MatchArm, nameres::crate_def_map, test_db::TestDB};
use expect_test::{Expect, expect};
use la_arena::RawIdx;
use test_fixture::WithFixture;
use triomphe::Arc;
use super::super::*;
@ -11,7 +12,7 @@ fn lower(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> (TestDB, Arc<Body>,
let db = TestDB::with_files(ra_fixture);
let krate = db.fetch_test_crate();
let def_map = db.crate_def_map(krate);
let def_map = crate_def_map(&db, krate);
let mut fn_def = None;
'outer: for (_, module) in def_map.modules() {
for decl in module.scope.declarations() {

View file

@ -189,8 +189,8 @@ fn f() {
}
"#,
expect![[r#"
BlockId(3801) in BlockRelativeModuleId { block: Some(BlockId(3800)), local_id: Idx::<ModuleData>(1) }
BlockId(3800) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
BlockId(3c01) in BlockRelativeModuleId { block: Some(BlockId(3c00)), local_id: Idx::<ModuleData>(1) }
BlockId(3c00) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
crate scope
"#]],
);

View file

@ -1,6 +1,7 @@
use crate::{
GenericDefId, ModuleDefId,
expr_store::pretty::{print_function, print_struct},
nameres::crate_def_map,
test_db::TestDB,
};
use expect_test::{Expect, expect};
@ -12,7 +13,7 @@ fn lower_and_print(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expe
let db = TestDB::with_files(ra_fixture);
let krate = db.fetch_test_crate();
let def_map = db.crate_def_map(krate);
let def_map = crate_def_map(&db, krate);
let mut defs = vec![];
for (_, module) in def_map.modules() {
for decl in module.scope.declarations() {