Skip to content

Commit dc7c6d4

Browse files
committed
Slightly shrink DefMap
1 parent 117f9b7 commit dc7c6d4

File tree

22 files changed

+130
-92
lines changed

22 files changed

+130
-92
lines changed

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

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

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/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);

crates/hir-def/src/nameres.rs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ use crate::{
9494
pub struct DefMap {
9595
_c: Count<Self>,
9696
block: Option<BlockInfo>,
97-
root: LocalModuleId,
9897
modules: Arena<ModuleData>,
9998
krate: CrateId,
10099
/// The prelude module for this crate. This either comes from an import
@@ -141,7 +140,19 @@ struct BlockInfo {
141140
/// The `BlockId` this `DefMap` was created from.
142141
block: BlockId,
143142
/// The containing module.
144-
parent: ModuleId,
143+
parent: BlockRelativeModuleId,
144+
}
145+
146+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
147+
struct BlockRelativeModuleId {
148+
block: Option<BlockId>,
149+
local_id: LocalModuleId,
150+
}
151+
152+
impl BlockRelativeModuleId {
153+
fn def_map(self, db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
154+
ModuleId { krate, block: self.block, local_id: self.local_id }.def_map(db)
155+
}
145156
}
146157

147158
impl std::ops::Index<LocalModuleId> for DefMap {
@@ -231,6 +242,8 @@ pub struct ModuleData {
231242
}
232243

233244
impl DefMap {
245+
pub const ROOT: LocalModuleId = LocalModuleId::from_raw(la_arena::RawIdx::from_u32(0));
246+
234247
pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
235248
let _p = profile::span("crate_def_map_query").detail(|| {
236249
db.crate_graph()[krate].display_name.as_deref().unwrap_or_default().to_string()
@@ -266,7 +279,13 @@ impl DefMap {
266279
ModuleData::new(ModuleOrigin::BlockExpr { block: block.ast_id }, visibility);
267280

268281
let mut def_map = DefMap::empty(krate, parent_map.edition, module_data);
269-
def_map.block = Some(BlockInfo { block: block_id, parent: block.module });
282+
def_map.block = Some(BlockInfo {
283+
block: block_id,
284+
parent: BlockRelativeModuleId {
285+
block: block.module.block,
286+
local_id: block.module.local_id,
287+
},
288+
});
270289

271290
let def_map = collector::collect_defs(db, def_map, tree_id);
272291
Arc::new(def_map)
@@ -275,6 +294,7 @@ impl DefMap {
275294
fn empty(krate: CrateId, edition: Edition, module_data: ModuleData) -> DefMap {
276295
let mut modules: Arena<ModuleData> = Arena::default();
277296
let root = modules.alloc(module_data);
297+
assert_eq!(root, Self::ROOT);
278298

279299
DefMap {
280300
_c: Count::new(),
@@ -289,7 +309,6 @@ impl DefMap {
289309
proc_macro_loading_error: None,
290310
derive_helpers_in_scope: FxHashMap::default(),
291311
prelude: None,
292-
root,
293312
modules,
294313
registered_attrs: Vec::new(),
295314
registered_tools: Vec::new(),
@@ -339,10 +358,6 @@ impl DefMap {
339358
self.no_std || self.no_core
340359
}
341360

342-
pub fn root(&self) -> LocalModuleId {
343-
self.root
344-
}
345-
346361
pub fn fn_as_proc_macro(&self, id: FunctionId) -> Option<ProcMacroId> {
347362
self.fn_proc_macro_mapping.get(&id).copied()
348363
}
@@ -377,9 +392,9 @@ impl DefMap {
377392
}
378393

379394
pub(crate) fn crate_root(&self, db: &dyn DefDatabase) -> ModuleId {
380-
self.with_ancestor_maps(db, self.root, &mut |def_map, _module| {
395+
self.with_ancestor_maps(db, Self::ROOT, &mut |def_map, _module| {
381396
if def_map.block.is_none() {
382-
Some(def_map.module_id(def_map.root))
397+
Some(def_map.module_id(Self::ROOT))
383398
} else {
384399
None
385400
}
@@ -439,7 +454,7 @@ impl DefMap {
439454
}
440455
let mut block = self.block;
441456
while let Some(block_info) = block {
442-
let parent = block_info.parent.def_map(db);
457+
let parent = block_info.parent.def_map(db, self.krate);
443458
if let Some(it) = f(&parent, block_info.parent.local_id) {
444459
return Some(it);
445460
}
@@ -452,15 +467,22 @@ impl DefMap {
452467
/// If this `DefMap` is for a block expression, returns the module containing the block (which
453468
/// might again be a block, or a module inside a block).
454469
pub fn parent(&self) -> Option<ModuleId> {
455-
Some(self.block?.parent)
470+
let BlockRelativeModuleId { block, local_id } = self.block?.parent;
471+
Some(ModuleId { krate: self.krate, block, local_id })
456472
}
457473

458474
/// Returns the module containing `local_mod`, either the parent `mod`, or the module (or block) containing
459475
/// the block, if `self` corresponds to a block expression.
460476
pub fn containing_module(&self, local_mod: LocalModuleId) -> Option<ModuleId> {
461477
match self[local_mod].parent {
462478
Some(parent) => Some(self.module_id(parent)),
463-
None => self.block.map(|block| block.parent),
479+
None => {
480+
self.block.map(
481+
|BlockInfo { parent: BlockRelativeModuleId { block, local_id }, .. }| {
482+
ModuleId { krate: self.krate, block, local_id }
483+
},
484+
)
485+
}
464486
}
465487
}
466488

@@ -471,12 +493,12 @@ impl DefMap {
471493
let mut arc;
472494
let mut current_map = self;
473495
while let Some(block) = current_map.block {
474-
go(&mut buf, db, current_map, "block scope", current_map.root);
496+
go(&mut buf, db, current_map, "block scope", Self::ROOT);
475497
buf.push('\n');
476-
arc = block.parent.def_map(db);
498+
arc = block.parent.def_map(db, self.krate);
477499
current_map = &arc;
478500
}
479-
go(&mut buf, db, current_map, "crate", current_map.root);
501+
go(&mut buf, db, current_map, "crate", Self::ROOT);
480502
return buf;
481503

482504
fn go(
@@ -506,7 +528,7 @@ impl DefMap {
506528
let mut current_map = self;
507529
while let Some(block) = current_map.block {
508530
format_to!(buf, "{:?} in {:?}\n", block.block, block.parent);
509-
arc = block.parent.def_map(db);
531+
arc = block.parent.def_map(db, self.krate);
510532
current_map = &arc;
511533
}
512534

@@ -534,7 +556,6 @@ impl DefMap {
534556
recursion_limit: _,
535557
krate: _,
536558
prelude: _,
537-
root: _,
538559
rustc_coherence_is_core: _,
539560
no_core: _,
540561
no_std: _,

crates/hir-def/src/nameres/collector.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
7171
for dep in &krate.dependencies {
7272
tracing::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id);
7373
let dep_def_map = db.crate_def_map(dep.crate_id);
74-
let dep_root = dep_def_map.module_id(dep_def_map.root);
74+
let dep_root = dep_def_map.module_id(DefMap::ROOT);
7575

7676
deps.insert(dep.as_name(), dep_root);
7777

@@ -287,7 +287,7 @@ impl DefCollector<'_> {
287287

288288
let file_id = self.db.crate_graph()[self.def_map.krate].root_file_id;
289289
let item_tree = self.db.file_item_tree(file_id.into());
290-
let module_id = self.def_map.root;
290+
let module_id = DefMap::ROOT;
291291

292292
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
293293

@@ -382,7 +382,7 @@ impl DefCollector<'_> {
382382

383383
fn seed_with_inner(&mut self, tree_id: TreeId) {
384384
let item_tree = tree_id.item_tree(self.db);
385-
let module_id = self.def_map.root;
385+
let module_id = DefMap::ROOT;
386386

387387
let is_cfg_enabled = item_tree
388388
.top_level_attrs(self.db, self.def_map.krate)
@@ -464,7 +464,7 @@ impl DefCollector<'_> {
464464
// Additionally, while the proc macro entry points must be `pub`, they are not publicly
465465
// exported in type/value namespace. This function reduces the visibility of all items
466466
// in the crate root that aren't proc macros.
467-
let root = self.def_map.root;
467+
let root = DefMap::ROOT;
468468
let module_id = self.def_map.module_id(root);
469469
let root = &mut self.def_map.modules[root];
470470
root.scope.censor_non_proc_macros(module_id);
@@ -560,13 +560,8 @@ impl DefCollector<'_> {
560560
};
561561
let path = ModPath::from_segments(path_kind, [krate, name![prelude], edition]);
562562

563-
let (per_ns, _) = self.def_map.resolve_path(
564-
self.db,
565-
self.def_map.root,
566-
&path,
567-
BuiltinShadowMode::Other,
568-
None,
569-
);
563+
let (per_ns, _) =
564+
self.def_map.resolve_path(self.db, DefMap::ROOT, &path, BuiltinShadowMode::Other, None);
570565

571566
match per_ns.types {
572567
Some((ModuleDefId::ModuleId(m), _)) => {
@@ -661,7 +656,7 @@ impl DefCollector<'_> {
661656
// In Rust, `#[macro_export]` macros are unconditionally visible at the
662657
// crate root, even if the parent modules is **not** visible.
663658
if export {
664-
let module_id = self.def_map.root;
659+
let module_id = DefMap::ROOT;
665660
self.def_map.modules[module_id].scope.declare(macro_.into());
666661
self.update(
667662
module_id,
@@ -712,7 +707,7 @@ impl DefCollector<'_> {
712707
/// A proc macro is similar to normal macro scope, but it would not visible in legacy textual scoped.
713708
/// And unconditionally exported.
714709
fn define_proc_macro(&mut self, name: Name, macro_: ProcMacroId) {
715-
let module_id = self.def_map.root;
710+
let module_id = DefMap::ROOT;
716711
self.def_map.modules[module_id].scope.declare(macro_.into());
717712
self.update(
718713
module_id,
@@ -732,7 +727,7 @@ impl DefCollector<'_> {
732727
let def_map = self.db.crate_def_map(krate);
733728
// `#[macro_use]` brings macros into macro_use prelude. Yes, even non-`macro_rules!`
734729
// macros.
735-
let root_scope = &def_map[def_map.root].scope;
730+
let root_scope = &def_map[DefMap::ROOT].scope;
736731
if let Some(names) = names {
737732
for name in names {
738733
// FIXME: Report diagnostic on 404.
@@ -834,9 +829,9 @@ impl DefCollector<'_> {
834829
let root = match self.def_map.block {
835830
Some(_) => {
836831
let def_map = self.def_map.crate_root(self.db).def_map(self.db);
837-
def_map.module_id(def_map.root())
832+
def_map.module_id(DefMap::ROOT)
838833
}
839-
None => self.def_map.module_id(self.def_map.root()),
834+
None => self.def_map.module_id(DefMap::ROOT),
840835
};
841836
Some(root)
842837
} else {
@@ -879,7 +874,7 @@ impl DefCollector<'_> {
879874
// extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658
880875
if import.is_extern_crate
881876
&& self.def_map.block.is_none()
882-
&& module_id == self.def_map.root
877+
&& module_id == DefMap::ROOT
883878
{
884879
if let (Some(ModuleDefId::ModuleId(def)), Some(name)) = (def.take_types(), name)
885880
{
@@ -1525,7 +1520,7 @@ impl ModCollector<'_, '_> {
15251520

15261521
fn collect(&mut self, items: &[ModItem], container: ItemContainerId) {
15271522
let krate = self.def_collector.def_map.krate;
1528-
let is_crate_root = self.module_id == self.def_collector.def_map.root;
1523+
let is_crate_root = self.module_id == DefMap::ROOT;
15291524

15301525
// Note: don't assert that inserted value is fresh: it's simply not true
15311526
// for macros.
@@ -1641,9 +1636,9 @@ impl ModCollector<'_, '_> {
16411636
FunctionLoc { container, id: ItemTreeId::new(self.tree_id, id) }.intern(db);
16421637

16431638
let vis = resolve_vis(def_map, &self.item_tree[it.visibility]);
1644-
if self.def_collector.is_proc_macro && self.module_id == def_map.root {
1639+
if self.def_collector.is_proc_macro && self.module_id == DefMap::ROOT {
16451640
if let Some(proc_macro) = attrs.parse_proc_macro_decl(&it.name) {
1646-
let crate_root = def_map.module_id(def_map.root);
1641+
let crate_root = def_map.module_id(DefMap::ROOT);
16471642
self.def_collector.export_proc_macro(
16481643
proc_macro,
16491644
ItemTreeId::new(self.tree_id, id),

0 commit comments

Comments
 (0)