Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0b4c09b

Browse files
committed
Auto merge of rust-lang#14941 - Veykril:def-map, r=Veykril
Shrink `DefMap`, share crate level items with block def maps
2 parents 4458e7f + 11b9371 commit 0b4c09b

File tree

25 files changed

+294
-231
lines changed

25 files changed

+294
-231
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ impl ExprCollector<'_> {
10751075
match block_id.map(|block_id| (self.db.block_def_map(block_id), block_id)) {
10761076
Some((def_map, block_id)) => {
10771077
self.body.block_scopes.push(block_id);
1078-
(def_map.module_id(def_map.root()), def_map)
1078+
(def_map.module_id(DefMap::ROOT), def_map)
10791079
}
10801080
None => (self.expander.module, self.def_map.clone()),
10811081
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ fn f() {
148148
}
149149
"#,
150150
expect![[r#"
151-
BlockId(1) in ModuleId { krate: Idx::<CrateData>(0), block: Some(BlockId(0)), local_id: Idx::<ModuleData>(1) }
152-
BlockId(0) in ModuleId { krate: Idx::<CrateData>(0), block: None, local_id: Idx::<ModuleData>(0) }
151+
BlockId(1) in BlockRelativeModuleId { block: Some(BlockId(0)), local_id: Idx::<ModuleData>(1) }
152+
BlockId(0) in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
153153
crate scope
154154
"#]],
155155
);

crates/hir-def/src/child_by_source.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
db::DefDatabase,
1313
dyn_map::{keys, DynMap},
1414
item_scope::ItemScope,
15+
nameres::DefMap,
1516
src::{HasChildSource, HasSource},
1617
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, FieldId, ImplId, Lookup, MacroId,
1718
ModuleDefId, ModuleId, TraitId, VariantId,
@@ -205,7 +206,7 @@ impl ChildBySource for DefWithBodyId {
205206
for (_, def_map) in body.blocks(db) {
206207
// All block expressions are merged into the same map, because they logically all add
207208
// inner items to the containing `DefWithBodyId`.
208-
def_map[def_map.root()].scope.child_by_source_to(db, res, file_id);
209+
def_map[DefMap::ROOT].scope.child_by_source_to(db, res, file_id);
209210
}
210211
}
211212
}

crates/hir-def/src/db.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
198198

199199
// endregion:attrs
200200

201-
#[salsa::invoke(LangItems::crate_lang_items_query)]
202-
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
203-
204201
#[salsa::invoke(LangItems::lang_item_query)]
205202
fn lang_item(&self, start_crate: CrateId, item: LangItem) -> Option<LangItemTarget>;
206203

207204
#[salsa::invoke(ImportMap::import_map_query)]
208205
fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
209206

207+
// region:visibilities
208+
210209
#[salsa::invoke(visibility::field_visibilities_query)]
211210
fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
212211

@@ -217,8 +216,14 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
217216
#[salsa::invoke(visibility::const_visibility_query)]
218217
fn const_visibility(&self, def: ConstId) -> Visibility;
219218

219+
// endregion:visibilities
220+
221+
#[salsa::invoke(LangItems::crate_lang_items_query)]
222+
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
223+
220224
#[salsa::transparent]
221225
fn crate_limits(&self, crate_id: CrateId) -> CrateLimits;
226+
222227
#[salsa::transparent]
223228
fn recursion_limit(&self, crate_id: CrateId) -> u32;
224229

crates/hir-def/src/find_path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn find_path_inner(
8181
}
8282

8383
let def_map = from.def_map(db);
84-
let crate_root = def_map.crate_root(db);
84+
let crate_root = def_map.crate_root();
8585
// - if the item is a module, jump straight to module search
8686
if let ItemInNs::Types(ModuleDefId::ModuleId(module_id)) = item {
8787
let mut visited_modules = FxHashSet::default();
@@ -454,7 +454,7 @@ fn find_local_import_locations(
454454
worklist.push(ancestor);
455455
}
456456

457-
let def_map = def_map.crate_root(db).def_map(db);
457+
let def_map = def_map.crate_root().def_map(db);
458458

459459
let mut seen: FxHashSet<_> = FxHashSet::default();
460460

crates/hir-def/src/import_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use rustc_hash::{FxHashSet, FxHasher};
1111
use triomphe::Arc;
1212

1313
use crate::{
14-
db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
15-
ModuleId, TraitId,
14+
db::DefDatabase, item_scope::ItemInNs, nameres::DefMap, visibility::Visibility, AssocItemId,
15+
ModuleDefId, ModuleId, TraitId,
1616
};
1717

1818
type FxIndexMap<K, V> = IndexMap<K, V, BuildHasherDefault<FxHasher>>;
@@ -183,7 +183,7 @@ fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
183183

184184
// We look only into modules that are public(ly reexported), starting with the crate root.
185185
let empty = ImportPath { segments: vec![] };
186-
let root = def_map.module_id(def_map.root());
186+
let root = def_map.module_id(DefMap::ROOT);
187187
let mut worklist = vec![(root, empty)];
188188
while let Some((module, mod_path)) = worklist.pop() {
189189
let ext_def_map;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use tt::token_id::{Subtree, TokenId};
3535
use crate::{
3636
db::DefDatabase,
3737
macro_id_to_def_id,
38-
nameres::{MacroSubNs, ModuleSource},
38+
nameres::{DefMap, MacroSubNs, ModuleSource},
3939
resolver::HasResolver,
4040
src::HasSource,
4141
test_db::TestDB,
@@ -61,7 +61,7 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
6161
let db = TestDB::with_files_extra_proc_macros(ra_fixture, extra_proc_macros);
6262
let krate = db.crate_graph().iter().next().unwrap();
6363
let def_map = db.crate_def_map(krate);
64-
let local_id = def_map.root();
64+
let local_id = DefMap::ROOT;
6565
let module = def_map.module_id(local_id);
6666
let resolver = module.resolver(&db);
6767
let source = def_map[local_id].definition_source(&db);

0 commit comments

Comments
 (0)