Skip to content

Commit 5816462

Browse files
committed
refactor: De-arc defmap queries
1 parent b1bd478 commit 5816462

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+498
-455
lines changed

crates/hir-def/src/db.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::{
2424
item_tree::{AttrOwner, ItemTree},
2525
lang_item::{self, LangItem},
2626
nameres::{
27-
DefMap, LocalDefMap,
2827
assoc::{ImplItems, TraitItems},
28+
crate_def_map,
2929
diagnostics::DefDiagnostics,
3030
},
3131
signatures::{
@@ -111,16 +111,6 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + SourceDatabase {
111111
#[salsa::invoke(ItemTree::block_item_tree_query)]
112112
fn block_item_tree(&self, block_id: BlockId) -> Arc<ItemTree>;
113113

114-
#[salsa::invoke(DefMap::crate_local_def_map_query)]
115-
fn crate_local_def_map(&self, krate: Crate) -> (Arc<DefMap>, Arc<LocalDefMap>);
116-
117-
#[salsa::invoke(DefMap::crate_def_map_query)]
118-
fn crate_def_map(&self, krate: Crate) -> Arc<DefMap>;
119-
120-
/// Computes the block-level `DefMap`.
121-
#[salsa::invoke(DefMap::block_def_map_query)]
122-
fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
123-
124114
/// Turns a MacroId into a MacroDefId, describing the macro's definition post name resolution.
125115
#[salsa::invoke(macro_def)]
126116
fn macro_def(&self, m: MacroId) -> MacroDefId;
@@ -363,7 +353,7 @@ fn include_macro_invoc(
363353
db: &dyn DefDatabase,
364354
krate: Crate,
365355
) -> Arc<[(MacroCallId, EditionedFileId)]> {
366-
db.crate_def_map(krate)
356+
crate_def_map(db, krate)
367357
.modules
368358
.values()
369359
.flat_map(|m| m.scope.iter_macro_invoc())

crates/hir-def/src/expr_store.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_hash::FxHashMap;
1919
use smallvec::SmallVec;
2020
use span::{Edition, SyntaxContext};
2121
use syntax::{AstPtr, SyntaxNodePtr, ast};
22-
use triomphe::Arc;
2322
use tt::TextRange;
2423

2524
use crate::{
@@ -30,7 +29,7 @@ use crate::{
3029
Array, AsmOperand, Binding, BindingId, Expr, ExprId, ExprOrPatId, Label, LabelId, Pat,
3130
PatId, RecordFieldPat, Statement,
3231
},
33-
nameres::DefMap,
32+
nameres::{DefMap, block_def_map},
3433
type_ref::{LifetimeRef, LifetimeRefId, PathId, TypeRef, TypeRefId},
3534
};
3635

@@ -225,8 +224,8 @@ impl ExpressionStore {
225224
pub fn blocks<'a>(
226225
&'a self,
227226
db: &'a dyn DefDatabase,
228-
) -> impl Iterator<Item = (BlockId, Arc<DefMap>)> + 'a {
229-
self.block_scopes.iter().map(move |&block| (block, db.block_def_map(block)))
227+
) -> impl Iterator<Item = (BlockId, &'a DefMap)> + 'a {
228+
self.block_scopes.iter().map(move |&block| (block, block_def_map(db, block)))
230229
}
231230

232231
pub fn walk_bindings_in_pat(&self, pat_id: PatId, mut f: impl FnMut(BindingId)) {

crates/hir-def/src/expr_store/lower.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::{
5656
item_scope::BuiltinShadowMode,
5757
item_tree::FieldsShape,
5858
lang_item::LangItem,
59-
nameres::{DefMap, LocalDefMap, MacroSubNs},
59+
nameres::{DefMap, LocalDefMap, MacroSubNs, block_def_map},
6060
type_ref::{
6161
ArrayType, ConstRef, FnType, LifetimeRef, LifetimeRefId, Mutability, PathId, Rawness,
6262
RefType, TraitBoundModifier, TraitRef, TypeBound, TypeRef, TypeRefId, UseArgRef,
@@ -436,8 +436,8 @@ pub struct ExprCollector<'db> {
436436
db: &'db dyn DefDatabase,
437437
cfg_options: &'db CfgOptions,
438438
expander: Expander,
439-
def_map: Arc<DefMap>,
440-
local_def_map: Arc<LocalDefMap>,
439+
def_map: &'db DefMap,
440+
local_def_map: &'db LocalDefMap,
441441
module: ModuleId,
442442
pub store: ExpressionStoreBuilder,
443443
pub(crate) source_map: ExpressionStoreSourceMap,
@@ -544,7 +544,7 @@ impl ExprCollector<'_> {
544544
current_file_id: HirFileId,
545545
) -> ExprCollector<'_> {
546546
let (def_map, local_def_map) = module.local_def_map(db);
547-
let expander = Expander::new(db, current_file_id, &def_map);
547+
let expander = Expander::new(db, current_file_id, def_map);
548548
ExprCollector {
549549
db,
550550
cfg_options: module.krate().cfg_options(db),
@@ -1947,7 +1947,7 @@ impl ExprCollector<'_> {
19471947
let resolver = |path: &_| {
19481948
self.def_map
19491949
.resolve_path(
1950-
&self.local_def_map,
1950+
self.local_def_map,
19511951
self.db,
19521952
module,
19531953
path,
@@ -2163,12 +2163,12 @@ impl ExprCollector<'_> {
21632163
};
21642164

21652165
let (module, def_map) =
2166-
match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
2166+
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
21672167
Some((def_map, block_id)) => {
21682168
self.store.block_scopes.push(block_id);
21692169
(def_map.module_id(DefMap::ROOT), def_map)
21702170
}
2171-
None => (self.module, self.def_map.clone()),
2171+
None => (self.module, self.def_map),
21722172
};
21732173
let prev_def_map = mem::replace(&mut self.def_map, def_map);
21742174
let prev_local_module = mem::replace(&mut self.module, module);
@@ -2247,7 +2247,7 @@ impl ExprCollector<'_> {
22472247
// This could also be a single-segment path pattern. To
22482248
// decide that, we need to try resolving the name.
22492249
let (resolved, _) = self.def_map.resolve_path(
2250-
&self.local_def_map,
2250+
self.local_def_map,
22512251
self.db,
22522252
self.module.local_id,
22532253
&name.clone().into(),

crates/hir-def/src/expr_store/lower/path/tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use syntax::ast::{self, make};
44
use test_fixture::WithFixture;
55

66
use crate::{
7-
db::DefDatabase,
87
expr_store::{
98
ExpressionStore,
109
lower::{
@@ -14,13 +13,15 @@ use crate::{
1413
path::Path,
1514
pretty,
1615
},
16+
nameres::crate_def_map,
1717
test_db::TestDB,
1818
};
1919

2020
fn lower_path(path: ast::Path) -> (TestDB, ExpressionStore, Option<Path>) {
2121
let (db, file_id) = TestDB::with_single_file("");
2222
let krate = db.fetch_test_crate();
23-
let mut ctx = ExprCollector::new(&db, db.crate_def_map(krate).root_module_id(), file_id.into());
23+
let mut ctx =
24+
ExprCollector::new(&db, crate_def_map(&db, krate).root_module_id(), file_id.into());
2425
let lowered_path = ctx.lower_path(path, &mut ExprCollector::impl_trait_allocator);
2526
let store = ctx.store.finish();
2627
(db, store, lowered_path)

crates/hir-def/src/expr_store/scope.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,13 @@ mod tests {
324324
use test_fixture::WithFixture;
325325
use test_utils::{assert_eq_text, extract_offset};
326326

327-
use crate::{FunctionId, ModuleDefId, db::DefDatabase, test_db::TestDB};
327+
use crate::{
328+
FunctionId, ModuleDefId, db::DefDatabase, nameres::crate_def_map, test_db::TestDB,
329+
};
328330

329331
fn find_function(db: &TestDB, file_id: FileId) -> FunctionId {
330332
let krate = db.test_crate();
331-
let crate_def_map = db.crate_def_map(krate);
333+
let crate_def_map = crate_def_map(db, krate);
332334

333335
let module = crate_def_map.modules_for_file(db, file_id).next().unwrap();
334336
let (_, def) = crate_def_map[module].scope.entries().next().unwrap();

crates/hir-def/src/expr_store/tests/body.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
mod block;
22

3-
use crate::{DefWithBodyId, ModuleDefId, hir::MatchArm, test_db::TestDB};
3+
use crate::{DefWithBodyId, ModuleDefId, hir::MatchArm, nameres::crate_def_map, test_db::TestDB};
44
use expect_test::{Expect, expect};
55
use la_arena::RawIdx;
66
use test_fixture::WithFixture;
7+
use triomphe::Arc;
78

89
use super::super::*;
910

1011
fn lower(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> (TestDB, Arc<Body>, DefWithBodyId) {
1112
let db = TestDB::with_files(ra_fixture);
1213

1314
let krate = db.fetch_test_crate();
14-
let def_map = db.crate_def_map(krate);
15+
let def_map = crate_def_map(&db, krate);
1516
let mut fn_def = None;
1617
'outer: for (_, module) in def_map.modules() {
1718
for decl in module.scope.declarations() {

crates/hir-def/src/expr_store/tests/body/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ fn f() {
189189
}
190190
"#,
191191
expect![[r#"
192-
BlockId(3801) in BlockRelativeModuleId { block: Some(BlockId(3800)), local_id: Idx::<ModuleData>(1) }
193-
BlockId(3800) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
192+
BlockId(3c01) in BlockRelativeModuleId { block: Some(BlockId(3c00)), local_id: Idx::<ModuleData>(1) }
193+
BlockId(3c00) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
194194
crate scope
195195
"#]],
196196
);

crates/hir-def/src/expr_store/tests/signatures.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
22
GenericDefId, ModuleDefId,
33
expr_store::pretty::{print_function, print_struct},
4+
nameres::crate_def_map,
45
test_db::TestDB,
56
};
67
use expect_test::{Expect, expect};
@@ -12,7 +13,7 @@ fn lower_and_print(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expe
1213
let db = TestDB::with_files(ra_fixture);
1314

1415
let krate = db.fetch_test_crate();
15-
let def_map = db.crate_def_map(krate);
16+
let def_map = crate_def_map(&db, krate);
1617
let mut defs = vec![];
1718
for (_, module) in def_map.modules() {
1819
for decl in module.scope.declarations() {

crates/hir-def/src/find_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn find_path(
5252
ignore_local_imports,
5353
is_std_item: item_module.krate().data(db).origin.is_lang(),
5454
from,
55-
from_def_map: &from.def_map(db),
55+
from_def_map: from.def_map(db),
5656
fuel: Cell::new(FIND_PATH_FUEL),
5757
},
5858
item,
@@ -691,7 +691,7 @@ mod tests {
691691
let (def_map, local_def_map) = module.local_def_map(&db);
692692
let resolved = def_map
693693
.resolve_path(
694-
&local_def_map,
694+
local_def_map,
695695
&db,
696696
module.local_id,
697697
&mod_path,

crates/hir-def/src/import_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
AssocItemId, AttrDefId, Complete, FxIndexMap, ModuleDefId, ModuleId, TraitId,
1717
db::DefDatabase,
1818
item_scope::{ImportOrExternCrate, ItemInNs},
19-
nameres::DefMap,
19+
nameres::{DefMap, crate_def_map},
2020
visibility::Visibility,
2121
};
2222

@@ -129,7 +129,7 @@ impl ImportMap {
129129
fn collect_import_map(db: &dyn DefDatabase, krate: Crate) -> ImportMapIndex {
130130
let _p = tracing::info_span!("collect_import_map").entered();
131131

132-
let def_map = db.crate_def_map(krate);
132+
let def_map = crate_def_map(db, krate);
133133
let mut map = FxIndexMap::default();
134134

135135
// We look only into modules that are public(ly reexported), starting with the crate root.

crates/hir-def/src/lang_item.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use triomphe::Arc;
1010
use crate::{
1111
AdtId, AssocItemId, AttrDefId, Crate, EnumId, EnumVariantId, FunctionId, ImplId, ModuleDefId,
1212
StaticId, StructId, TraitId, TypeAliasId, UnionId, db::DefDatabase, expr_store::path::Path,
13+
nameres::crate_def_map,
1314
};
1415

1516
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -90,7 +91,7 @@ pub fn crate_lang_items(db: &dyn DefDatabase, krate: Crate) -> Option<Box<LangIt
9091

9192
let mut lang_items = LangItems::default();
9293

93-
let crate_def_map = db.crate_def_map(krate);
94+
let crate_def_map = crate_def_map(db, krate);
9495

9596
for (_, module_data) in crate_def_map.modules() {
9697
for impl_def in module_data.scope.impls() {
@@ -209,7 +210,7 @@ pub(crate) fn crate_notable_traits(db: &dyn DefDatabase, krate: Crate) -> Option
209210

210211
let mut traits = Vec::new();
211212

212-
let crate_def_map = db.crate_def_map(krate);
213+
let crate_def_map = crate_def_map(db, krate);
213214

214215
for (_, module_data) in crate_def_map.modules() {
215216
for def in module_data.scope.declarations() {

crates/hir-def/src/lib.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ use crate::{
9292
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
9393
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
9494
},
95-
nameres::LocalDefMap,
95+
nameres::{LocalDefMap, block_def_map, crate_def_map, crate_local_def_map},
9696
signatures::VariantFields,
9797
};
9898

@@ -324,12 +324,13 @@ pub struct CrateRootModuleId {
324324
}
325325

326326
impl CrateRootModuleId {
327-
pub fn def_map(&self, db: &dyn DefDatabase) -> Arc<DefMap> {
328-
db.crate_def_map(self.krate)
327+
pub fn def_map(self, db: &dyn DefDatabase) -> &DefMap {
328+
crate_def_map(db, self.krate)
329329
}
330330

331-
pub(crate) fn local_def_map(&self, db: &dyn DefDatabase) -> (Arc<DefMap>, Arc<LocalDefMap>) {
332-
db.crate_local_def_map(self.krate)
331+
pub(crate) fn local_def_map(self, db: &dyn DefDatabase) -> (&DefMap, &LocalDefMap) {
332+
let def_map = crate_local_def_map(db, self.krate);
333+
(def_map.def_map(db), def_map.local(db))
333334
}
334335

335336
pub fn krate(self) -> Crate {
@@ -390,26 +391,29 @@ pub struct ModuleId {
390391
}
391392

392393
impl ModuleId {
393-
pub fn def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
394+
pub fn def_map(self, db: &dyn DefDatabase) -> &DefMap {
394395
match self.block {
395-
Some(block) => db.block_def_map(block),
396-
None => db.crate_def_map(self.krate),
396+
Some(block) => block_def_map(db, block),
397+
None => crate_def_map(db, self.krate),
397398
}
398399
}
399400

400-
pub(crate) fn local_def_map(self, db: &dyn DefDatabase) -> (Arc<DefMap>, Arc<LocalDefMap>) {
401+
pub(crate) fn local_def_map(self, db: &dyn DefDatabase) -> (&DefMap, &LocalDefMap) {
401402
match self.block {
402-
Some(block) => (db.block_def_map(block), self.only_local_def_map(db)),
403-
None => db.crate_local_def_map(self.krate),
403+
Some(block) => (block_def_map(db, block), self.only_local_def_map(db)),
404+
None => {
405+
let def_map = crate_local_def_map(db, self.krate);
406+
(def_map.def_map(db), def_map.local(db))
407+
}
404408
}
405409
}
406410

407-
pub(crate) fn only_local_def_map(self, db: &dyn DefDatabase) -> Arc<LocalDefMap> {
408-
db.crate_local_def_map(self.krate).1
411+
pub(crate) fn only_local_def_map(self, db: &dyn DefDatabase) -> &LocalDefMap {
412+
crate_local_def_map(db, self.krate).local(db)
409413
}
410414

411-
pub fn crate_def_map(self, db: &dyn DefDatabase) -> Arc<DefMap> {
412-
db.crate_def_map(self.krate)
415+
pub fn crate_def_map(self, db: &dyn DefDatabase) -> &DefMap {
416+
crate_def_map(db, self.krate)
413417
}
414418

415419
pub fn krate(self) -> Crate {

crates/hir-def/src/macro_expansion_tests/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use test_fixture::WithFixture;
3939
use crate::{
4040
AdtId, Lookup, ModuleDefId,
4141
db::DefDatabase,
42-
nameres::{DefMap, ModuleSource},
42+
nameres::{DefMap, ModuleSource, crate_def_map},
4343
src::HasSource,
4444
test_db::TestDB,
4545
tt::TopSubtree,
@@ -49,7 +49,7 @@ use crate::{
4949
fn check_errors(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
5050
let db = TestDB::with_files(ra_fixture);
5151
let krate = db.fetch_test_crate();
52-
let def_map = db.crate_def_map(krate);
52+
let def_map = crate_def_map(&db, krate);
5353
let errors = def_map
5454
.modules()
5555
.flat_map(|module| module.1.scope.all_macro_calls())
@@ -113,7 +113,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
113113

114114
let (body, sm) = db.body_with_source_map(body);
115115
if let Some(it) =
116-
body.blocks(db).find_map(|block| resolve(db, &block.1, ast_id, ast_ptr))
116+
body.blocks(db).find_map(|block| resolve(db, block.1, ast_id, ast_ptr))
117117
{
118118
return Some(it);
119119
}
@@ -127,7 +127,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
127127

128128
let db = TestDB::with_files_extra_proc_macros(ra_fixture, extra_proc_macros);
129129
let krate = db.fetch_test_crate();
130-
let def_map = db.crate_def_map(krate);
130+
let def_map = crate_def_map(&db, krate);
131131
let local_id = DefMap::ROOT;
132132
let source = def_map[local_id].definition_source(&db);
133133
let source_file = match source.value {
@@ -142,7 +142,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
142142
let ast_id = db.ast_id_map(source.file_id).ast_id(&macro_call_node);
143143
let ast_id = InFile::new(source.file_id, ast_id);
144144
let ptr = InFile::new(source.file_id, AstPtr::new(&macro_call_node));
145-
let macro_call_id = resolve(&db, &def_map, ast_id, ptr)
145+
let macro_call_id = resolve(&db, def_map, ast_id, ptr)
146146
.unwrap_or_else(|| panic!("unable to find semantic macro call {macro_call_node}"));
147147
let expansion_result = db.parse_macro_expansion(macro_call_id);
148148
expansions.push((macro_call_node.clone(), expansion_result));

0 commit comments

Comments
 (0)