Skip to content

Commit e059280

Browse files
authored
Merge pull request #21178 from Veykril/tracked-modules
internal: Make `ModuleId` a tracked struct
2 parents 41f0d80 + 64cabd8 commit e059280

File tree

137 files changed

+1168
-1263
lines changed

Some content is hidden

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

137 files changed

+1168
-1263
lines changed

crates/hir-def/src/attrs.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ use syntax::{
4545
use tt::{TextRange, TextSize};
4646

4747
use crate::{
48-
AdtId, AstIdLoc, AttrDefId, FieldId, FunctionId, GenericDefId, HasModule, InternedModuleId,
49-
LifetimeParamId, LocalFieldId, MacroId, TypeOrConstParamId, VariantId,
48+
AdtId, AstIdLoc, AttrDefId, FieldId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
49+
LocalFieldId, MacroId, ModuleId, TypeOrConstParamId, VariantId,
5050
db::DefDatabase,
5151
hir::generics::{GenericParams, LocalLifetimeParamId, LocalTypeOrConstParamId},
5252
nameres::ModuleOrigin,
@@ -295,9 +295,8 @@ fn attrs_source(
295295
) -> (InFile<ast::AnyHasAttrs>, Option<InFile<ast::Module>>, Crate) {
296296
let (owner, krate) = match owner {
297297
AttrDefId::ModuleId(id) => {
298-
let id = id.loc(db);
299298
let def_map = id.def_map(db);
300-
let (definition, declaration) = match def_map[id.local_id].origin {
299+
let (definition, declaration) = match def_map[id].origin {
301300
ModuleOrigin::CrateRoot { definition } => {
302301
let file = db.parse(definition).tree();
303302
(InFile::new(definition.into(), ast::AnyHasAttrs::from(file)), None)
@@ -318,7 +317,7 @@ fn attrs_source(
318317
(block.with_value(definition.into()), None)
319318
}
320319
};
321-
return (definition, declaration, id.krate);
320+
return (definition, declaration, def_map.krate());
322321
}
323322
AttrDefId::AdtId(AdtId::StructId(it)) => attrs_from_ast_id_loc(db, it),
324323
AttrDefId::AdtId(AdtId::UnionId(it)) => attrs_from_ast_id_loc(db, it),
@@ -1201,14 +1200,14 @@ impl AttrFlags {
12011200
}
12021201

12031202
#[inline]
1204-
pub fn doc_keyword(db: &dyn DefDatabase, owner: InternedModuleId) -> Option<Symbol> {
1203+
pub fn doc_keyword(db: &dyn DefDatabase, owner: ModuleId) -> Option<Symbol> {
12051204
if !AttrFlags::query(db, AttrDefId::ModuleId(owner)).contains(AttrFlags::HAS_DOC_KEYWORD) {
12061205
return None;
12071206
}
12081207
return doc_keyword(db, owner);
12091208

12101209
#[salsa::tracked]
1211-
fn doc_keyword(db: &dyn DefDatabase, owner: InternedModuleId) -> Option<Symbol> {
1210+
fn doc_keyword(db: &dyn DefDatabase, owner: ModuleId) -> Option<Symbol> {
12121211
collect_attrs(db, AttrDefId::ModuleId(owner), |attr| {
12131212
if let Meta::TokenTree { path, tt } = attr
12141213
&& path.is1("doc")

crates/hir-def/src/db.rs

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

1010
use crate::{
11-
AssocItemId, AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, CrateRootModuleId, DefWithBodyId,
12-
EnumId, EnumLoc, EnumVariantId, EnumVariantLoc, ExternBlockId, ExternBlockLoc, ExternCrateId,
13-
ExternCrateLoc, FunctionId, FunctionLoc, GenericDefId, HasModule, ImplId, ImplLoc,
14-
InternedModuleId, LocalFieldId, Macro2Id, Macro2Loc, MacroExpander, MacroId, MacroRulesId,
15-
MacroRulesLoc, MacroRulesLocFlags, ProcMacroId, ProcMacroLoc, StaticId, StaticLoc, StructId,
16-
StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, UnionLoc, UseId, UseLoc,
17-
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,
1817
attrs::AttrFlags,
1918
expr_store::{
2019
Body, BodySourceMap, ExpressionStore, ExpressionStoreSourceMap, scope::ExprScopes,
@@ -83,9 +82,6 @@ pub trait InternDatabase: RootQueryDb {
8382
#[salsa::interned]
8483
fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
8584
// endregion: items
86-
87-
#[salsa::interned]
88-
fn intern_block(&self, loc: BlockLoc) -> BlockId;
8985
}
9086

9187
#[query_group::query_group]
@@ -276,8 +272,8 @@ fn include_macro_invoc(
276272
}
277273

278274
fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: Crate) -> bool {
279-
let root_module = CrateRootModuleId::from(crate_id).module(db);
280-
let attrs = AttrFlags::query(db, AttrDefId::ModuleId(InternedModuleId::new(db, root_module)));
275+
let root_module = crate_def_map(db, crate_id).root_module_id();
276+
let attrs = AttrFlags::query(db, AttrDefId::ModuleId(root_module));
281277
attrs.contains(AttrFlags::IS_NO_STD)
282278
}
283279

@@ -298,7 +294,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
298294
let loc: Macro2Loc = it.lookup(db);
299295

300296
MacroDefId {
301-
krate: loc.container.krate,
297+
krate: loc.container.krate(db),
302298
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
303299
local_inner: false,
304300
allow_internal_unsafe: loc.allow_internal_unsafe,
@@ -309,7 +305,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
309305
let loc: MacroRulesLoc = it.lookup(db);
310306

311307
MacroDefId {
312-
krate: loc.container.krate,
308+
krate: loc.container.krate(db),
313309
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
314310
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
315311
allow_internal_unsafe: loc
@@ -322,7 +318,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
322318
let loc = it.lookup(db);
323319

324320
MacroDefId {
325-
krate: loc.container.krate,
321+
krate: loc.container.krate(db),
326322
kind: MacroDefKind::ProcMacro(loc.id, loc.expander, loc.kind),
327323
local_inner: false,
328324
allow_internal_unsafe: false,

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

Lines changed: 16 additions & 13 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,
@@ -437,6 +437,7 @@ pub struct ExprCollector<'db> {
437437
current_binding_owner: Option<ExprId>,
438438

439439
awaitable_context: Option<Awaitable>,
440+
krate: base_db::Crate,
440441
}
441442

442443
#[derive(Clone, Debug)]
@@ -524,9 +525,10 @@ impl<'db> ExprCollector<'db> {
524525
) -> ExprCollector<'_> {
525526
let (def_map, local_def_map) = module.local_def_map(db);
526527
let expander = Expander::new(db, current_file_id, def_map);
528+
let krate = module.krate(db);
527529
ExprCollector {
528530
db,
529-
cfg_options: module.krate().cfg_options(db),
531+
cfg_options: krate.cfg_options(db),
530532
module,
531533
def_map,
532534
local_def_map,
@@ -540,12 +542,13 @@ impl<'db> ExprCollector<'db> {
540542
awaitable_context: None,
541543
current_block_legacy_macro_defs_count: FxHashMap::default(),
542544
outer_impl_trait: false,
545+
krate,
543546
}
544547
}
545548

546549
#[inline]
547550
pub(crate) fn lang_items(&self) -> &'db LangItems {
548-
self.lang_items.get_or_init(|| crate::lang_item::lang_items(self.db, self.module.krate))
551+
self.lang_items.get_or_init(|| crate::lang_item::lang_items(self.db, self.def_map.krate()))
549552
}
550553

551554
#[inline]
@@ -1915,9 +1918,8 @@ impl<'db> ExprCollector<'db> {
19151918
T: ast::AstNode,
19161919
{
19171920
let macro_call_ptr = self.expander.in_file(syntax_ptr);
1918-
let module = self.module.local_id;
19191921

1920-
let block_call = self.def_map.modules[self.module.local_id].scope.macro_invoc(
1922+
let block_call = self.def_map.modules[self.module].scope.macro_invoc(
19211923
self.expander.in_file(self.expander.ast_id_map().ast_id_for_ptr(syntax_ptr)),
19221924
);
19231925
let res = match block_call {
@@ -1929,7 +1931,7 @@ impl<'db> ExprCollector<'db> {
19291931
.resolve_path(
19301932
self.local_def_map,
19311933
self.db,
1932-
module,
1934+
self.module,
19331935
path,
19341936
crate::item_scope::BuiltinShadowMode::Other,
19351937
Some(MacroSubNs::Bang),
@@ -1940,7 +1942,7 @@ impl<'db> ExprCollector<'db> {
19401942
self.expander.enter_expand(
19411943
self.db,
19421944
mcall,
1943-
self.module.krate(),
1945+
self.krate,
19441946
resolver,
19451947
&mut |ptr, call| {
19461948
_ = self.store.expansions.insert(ptr.map(|(it, _)| it), call);
@@ -2058,7 +2060,8 @@ impl<'db> ExprCollector<'db> {
20582060
return;
20592061
};
20602062
let name = name.as_name();
2061-
let macro_id = self.def_map.modules[DefMap::ROOT].scope.get(&name).take_macros();
2063+
let macro_id =
2064+
self.def_map.modules[self.def_map.root].scope.get(&name).take_macros();
20622065
self.collect_macro_def(statements, macro_id);
20632066
}
20642067
ast::Stmt::Item(ast::Item::MacroRules(macro_)) => {
@@ -2072,7 +2075,7 @@ impl<'db> ExprCollector<'db> {
20722075
let name = name.as_name();
20732076
let macro_defs_count =
20742077
self.current_block_legacy_macro_defs_count.entry(name.clone()).or_insert(0);
2075-
let macro_id = self.def_map.modules[DefMap::ROOT]
2078+
let macro_id = self.def_map.modules[self.def_map.root]
20762079
.scope
20772080
.get_legacy_macro(&name)
20782081
.and_then(|it| it.get(*macro_defs_count))
@@ -2111,14 +2114,14 @@ impl<'db> ExprCollector<'db> {
21112114
) -> ExprId {
21122115
let block_id = self.expander.ast_id_map().ast_id_for_block(&block).map(|file_local_id| {
21132116
let ast_id = self.expander.in_file(file_local_id);
2114-
self.db.intern_block(BlockLoc { ast_id, module: self.module })
2117+
unsafe { BlockIdLt::new(self.db, ast_id, self.module).to_static() }
21152118
});
21162119

21172120
let (module, def_map) =
21182121
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
21192122
Some((def_map, block_id)) => {
21202123
self.store.block_scopes.push(block_id);
2121-
(def_map.module_id(DefMap::ROOT), def_map)
2124+
(def_map.root_module_id(), def_map)
21222125
}
21232126
None => (self.module, self.def_map),
21242127
};
@@ -2201,7 +2204,7 @@ impl<'db> ExprCollector<'db> {
22012204
let (resolved, _) = self.def_map.resolve_path(
22022205
self.local_def_map,
22032206
self.db,
2204-
self.module.local_id,
2207+
self.module,
22052208
&name.clone().into(),
22062209
BuiltinShadowMode::Other,
22072210
None,
@@ -3131,7 +3134,7 @@ impl<'db> ExprCollector<'db> {
31313134
let precision_expr = self.make_count(precision, argmap);
31323135
let width_expr = self.make_count(width, argmap);
31333136

3134-
if self.module.krate().workspace_data(self.db).is_atleast_187() {
3137+
if self.krate.workspace_data(self.db).is_atleast_187() {
31353138
// These need to match the constants in library/core/src/fmt/rt.rs.
31363139
let align = match alignment {
31373140
Some(FormatAlignment::Left) => 0,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ fn def_map_at(#[rust_analyzer::rust_fixture] ra_fixture: &str) -> String {
3232
let (db, position) = TestDB::with_position(ra_fixture);
3333

3434
let module = db.module_at_position(position);
35-
module.def_map(&db).dump(&db)
35+
salsa::plumbing::attach(&db, || module.def_map(&db).dump(&db))
3636
}
3737

3838
fn check_block_scopes_at(#[rust_analyzer::rust_fixture] ra_fixture: &str, expect: Expect) {
3939
let (db, position) = TestDB::with_position(ra_fixture);
4040

4141
let module = db.module_at_position(position);
42-
let actual = module.def_map(&db).dump_block_scopes(&db);
42+
let actual = salsa::plumbing::attach(&db, || format!("{module:#?}"));
4343
expect.assert_eq(&actual);
4444
}
4545

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

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,63 @@ 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) }
194-
crate scope
195-
"#]],
192+
ModuleIdLt {
193+
[salsa id]: Id(3003),
194+
krate: Crate(
195+
Id(1c00),
196+
),
197+
block: Some(
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+
},
247+
),
248+
}"#]],
196249
);
197250
}
198251

@@ -460,7 +513,7 @@ fn foo() {
460513
}
461514

462515
#[test]
463-
fn is_visible_from_same_def_map() {
516+
fn is_visible_from_same_def_map_regression_9481() {
464517
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/9481
465518
check_at(
466519
r#"
@@ -478,7 +531,6 @@ fn outer() {
478531
- tests : type
479532
480533
(block scope)::tests
481-
- name : _
482534
- outer : value (glob)
483535
484536
crate

0 commit comments

Comments
 (0)