Skip to content

Commit 381c425

Browse files
committed
resolve: Refactor away the side table decl_parent_modules
Instead keep parent modules in `DeclData` itself
1 parent 9bc8b40 commit 381c425

4 files changed

Lines changed: 46 additions & 55 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4848
/// and report an error in case of a collision.
4949
pub(crate) fn plant_decl_into_local_module(
5050
&mut self,
51-
parent: Module<'ra>,
5251
ident: Ident,
5352
ns: Namespace,
5453
decl: Decl<'ra>,
5554
) {
56-
if let Err(old_decl) = self.try_plant_decl_into_local_module(parent, ident, ns, decl, false)
57-
{
58-
self.report_conflict(parent, ident, ns, old_decl, decl);
55+
if let Err(old_decl) = self.try_plant_decl_into_local_module(ident, ns, decl, false) {
56+
self.report_conflict(ident, ns, old_decl, decl);
5957
}
6058
}
6159

@@ -70,8 +68,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
7068
span: Span,
7169
expn_id: LocalExpnId,
7270
) {
73-
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id);
74-
self.plant_decl_into_local_module(parent, ident, ns, decl);
71+
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent));
72+
self.plant_decl_into_local_module(ident, ns, decl);
7573
}
7674

7775
/// Create a name definitinon from the given components, and put it into the extern module.
@@ -95,6 +93,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
9593
vis: CmCell::new(vis),
9694
span,
9795
expansion,
96+
parent_module: Some(parent),
9897
});
9998
// Even if underscore names cannot be looked up, we still need to add them to modules,
10099
// because they can be fetched by glob imports from those modules, and bring traits
@@ -287,7 +286,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
287286
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
288287
let span = child_span(self, reexport_chain, res);
289288
let res = res.expect_non_local();
290-
self.arenas.new_def_decl(res, vis, span, expansion)
289+
self.arenas.new_def_decl(res, vis, span, expansion, Some(parent))
291290
});
292291

293292
// Record primary definitions.
@@ -842,7 +841,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
842841
ident,
843842
local_def_id,
844843
vis,
845-
parent,
846844
);
847845
}
848846

@@ -974,10 +972,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
974972
ident: Ident,
975973
local_def_id: LocalDefId,
976974
vis: Visibility,
977-
parent: Module<'ra>,
978975
) {
979976
let sp = item.span;
980977
let parent_scope = self.parent_scope;
978+
let parent = parent_scope.module;
981979
let expansion = parent_scope.expansion;
982980

983981
let (used, module, decl) = if orig_name.is_none() && ident.name == kw::SelfLower {
@@ -1007,7 +1005,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10071005
let import = self.r.arenas.alloc_import(ImportData {
10081006
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
10091007
root_id: item.id,
1010-
parent_scope: self.parent_scope,
1008+
parent_scope,
10111009
imported_module: CmCell::new(module),
10121010
has_attributes: !item.attrs.is_empty(),
10131011
use_span_with_attributes: item.span_with_attributes(),
@@ -1055,7 +1053,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10551053
}),
10561054
};
10571055
}
1058-
self.r.plant_decl_into_local_module(parent, ident, TypeNS, import_decl);
1056+
self.r.plant_decl_into_local_module(ident, TypeNS, import_decl);
10591057
}
10601058

10611059
/// Constructs the reduced graph for one foreign item.
@@ -1298,14 +1296,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
12981296
} else {
12991297
Visibility::Restricted(CRATE_DEF_ID)
13001298
};
1301-
let decl = self.r.arenas.new_def_decl(res, vis.to_def_id(), span, expansion);
1302-
self.r.set_decl_parent_module(decl, parent_scope.module);
1299+
let decl = self.r.arenas.new_def_decl(
1300+
res,
1301+
vis.to_def_id(),
1302+
span,
1303+
expansion,
1304+
Some(parent_scope.module),
1305+
);
13031306
self.r.all_macro_rules.insert(ident.name);
13041307
if is_macro_export {
13051308
let import = self.r.arenas.alloc_import(ImportData {
13061309
kind: ImportKind::MacroExport,
13071310
root_id: item.id,
1308-
parent_scope: self.parent_scope,
1311+
parent_scope: ParentScope { module: self.r.graph_root, ..parent_scope },
13091312
imported_module: CmCell::new(None),
13101313
has_attributes: false,
13111314
use_span_with_attributes: span,
@@ -1318,7 +1321,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13181321
});
13191322
self.r.import_use_map.insert(import, Used::Other);
13201323
let import_decl = self.r.new_import_decl(decl, import);
1321-
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
1324+
self.r.plant_decl_into_local_module(ident, MacroNS, import_decl);
13221325
} else {
13231326
self.r.check_reserved_macro_name(ident, res);
13241327
self.insert_unused_macro(ident, def_id, item.id);

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,21 +208,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
208208

209209
pub(crate) fn report_conflict(
210210
&mut self,
211-
parent: Module<'_>,
212211
ident: Ident,
213212
ns: Namespace,
214213
new_binding: Decl<'ra>,
215214
old_binding: Decl<'ra>,
216215
) {
217216
// Error on the second of two conflicting names
218217
if old_binding.span.lo() > new_binding.span.lo() {
219-
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
218+
return self.report_conflict(ident, ns, old_binding, new_binding);
220219
}
221220

222-
let container = match parent.kind {
221+
let container = match old_binding.parent_module.unwrap().kind {
223222
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
224223
// indirectly *calls* the resolver, and would cause a query cycle.
225-
ModuleKind::Def(kind, _, _) => kind.descr(parent.def_id()),
224+
ModuleKind::Def(kind, def_id, _) => kind.descr(def_id),
226225
ModuleKind::Block => "block",
227226
};
228227

@@ -2032,15 +2031,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20322031
help_msgs.push(format!("use `::{ident}` to refer to this {thing} unambiguously"))
20332032
}
20342033

2035-
if let Scope::ModuleNonGlobs(module, _) | Scope::ModuleGlobs(module, _) = scope {
2036-
if module == self.graph_root {
2037-
help_msgs.push(format!(
2038-
"use `crate::{ident}` to refer to this {thing} unambiguously"
2039-
));
2040-
} else if module != self.empty_module && module.is_normal() {
2041-
help_msgs.push(format!(
2042-
"use `self::{ident}` to refer to this {thing} unambiguously"
2043-
));
2034+
if kind != AmbiguityKind::GlobVsGlob {
2035+
if let Scope::ModuleNonGlobs(module, _) | Scope::ModuleGlobs(module, _) = scope {
2036+
if module == self.graph_root {
2037+
help_msgs.push(format!(
2038+
"use `crate::{ident}` to refer to this {thing} unambiguously"
2039+
));
2040+
} else if module.is_normal() {
2041+
help_msgs.push(format!(
2042+
"use `self::{ident}` to refer to this {thing} unambiguously"
2043+
));
2044+
}
20442045
}
20452046
}
20462047

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
340340
span: import.span,
341341
vis: CmCell::new(vis),
342342
expansion: import.parent_scope.expansion,
343+
parent_module: Some(import.parent_scope.module),
343344
})
344345
}
345346

@@ -409,15 +410,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
409410
/// and return existing declaration if there is a collision.
410411
pub(crate) fn try_plant_decl_into_local_module(
411412
&mut self,
412-
module: Module<'ra>,
413413
ident: Ident,
414414
ns: Namespace,
415415
decl: Decl<'ra>,
416416
warn_ambiguity: bool,
417417
) -> Result<(), Decl<'ra>> {
418+
let module = decl.parent_module.unwrap();
418419
let res = decl.res();
419420
self.check_reserved_macro_name(ident, res);
420-
self.set_decl_parent_module(decl, module);
421421
// Even if underscore names cannot be looked up, we still need to add them to modules,
422422
// because they can be fetched by glob imports from those modules, and bring traits
423423
// into scope both directly and through glob imports.
@@ -511,7 +511,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
511511
if self.is_accessible_from(binding.vis(), scope) {
512512
let import_decl = self.new_import_decl(binding, *import);
513513
let _ = self.try_plant_decl_into_local_module(
514-
import.parent_scope.module,
515514
ident.0,
516515
key.ns,
517516
import_decl,
@@ -534,8 +533,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
534533
let dummy_decl = self.new_import_decl(dummy_decl, import);
535534
self.per_ns(|this, ns| {
536535
let module = import.parent_scope.module;
537-
let _ =
538-
this.try_plant_decl_into_local_module(module, target, ns, dummy_decl, false);
536+
let _ = this.try_plant_decl_into_local_module(target, ns, dummy_decl, false);
539537
// Don't remove underscores from `single_imports`, they were never added.
540538
if target.name != kw::Underscore {
541539
let key = BindingKey::new(target, ns);
@@ -916,7 +914,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
916914
// We need the `target`, `source` can be extracted.
917915
let import_decl = this.new_import_decl(binding, import);
918916
this.get_mut_unchecked().plant_decl_into_local_module(
919-
parent,
920917
target,
921918
ns,
922919
import_decl,
@@ -1541,7 +1538,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15411538
.and_then(|r| r.binding())
15421539
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15431540
let _ = self.try_plant_decl_into_local_module(
1544-
import.parent_scope.module,
15451541
key.ident.0,
15461542
key.ns,
15471543
import_decl,

compiler/rustc_resolve/src/lib.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ use rustc_metadata::creader::CStore;
6767
use rustc_middle::metadata::{AmbigModChild, ModChild, Reexport};
6868
use rustc_middle::middle::privacy::EffectiveVisibilities;
6969
use rustc_middle::query::Providers;
70-
use rustc_middle::span_bug;
7170
use rustc_middle::ty::{
7271
self, DelegationFnSig, DelegationInfo, Feed, MainDefinition, RegisteredTools,
7372
ResolverAstLowering, ResolverGlobalCtxt, TyCtxt, TyCtxtFeed, Visibility,
@@ -812,6 +811,7 @@ struct DeclData<'ra> {
812811
expansion: LocalExpnId,
813812
span: Span,
814813
vis: CmCell<Visibility<DefId>>,
814+
parent_module: Option<Module<'ra>>,
815815
}
816816

817817
/// All name declarations are unique and allocated on a same arena,
@@ -916,7 +916,6 @@ struct AmbiguityError<'ra> {
916916
ident: Ident,
917917
b1: Decl<'ra>,
918918
b2: Decl<'ra>,
919-
// `empty_module` in module scope serves as an unknown module here.
920919
scope1: Scope<'ra>,
921920
scope2: Scope<'ra>,
922921
warning: bool,
@@ -1180,7 +1179,6 @@ pub struct Resolver<'ra, 'tcx> {
11801179
local_module_map: FxIndexMap<LocalDefId, Module<'ra>>,
11811180
/// Lazily populated cache of modules loaded from external crates.
11821181
extern_module_map: CacheRefCell<FxIndexMap<DefId, Module<'ra>>>,
1183-
decl_parent_modules: FxHashMap<Decl<'ra>, Module<'ra>>,
11841182

11851183
/// Maps glob imports to the names of items actually imported.
11861184
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
@@ -1343,6 +1341,7 @@ impl<'ra> ResolverArenas<'ra> {
13431341
vis: Visibility<DefId>,
13441342
span: Span,
13451343
expansion: LocalExpnId,
1344+
parent_module: Option<Module<'ra>>,
13461345
) -> Decl<'ra> {
13471346
self.alloc_decl(DeclData {
13481347
kind: DeclKind::Def(res),
@@ -1351,11 +1350,12 @@ impl<'ra> ResolverArenas<'ra> {
13511350
vis: CmCell::new(vis),
13521351
span,
13531352
expansion,
1353+
parent_module,
13541354
})
13551355
}
13561356

13571357
fn new_pub_def_decl(&'ra self, res: Res, span: Span, expn_id: LocalExpnId) -> Decl<'ra> {
1358-
self.new_def_decl(res, Visibility::Public, span, expn_id)
1358+
self.new_def_decl(res, Visibility::Public, span, expn_id, None)
13591359
}
13601360

13611361
fn new_module(
@@ -1610,7 +1610,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16101610
local_module_map,
16111611
extern_module_map: Default::default(),
16121612
block_map: Default::default(),
1613-
decl_parent_modules: FxHashMap::default(),
16141613
ast_transform_scopes: FxHashMap::default(),
16151614

16161615
glob_map: Default::default(),
@@ -2061,8 +2060,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20612060
ident,
20622061
b1: used_decl,
20632062
b2,
2064-
scope1: Scope::ModuleGlobs(self.empty_module, None),
2065-
scope2: Scope::ModuleGlobs(self.empty_module, None),
2063+
scope1: Scope::ModuleGlobs(used_decl.parent_module.unwrap(), None),
2064+
scope2: Scope::ModuleGlobs(b2.parent_module.unwrap(), None),
20662065
warning: warn_ambiguity,
20672066
};
20682067
if !self.matches_previous_ambiguity_error(&ambiguity_error) {
@@ -2227,14 +2226,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22272226
vis.is_accessible_from(module.nearest_parent_mod(), self.tcx)
22282227
}
22292228

2230-
fn set_decl_parent_module(&mut self, decl: Decl<'ra>, module: Module<'ra>) {
2231-
if let Some(old_module) = self.decl_parent_modules.insert(decl, module) {
2232-
if module != old_module {
2233-
span_bug!(decl.span, "parent module is reset for a name declaration");
2234-
}
2235-
}
2236-
}
2237-
22382229
fn disambiguate_macro_rules_vs_modularized(
22392230
&self,
22402231
macro_rules: Decl<'ra>,
@@ -2244,13 +2235,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22442235
// is disambiguated to mitigate regressions from macro modularization.
22452236
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
22462237
//
2247-
// panic on index should be impossible, the only name_bindings passed in should be from
2238+
// Panic on unwrap should be impossible, the only name_bindings passed in should be from
22482239
// `resolve_ident_in_scope_set` which will always refer to a local binding from an
2249-
// import or macro definition
2250-
let macro_rules = &self.decl_parent_modules[&macro_rules];
2251-
let modularized = &self.decl_parent_modules[&modularized];
2240+
// import or macro definition.
2241+
let macro_rules = macro_rules.parent_module.unwrap();
2242+
let modularized = modularized.parent_module.unwrap();
22522243
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
2253-
&& modularized.is_ancestor_of(*macro_rules)
2244+
&& modularized.is_ancestor_of(macro_rules)
22542245
}
22552246

22562247
fn extern_prelude_get_item<'r>(

0 commit comments

Comments
 (0)