Skip to content

Commit ee2d044

Browse files
committed
Make ModuleId a tracked struct
optimize some stuff Optimize `pub(crate)` visibility resolution Optimize private visibility resolution
1 parent 2ca0461 commit ee2d044

File tree

135 files changed

+1005
-1167
lines changed

Some content is hidden

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

135 files changed

+1005
-1167
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 & 12 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, 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,
1817
attrs::AttrFlags,
1918
expr_store::{
2019
Body, BodySourceMap, ExpressionStore, ExpressionStoreSourceMap, scope::ExprScopes,
@@ -276,8 +275,8 @@ fn include_macro_invoc(
276275
}
277276

278277
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)));
278+
let root_module = crate_def_map(db, crate_id).root_module_id();
279+
let attrs = AttrFlags::query(db, AttrDefId::ModuleId(root_module));
281280
attrs.contains(AttrFlags::IS_NO_STD)
282281
}
283282

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

300299
MacroDefId {
301-
krate: loc.container.krate,
300+
krate: loc.container.krate(db),
302301
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
303302
local_inner: false,
304303
allow_internal_unsafe: loc.allow_internal_unsafe,
@@ -309,7 +308,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
309308
let loc: MacroRulesLoc = it.lookup(db);
310309

311310
MacroDefId {
312-
krate: loc.container.krate,
311+
krate: loc.container.krate(db),
313312
kind: kind(loc.expander, loc.id.file_id, loc.id.value.upcast()),
314313
local_inner: loc.flags.contains(MacroRulesLocFlags::LOCAL_INNER),
315314
allow_internal_unsafe: loc
@@ -322,7 +321,7 @@ fn macro_def(db: &dyn DefDatabase, id: MacroId) -> MacroDefId {
322321
let loc = it.lookup(db);
323322

324323
MacroDefId {
325-
krate: loc.container.krate,
324+
krate: loc.container.krate(db),
326325
kind: MacroDefKind::ProcMacro(loc.id, loc.expander, loc.kind),
327326
local_inner: false,
328327
allow_internal_unsafe: false,

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
@@ -2118,7 +2121,7 @@ impl<'db> ExprCollector<'db> {
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: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,17 @@ 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+
BlockId(
199+
3c01,
200+
),
201+
),
202+
}"#]],
196203
);
197204
}
198205

@@ -460,7 +467,7 @@ fn foo() {
460467
}
461468

462469
#[test]
463-
fn is_visible_from_same_def_map() {
470+
fn is_visible_from_same_def_map_regression_9481() {
464471
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/9481
465472
check_at(
466473
r#"
@@ -478,7 +485,6 @@ fn outer() {
478485
- tests : type
479486
480487
(block scope)::tests
481-
- name : _
482488
- outer : value (glob)
483489
484490
crate

0 commit comments

Comments
 (0)