Skip to content

Commit 51f55eb

Browse files
committed
Turn BlockLoc into a tracked struct
1 parent 3a5d0d9 commit 51f55eb

File tree

14 files changed

+213
-145
lines changed

14 files changed

+213
-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/expr_store/tests/body/block.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,55 @@ fn f() {
195195
Id(1c00),
196196
),
197197
block: Some(
198-
BlockId(
199-
3c01,
200-
),
198+
BlockIdLt {
199+
[salsa id]: Id(3c01),
200+
ast_id: InFileWrapper {
201+
file_id: FileId(
202+
EditionedFileIdData {
203+
editioned_file_id: EditionedFileId(
204+
0,
205+
Edition2024,
206+
),
207+
krate: Crate(
208+
Id(1c00),
209+
),
210+
},
211+
),
212+
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: F9BF }),
213+
},
214+
module: ModuleIdLt {
215+
[salsa id]: Id(3002),
216+
krate: Crate(
217+
Id(1c00),
218+
),
219+
block: Some(
220+
BlockIdLt {
221+
[salsa id]: Id(3c00),
222+
ast_id: InFileWrapper {
223+
file_id: FileId(
224+
EditionedFileIdData {
225+
editioned_file_id: EditionedFileId(
226+
0,
227+
Edition2024,
228+
),
229+
krate: Crate(
230+
Id(1c00),
231+
),
232+
},
233+
),
234+
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: C181 }),
235+
},
236+
module: ModuleIdLt {
237+
[salsa id]: Id(3000),
238+
krate: Crate(
239+
Id(1c00),
240+
),
241+
block: None,
242+
},
243+
},
244+
),
245+
},
246+
},
201247
),
202248
}"#]],
203249
);

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()

0 commit comments

Comments
 (0)