Skip to content

Commit 77894b3

Browse files
committed
Make ModuleId a tracked struct
1 parent fe5a925 commit 77894b3

File tree

123 files changed

+1011
-1086
lines changed

Some content is hidden

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

123 files changed

+1011
-1086
lines changed

crates/base-db/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,19 @@ impl Files {
167167
}
168168
}
169169

170-
#[salsa_macros::interned(no_lifetime, debug, constructor=from_span)]
170+
#[salsa_macros::interned(no_lifetime, constructor=from_span)]
171171
#[derive(PartialOrd, Ord)]
172172
pub struct EditionedFileId {
173173
pub editioned_file_id: span::EditionedFileId,
174174
}
175175

176+
impl std::fmt::Debug for EditionedFileId {
177+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
178+
salsa::with_attached_database(|db| self.editioned_file_id(db).fmt(f))
179+
.unwrap_or_else(|| f.debug_tuple("EditionedFileId").field(&self.0).finish())
180+
}
181+
}
182+
176183
impl EditionedFileId {
177184
// Salsa already uses the name `new`...
178185
#[inline]

crates/hir-def/src/attr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@ impl Attrs {
108108
let (fields, file_id, krate) = match v {
109109
VariantId::EnumVariantId(it) => {
110110
let loc = it.lookup(db);
111-
let krate = loc.parent.lookup(db).container.krate;
111+
let krate = loc.parent.lookup(db).container.krate(db);
112112
let source = loc.source(db);
113113
(source.value.field_list(), source.file_id, krate)
114114
}
115115
VariantId::StructId(it) => {
116116
let loc = it.lookup(db);
117-
let krate = loc.container.krate;
117+
let krate = loc.container.krate(db);
118118
let source = loc.source(db);
119119
(source.value.field_list(), source.file_id, krate)
120120
}
121121
VariantId::UnionId(it) => {
122122
let loc = it.lookup(db);
123-
let krate = loc.container.krate;
123+
let krate = loc.container.krate(db);
124124
let source = loc.source(db);
125125
(
126126
source.value.record_field_list().map(ast::FieldList::RecordFieldList),
@@ -520,7 +520,7 @@ impl AttrsWithOwner {
520520
match def {
521521
AttrDefId::ModuleId(module) => {
522522
let def_map = module.def_map(db);
523-
let mod_data = &def_map[module.local_id];
523+
let mod_data = &def_map[module];
524524

525525
let raw_attrs = match mod_data.origin {
526526
ModuleOrigin::File { definition, declaration_tree_id, declaration, .. } => {
@@ -544,7 +544,7 @@ impl AttrsWithOwner {
544544
tree.top_level_raw_attrs().clone()
545545
}
546546
};
547-
Attrs::expand_cfg_attr(db, module.krate, raw_attrs)
547+
Attrs::expand_cfg_attr(db, module.krate(db), raw_attrs)
548548
}
549549
AttrDefId::FieldId(it) => db.fields_attrs(it.parent)[it.local_id].clone(),
550550
AttrDefId::EnumVariantId(it) => attrs_from_ast_id_loc(db, it),
@@ -618,7 +618,7 @@ impl AttrsWithOwner {
618618
// Modules can have 2 attribute owners (the `mod x;` item, and the module file itself).
619619

620620
let def_map = module.def_map(db);
621-
let mod_data = &def_map[module.local_id];
621+
let mod_data = &def_map[module];
622622
match mod_data.declaration_source(db) {
623623
Some(it) => {
624624
let mut map = AttrSourceMap::new(InFile::new(it.file_id, &it.value));

crates/hir-def/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
418418
let loc: Macro2Loc = it.lookup(db);
419419

420420
MacroDefId {
421-
krate: loc.container.krate,
421+
krate: loc.container.krate(db),
422422
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
423423
local_inner: false,
424424
allow_internal_unsafe: loc.allow_internal_unsafe,
@@ -429,7 +429,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
429429
let loc: MacroRulesLoc = it.lookup(db);
430430

431431
MacroDefId {
432-
krate: loc.container.krate,
432+
krate: loc.container.krate(db),
433433
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
434434
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
435435
allow_internal_unsafe: loc
@@ -442,7 +442,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
442442
let loc = it.lookup(db);
443443

444444
MacroDefId {
445-
krate: loc.container.krate,
445+
krate: loc.container.krate(db),
446446
kind: MacroDefKind::ProcMacro(loc.id, loc.expander, loc.kind),
447447
local_inner: false,
448448
allow_internal_unsafe: false,

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ pub struct ExprCollector<'db> {
458458
current_binding_owner: Option<ExprId>,
459459

460460
awaitable_context: Option<Awaitable>,
461+
krate: base_db::Crate,
461462
}
462463

463464
#[derive(Clone, Debug)]
@@ -547,7 +548,7 @@ impl ExprCollector<'_> {
547548
let expander = Expander::new(db, current_file_id, def_map);
548549
ExprCollector {
549550
db,
550-
cfg_options: module.krate().cfg_options(db),
551+
cfg_options: module.krate(db).cfg_options(db),
551552
module,
552553
def_map,
553554
local_def_map,
@@ -561,6 +562,7 @@ impl ExprCollector<'_> {
561562
awaitable_context: None,
562563
current_block_legacy_macro_defs_count: FxHashMap::default(),
563564
outer_impl_trait: false,
565+
krate: module.krate(db),
564566
}
565567
}
566568

@@ -1940,9 +1942,8 @@ impl ExprCollector<'_> {
19401942
T: ast::AstNode,
19411943
{
19421944
let macro_call_ptr = self.expander.in_file(syntax_ptr);
1943-
let module = self.module.local_id;
19441945

1945-
let block_call = self.def_map.modules[self.module.local_id].scope.macro_invoc(
1946+
let block_call = self.def_map.modules[self.module].scope.macro_invoc(
19461947
self.expander.in_file(self.expander.ast_id_map().ast_id_for_ptr(syntax_ptr)),
19471948
);
19481949
let res = match block_call {
@@ -1954,7 +1955,7 @@ impl ExprCollector<'_> {
19541955
.resolve_path(
19551956
self.local_def_map,
19561957
self.db,
1957-
module,
1958+
self.module,
19581959
path,
19591960
crate::item_scope::BuiltinShadowMode::Other,
19601961
Some(MacroSubNs::Bang),
@@ -1965,7 +1966,7 @@ impl ExprCollector<'_> {
19651966
self.expander.enter_expand(
19661967
self.db,
19671968
mcall,
1968-
self.module.krate(),
1969+
self.krate,
19691970
resolver,
19701971
&mut |ptr, call| {
19711972
_ = self.source_map.expansions.insert(ptr.map(|(it, _)| it), call);
@@ -2095,7 +2096,8 @@ impl ExprCollector<'_> {
20952096
return;
20962097
};
20972098
let name = name.as_name();
2098-
let macro_id = self.def_map.modules[DefMap::ROOT].scope.get(&name).take_macros();
2099+
let macro_id =
2100+
self.def_map.modules[self.def_map.root].scope.get(&name).take_macros();
20992101
self.collect_macro_def(statements, macro_id);
21002102
}
21012103
ast::Stmt::Item(ast::Item::MacroRules(macro_)) => {
@@ -2109,7 +2111,7 @@ impl ExprCollector<'_> {
21092111
let name = name.as_name();
21102112
let macro_defs_count =
21112113
self.current_block_legacy_macro_defs_count.entry(name.clone()).or_insert(0);
2112-
let macro_id = self.def_map.modules[DefMap::ROOT]
2114+
let macro_id = self.def_map.modules[self.def_map.root]
21132115
.scope
21142116
.get_legacy_macro(&name)
21152117
.and_then(|it| it.get(*macro_defs_count))
@@ -2155,7 +2157,7 @@ impl ExprCollector<'_> {
21552157
match block_id.map(|block_id| (block_def_map(self.db, block_id), block_id)) {
21562158
Some((def_map, block_id)) => {
21572159
self.store.block_scopes.push(block_id);
2158-
(def_map.module_id(DefMap::ROOT), def_map)
2160+
(def_map.root_module_id(), def_map)
21592161
}
21602162
None => (self.module, self.def_map),
21612163
};
@@ -2238,7 +2240,7 @@ impl ExprCollector<'_> {
22382240
let (resolved, _) = self.def_map.resolve_path(
22392241
self.local_def_map,
22402242
self.db,
2241-
self.module.local_id,
2243+
self.module,
22422244
&name.clone().into(),
22432245
BuiltinShadowMode::Other,
22442246
None,
@@ -2865,12 +2867,12 @@ impl ExprCollector<'_> {
28652867

28662868
let new_v1_formatted = LangItem::FormatArguments.ty_rel_path(
28672869
self.db,
2868-
self.module.krate(),
2870+
self.krate,
28692871
Name::new_symbol_root(sym::new_v1_formatted),
28702872
);
28712873
let unsafe_arg_new = LangItem::FormatUnsafeArg.ty_rel_path(
28722874
self.db,
2873-
self.module.krate(),
2875+
self.krate,
28742876
Name::new_symbol_root(sym::new),
28752877
);
28762878
let new_v1_formatted =
@@ -2961,7 +2963,7 @@ impl ExprCollector<'_> {
29612963
let precision_expr = self.make_count(precision, argmap);
29622964
let width_expr = self.make_count(width, argmap);
29632965

2964-
if self.module.krate().workspace_data(self.db).is_atleast_187() {
2966+
if self.krate.workspace_data(self.db).is_atleast_187() {
29652967
// These need to match the constants in library/core/src/fmt/rt.rs.
29662968
let align = match alignment {
29672969
Some(FormatAlignment::Left) => 0,
@@ -2996,15 +2998,15 @@ impl ExprCollector<'_> {
29962998
let width =
29972999
RecordLitField { name: Name::new_symbol_root(sym::width), expr: width_expr };
29983000
self.alloc_expr_desugared(Expr::RecordLit {
2999-
path: LangItem::FormatPlaceholder.path(self.db, self.module.krate()).map(Box::new),
3001+
path: LangItem::FormatPlaceholder.path(self.db, self.krate).map(Box::new),
30003002
fields: Box::new([position, flags, precision, width]),
30013003
spread: None,
30023004
})
30033005
} else {
30043006
let format_placeholder_new = {
30053007
let format_placeholder_new = LangItem::FormatPlaceholder.ty_rel_path(
30063008
self.db,
3007-
self.module.krate(),
3009+
self.krate,
30083010
Name::new_symbol_root(sym::new),
30093011
);
30103012
match format_placeholder_new {
@@ -3027,7 +3029,7 @@ impl ExprCollector<'_> {
30273029
let align = {
30283030
let align = LangItem::FormatAlignment.ty_rel_path(
30293031
self.db,
3030-
self.module.krate(),
3032+
self.krate,
30313033
match alignment {
30323034
Some(FormatAlignment::Left) => Name::new_symbol_root(sym::Left),
30333035
Some(FormatAlignment::Right) => Name::new_symbol_root(sym::Right),
@@ -3080,7 +3082,7 @@ impl ExprCollector<'_> {
30803082
)));
30813083
let count_is = match LangItem::FormatCount.ty_rel_path(
30823084
self.db,
3083-
self.module.krate(),
3085+
self.krate,
30843086
Name::new_symbol_root(sym::Is),
30853087
) {
30863088
Some(count_is) => self.alloc_expr_desugared(Expr::Path(count_is)),
@@ -3098,7 +3100,7 @@ impl ExprCollector<'_> {
30983100
)));
30993101
let count_param = match LangItem::FormatCount.ty_rel_path(
31003102
self.db,
3101-
self.module.krate(),
3103+
self.krate,
31023104
Name::new_symbol_root(sym::Param),
31033105
) {
31043106
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
@@ -3116,7 +3118,7 @@ impl ExprCollector<'_> {
31163118
}
31173119
None => match LangItem::FormatCount.ty_rel_path(
31183120
self.db,
3119-
self.module.krate(),
3121+
self.krate,
31203122
Name::new_symbol_root(sym::Implied),
31213123
) {
31223124
Some(count_param) => self.alloc_expr_desugared(Expr::Path(count_param)),
@@ -3138,7 +3140,7 @@ impl ExprCollector<'_> {
31383140

31393141
let new_fn = match LangItem::FormatArgument.ty_rel_path(
31403142
self.db,
3141-
self.module.krate(),
3143+
self.krate,
31423144
Name::new_symbol_root(match ty {
31433145
Format(Display) => sym::new_display,
31443146
Format(Debug) => sym::new_debug,
@@ -3161,7 +3163,7 @@ impl ExprCollector<'_> {
31613163
// endregion: format
31623164

31633165
fn lang_path(&self, lang: LangItem) -> Option<Path> {
3164-
lang.path(self.db, self.module.krate())
3166+
lang.path(self.db, self.krate)
31653167
}
31663168
}
31673169

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: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ fn outer() {
176176

177177
#[test]
178178
fn nested_module_scoping() {
179+
cov_mark::check!(nested_module_scoping);
179180
check_block_scopes_at(
180181
r#"
181182
fn f() {
@@ -189,10 +190,57 @@ fn f() {
189190
}
190191
"#,
191192
expect![[r#"
192-
BlockIdLt { [salsa id]: Id(3c01) } in BlockRelativeModuleId { block: Some(BlockIdLt { [salsa id]: Id(3c00) }), local_id: Idx::<ModuleData>(1) }
193-
BlockIdLt { [salsa id]: Id(3c00) } in BlockRelativeModuleId { block: None, local_id: Idx::<ModuleData>(0) }
194-
crate scope
195-
"#]],
193+
ModuleIdLt {
194+
[salsa id]: Id(3003),
195+
krate: Crate(
196+
Id(2000),
197+
),
198+
block: Some(
199+
BlockIdLt {
200+
[salsa id]: Id(4001),
201+
loc: BlockLoc {
202+
ast_id: InFileWrapper {
203+
file_id: FileId(
204+
EditionedFileId(
205+
0,
206+
Edition2024,
207+
),
208+
),
209+
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: DA08 }),
210+
},
211+
module: ModuleIdLt {
212+
[salsa id]: Id(3002),
213+
krate: Crate(
214+
Id(2000),
215+
),
216+
block: Some(
217+
BlockIdLt {
218+
[salsa id]: Id(4000),
219+
loc: BlockLoc {
220+
ast_id: InFileWrapper {
221+
file_id: FileId(
222+
EditionedFileId(
223+
0,
224+
Edition2024,
225+
),
226+
),
227+
value: FileAstId::<syntax::ast::generated::nodes::BlockExpr>(ErasedFileAstId { kind: BlockExpr, index: 0, hash: 2997 }),
228+
},
229+
module: ModuleIdLt {
230+
[salsa id]: Id(3000),
231+
krate: Crate(
232+
Id(2000),
233+
),
234+
block: None,
235+
},
236+
},
237+
},
238+
),
239+
},
240+
},
241+
},
242+
),
243+
}"#]],
196244
);
197245
}
198246

@@ -455,9 +503,8 @@ fn foo() {
455503
}
456504

457505
#[test]
458-
fn is_visible_from_same_def_map() {
506+
fn is_visible_from_same_def_map_regression_9481() {
459507
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/9481
460-
cov_mark::check!(is_visible_from_same_block_def_map);
461508
check_at(
462509
r#"
463510
fn outer() {
@@ -474,7 +521,6 @@ fn outer() {
474521
tests: t
475522
476523
block scope::tests
477-
name: _
478524
outer: vg
479525
480526
crate

0 commit comments

Comments
 (0)