Skip to content

Commit dbef120

Browse files
committed
Turn BlockLoc into a tracked struct
1 parent ee2d044 commit dbef120

File tree

14 files changed

+172
-145
lines changed

14 files changed

+172
-145
lines changed

crates/hir-def/src/db.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use la_arena::ArenaMap;
88
use triomphe::Arc;
99

1010
use crate::{
11-
AssocItemId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc,
12-
EnumVariantId, EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc,
13-
FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc,
14-
MacroExpander, MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId,
15-
ProcMacroLoc, StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId,
16-
TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc, VariantId,
11+
AssocItemId, AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, EnumVariantId,
12+
EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId, ExternCrateLoc, FunctionId,
13+
FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalFieldId, Macro2Id, Macro2Loc, MacroExpander,
14+
MacroId, MacroRulesId, MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId,
15+
StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
16+
UnionLoc, UseId, UseLoc, VariantId,
1717
attrs::AttrFlags,
1818
expr_store::{
1919
Body, BodySourceMap, ExpressionStore, ExpressionStoreSourceMap, scope::ExprScopes,
@@ -82,9 +82,6 @@ pub trait InternDatabase: RootQueryDb {
8282
#[salsa::interned]
8383
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
8484
// endregion: items
85-
86-
#[salsa::interned]
87-
fn intern_block(&self, loc: BlockLoc) -> BlockId;
8885
}
8986

9087
#[query_group::query_group]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use triomphe::Arc;
3131
use tt::TextRange;
3232

3333
use crate::{
34-
AdtId, BlockId, BlockLoc, DefWithBodyId, FunctionId, GenericDefId, ImplId, MacroId,
34+
AdtId, BlockId, BlockIdLt, DefWithBodyId, FunctionId, GenericDefId, ImplId, MacroId,
3535
ModuleDefId, ModuleId, TraitId, TypeAliasId, UnresolvedMacro,
3636
attrs::AttrFlags,
3737
builtin_type::BuiltinUint,
@@ -2114,7 +2114,7 @@ impl<'db> ExprCollector<'db> {
21142114
) -> ExprId {
21152115
let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| {
21162116
let ast_id = self.expander.in_file(file_local_id);
2117-
self.db.intern_block(BlockLoc { ast_id, module: self.module })
2117+
unsafe { BlockIdLt::new(self.db, ast_id, self.module).to_static() }
21182118
});
21192119

21202120
let (module, def_map) =

crates/hir-def/src/find_path.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use intern::sym;
1212
use rustc_hash::FxHashSet;
1313

1414
use crate::{
15-
FindPathConfig, ModuleDefId, ModuleId,
15+
FindPathConfig, ModuleDefId, ModuleIdLt,
1616
db::DefDatabase,
1717
item_scope::ItemInNs,
1818
nameres::DefMap,
@@ -24,7 +24,7 @@ use crate::{
2424
pub fn find_path(
2525
db: &dyn DefDatabase,
2626
item: ItemInNs,
27-
from: ModuleId,
27+
from: ModuleIdLt<'_>,
2828
mut prefix_kind: PrefixKind,
2929
ignore_local_imports: bool,
3030
mut cfg: FindPathConfig,
@@ -102,14 +102,14 @@ struct FindPathCtx<'db> {
102102
cfg: FindPathConfig,
103103
ignore_local_imports: bool,
104104
is_std_item: bool,
105-
from: ModuleId,
105+
from: ModuleIdLt<'db>,
106106
from_crate: Crate,
107-
crate_root: ModuleId,
107+
crate_root: ModuleIdLt<'db>,
108108
from_def_map: &'db DefMap,
109109
fuel: Cell<usize>,
110110
}
111111

112-
/// Attempts to find a path to refer to the given `item` visible from the `from` ModuleId
112+
/// Attempts to find a path to refer to the given `item` visible from the `from` ModuleIdLt<'_>
113113
fn find_path_inner(ctx: &FindPathCtx<'_>, item: ItemInNs, max_len: usize) -> Option<ModPath> {
114114
// - if the item is a module, jump straight to module search
115115
if !ctx.is_std_item
@@ -157,10 +157,10 @@ fn find_path_inner(ctx: &FindPathCtx<'_>, item: ItemInNs, max_len: usize) -> Opt
157157
}
158158

159159
#[tracing::instrument(skip_all)]
160-
fn find_path_for_module(
161-
ctx: &FindPathCtx<'_>,
162-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
163-
module_id: ModuleId,
160+
fn find_path_for_module<'db>(
161+
ctx: &'db FindPathCtx<'db>,
162+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
163+
module_id: ModuleIdLt<'db>,
164164
maybe_extern: bool,
165165
max_len: usize,
166166
) -> Option<Choice> {
@@ -217,7 +217,7 @@ fn find_path_for_module(
217217
ctx.db,
218218
ctx.from_def_map,
219219
ctx.from,
220-
ItemInNs::Types(module_id.into()),
220+
ItemInNs::Types(unsafe { module_id.to_static() }.into()),
221221
ctx.ignore_local_imports,
222222
);
223223
if let Some(scope_name) = scope_name {
@@ -244,7 +244,7 @@ fn find_path_for_module(
244244
}
245245

246246
// - if the module is in the prelude, return it by that path
247-
let item = ItemInNs::Types(module_id.into());
247+
let item = ItemInNs::Types(unsafe { module_id.to_static() }.into());
248248
if let Some(choice) = find_in_prelude(ctx.db, ctx.from_def_map, item, ctx.from) {
249249
return Some(choice);
250250
}
@@ -257,10 +257,10 @@ fn find_path_for_module(
257257
best_choice
258258
}
259259

260-
fn find_in_scope(
261-
db: &dyn DefDatabase,
260+
fn find_in_scope<'db>(
261+
db: &'db dyn DefDatabase,
262262
def_map: &DefMap,
263-
from: ModuleId,
263+
from: ModuleIdLt<'db>,
264264
item: ItemInNs,
265265
ignore_local_imports: bool,
266266
) -> Option<Name> {
@@ -278,7 +278,7 @@ fn find_in_prelude(
278278
db: &dyn DefDatabase,
279279
local_def_map: &DefMap,
280280
item: ItemInNs,
281-
from: ModuleId,
281+
from: ModuleIdLt<'_>,
282282
) -> Option<Choice> {
283283
let (prelude_module, _) = local_def_map.prelude()?;
284284
let prelude_def_map = prelude_module.def_map(db);
@@ -310,8 +310,8 @@ fn find_in_prelude(
310310
fn is_kw_kind_relative_to_from(
311311
db: &dyn DefDatabase,
312312
def_map: &DefMap,
313-
item: ModuleId,
314-
from: ModuleId,
313+
item: ModuleIdLt<'_>,
314+
from: ModuleIdLt<'_>,
315315
) -> Option<PathKind> {
316316
if item.krate(db) != from.krate(db) || item.block(db).is_some() || from.block(db).is_some() {
317317
return None;
@@ -332,9 +332,9 @@ fn is_kw_kind_relative_to_from(
332332
}
333333

334334
#[tracing::instrument(skip_all)]
335-
fn calculate_best_path(
336-
ctx: &FindPathCtx<'_>,
337-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
335+
fn calculate_best_path<'db>(
336+
ctx: &'db FindPathCtx<'db>,
337+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
338338
item: ItemInNs,
339339
max_len: usize,
340340
best_choice: &mut Option<Choice>,
@@ -372,9 +372,9 @@ fn calculate_best_path(
372372
}
373373
}
374374

375-
fn find_in_sysroot(
376-
ctx: &FindPathCtx<'_>,
377-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
375+
fn find_in_sysroot<'db>(
376+
ctx: &'db FindPathCtx<'db>,
377+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
378378
item: ItemInNs,
379379
max_len: usize,
380380
best_choice: &mut Option<Choice>,
@@ -418,9 +418,9 @@ fn find_in_sysroot(
418418
});
419419
}
420420

421-
fn find_in_dep(
422-
ctx: &FindPathCtx<'_>,
423-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
421+
fn find_in_dep<'db>(
422+
ctx: &'db FindPathCtx<'db>,
423+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
424424
item: ItemInNs,
425425
max_len: usize,
426426
best_choice: &mut Option<Choice>,
@@ -461,9 +461,9 @@ fn find_in_dep(
461461
}
462462
}
463463

464-
fn calculate_best_path_local(
465-
ctx: &FindPathCtx<'_>,
466-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
464+
fn calculate_best_path_local<'db>(
465+
ctx: &'db FindPathCtx<'db>,
466+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
467467
item: ItemInNs,
468468
max_len: usize,
469469
best_choice: &mut Option<Choice>,
@@ -558,11 +558,11 @@ fn path_kind_len(kind: PathKind) -> usize {
558558
}
559559

560560
/// Finds locations in `from.krate` from which `item` can be imported by `from`.
561-
fn find_local_import_locations(
562-
ctx: &FindPathCtx<'_>,
561+
fn find_local_import_locations<'db>(
562+
ctx: &'db FindPathCtx<'db>,
563563
item: ItemInNs,
564-
visited_modules: &mut FxHashSet<(ItemInNs, ModuleId)>,
565-
mut cb: impl FnMut(&mut FxHashSet<(ItemInNs, ModuleId)>, &Name, ModuleId),
564+
visited_modules: &mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>,
565+
mut cb: impl FnMut(&mut FxHashSet<(ItemInNs, ModuleIdLt<'db>)>, &Name, ModuleIdLt<'db>),
566566
) {
567567
let _p = tracing::info_span!("find_local_import_locations").entered();
568568
let db = ctx.db;

crates/hir-def/src/import_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ mod tests {
496496
use expect_test::{Expect, expect};
497497
use test_fixture::WithFixture;
498498

499-
use crate::{ItemContainerId, Lookup, nameres::assoc::TraitItems, test_db::TestDB};
499+
use crate::{ItemContainerId, Lookup, ModuleIdLt, nameres::assoc::TraitItems, test_db::TestDB};
500500

501501
use super::*;
502502

@@ -628,8 +628,8 @@ mod tests {
628628
expect.assert_eq(&actual)
629629
}
630630

631-
fn render_path(db: &dyn DefDatabase, info: &ImportInfo) -> String {
632-
let mut module = info.container;
631+
fn render_path<'db>(db: &'db dyn DefDatabase, info: &ImportInfo) -> String {
632+
let mut module: ModuleIdLt<'db> = info.container;
633633
let mut segments = vec![&info.name];
634634

635635
let def_map = module.def_map(db);

crates/hir-def/src/item_tree.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use syntax::{SyntaxKind, ast, match_ast};
5858
use thin_vec::ThinVec;
5959
use triomphe::Arc;
6060

61-
use crate::{BlockId, Lookup, db::DefDatabase};
61+
use crate::{BlockId, db::DefDatabase};
6262

6363
pub(crate) use crate::item_tree::{
6464
attrs::*,
@@ -150,10 +150,10 @@ pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc
150150
let _p = tracing::info_span!("block_item_tree_query", ?block).entered();
151151
static EMPTY: OnceLock<Arc<ItemTree>> = OnceLock::new();
152152

153-
let loc = block.lookup(db);
154-
let block = loc.ast_id.to_node(db);
153+
let ast_id = block.ast_id(db);
154+
let block = ast_id.to_node(db);
155155

156-
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
156+
let ctx = lower::Ctx::new(db, ast_id.file_id);
157157
let mut item_tree = ctx.lower_block(&block);
158158
let ItemTree { top_level, top_attrs, attrs, vis, big_data, small_data } = &item_tree;
159159
if small_data.is_empty()

crates/hir-def/src/lib.rs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -420,13 +420,31 @@ pub struct ProcMacroLoc {
420420
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);
421421
impl_loc!(ProcMacroLoc, id: Fn, container: ModuleId);
422422

423-
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
424-
pub struct BlockLoc {
423+
#[salsa_macros::tracked(debug)]
424+
#[derive(PartialOrd, Ord)]
425+
pub struct BlockIdLt<'db> {
425426
pub ast_id: AstId<ast::BlockExpr>,
426427
/// The containing module.
427-
pub module: ModuleId,
428+
pub module: ModuleIdLt<'db>,
429+
}
430+
pub type BlockId = BlockIdLt<'static>;
431+
432+
impl BlockIdLt<'_> {
433+
/// # Safety
434+
///
435+
/// The caller must ensure that the `ModuleId` is not leaked outside of query computations.
436+
pub unsafe fn to_static(self) -> BlockId {
437+
unsafe { std::mem::transmute(self) }
438+
}
439+
}
440+
impl BlockId {
441+
/// # Safety
442+
///
443+
/// The caller must ensure that the `BlockId` comes from the given database.
444+
pub unsafe fn to_db<'db>(self, _db: &'db dyn DefDatabase) -> BlockIdLt<'db> {
445+
unsafe { std::mem::transmute(self) }
446+
}
428447
}
429-
impl_intern!(BlockId, BlockLoc, intern_block, lookup_intern_block);
430448

431449
#[salsa_macros::tracked(debug)]
432450
#[derive(PartialOrd, Ord)]
@@ -436,34 +454,26 @@ pub struct ModuleIdLt<'db> {
436454
/// If this `ModuleId` was derived from a `DefMap` for a block expression, this stores the
437455
/// `BlockId` of that block expression. If `None`, this module is part of the crate-level
438456
/// `DefMap` of `krate`.
439-
pub block: Option<BlockId>,
457+
pub block: Option<BlockIdLt<'db>>,
440458
}
441459
pub type ModuleId = ModuleIdLt<'static>;
442460

443-
impl ModuleIdLt<'_> {
461+
impl<'db> ModuleIdLt<'db> {
444462
/// # Safety
445463
///
446464
/// The caller must ensure that the `ModuleId` is not leaked outside of query computations.
447465
pub unsafe fn to_static(self) -> ModuleId {
448466
unsafe { std::mem::transmute(self) }
449467
}
450-
}
451-
impl ModuleId {
452-
/// # Safety
453-
///
454-
/// The caller must ensure that the `ModuleId` comes from the given database.
455-
pub unsafe fn to_db<'db>(self, _db: &'db dyn DefDatabase) -> ModuleIdLt<'db> {
456-
unsafe { std::mem::transmute(self) }
457-
}
458468

459-
pub fn def_map(self, db: &dyn DefDatabase) -> &DefMap {
469+
pub fn def_map(self, db: &'db dyn DefDatabase) -> &'db DefMap {
460470
match self.block(db) {
461471
Some(block) => block_def_map(db, block),
462472
None => crate_def_map(db, self.krate(db)),
463473
}
464474
}
465475

466-
pub(crate) fn local_def_map(self, db: &dyn DefDatabase) -> (&DefMap, &LocalDefMap) {
476+
pub(crate) fn local_def_map(self, db: &'db dyn DefDatabase) -> (&'db DefMap, &'db LocalDefMap) {
467477
match self.block(db) {
468478
Some(block) => (block_def_map(db, block), self.only_local_def_map(db)),
469479
None => {
@@ -473,15 +483,15 @@ impl ModuleId {
473483
}
474484
}
475485

476-
pub(crate) fn only_local_def_map(self, db: &dyn DefDatabase) -> &LocalDefMap {
486+
pub(crate) fn only_local_def_map(self, db: &'db dyn DefDatabase) -> &'db LocalDefMap {
477487
crate_local_def_map(db, self.krate(db)).local(db)
478488
}
479489

480-
pub fn crate_def_map(self, db: &dyn DefDatabase) -> &DefMap {
490+
pub fn crate_def_map(self, db: &'db dyn DefDatabase) -> &'db DefMap {
481491
crate_def_map(db, self.krate(db))
482492
}
483493

484-
pub fn name(self, db: &dyn DefDatabase) -> Option<Name> {
494+
pub fn name(self, db: &'db dyn DefDatabase) -> Option<Name> {
485495
let def_map = self.def_map(db);
486496
let parent = def_map[self].parent?;
487497
def_map[parent].children.iter().find_map(|(name, module_id)| {
@@ -491,15 +501,24 @@ impl ModuleId {
491501

492502
/// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
493503
/// the block, if `self` corresponds to a block expression.
494-
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
504+
pub fn containing_module(self, db: &'db dyn DefDatabase) -> Option<ModuleIdLt<'db>> {
495505
self.def_map(db).containing_module(self)
496506
}
497507

498-
pub fn is_block_module(self, db: &dyn DefDatabase) -> bool {
508+
pub fn is_block_module(self, db: &'db dyn DefDatabase) -> bool {
499509
self.block(db).is_some() && self.def_map(db).root_module_id() == self
500510
}
501511
}
502512

513+
impl ModuleId {
514+
/// # Safety
515+
///
516+
/// The caller must ensure that the `ModuleId` comes from the given database.
517+
pub unsafe fn to_db<'db>(self, _db: &'db dyn DefDatabase) -> ModuleIdLt<'db> {
518+
unsafe { std::mem::transmute(self) }
519+
}
520+
}
521+
503522
impl HasModule for ModuleId {
504523
#[inline]
505524
fn module(&self, _db: &dyn DefDatabase) -> ModuleId {

0 commit comments

Comments
 (0)