Skip to content

Commit c4820e6

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

File tree

4 files changed

+46
-54
lines changed

4 files changed

+46
-54
lines changed

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: Macros20NormalizedIdent,
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.0, 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.0, ns, old_decl, decl);
5957
}
6058
}
6159

@@ -70,9 +68,9 @@ 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);
71+
let decl = self.arenas.new_def_decl(res, vis.to_def_id(), span, expn_id, Some(parent));
7472
let ident = Macros20NormalizedIdent::new(ident);
75-
self.plant_decl_into_local_module(parent, ident, ns, decl);
73+
self.plant_decl_into_local_module(ident, ns, decl);
7674
}
7775

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

295294
// Record primary definitions.
@@ -844,7 +843,6 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
844843
ident,
845844
local_def_id,
846845
vis,
847-
parent,
848846
);
849847
}
850848

@@ -976,10 +974,10 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
976974
ident: Ident,
977975
local_def_id: LocalDefId,
978976
vis: Visibility,
979-
parent: Module<'ra>,
980977
) {
981978
let sp = item.span;
982979
let parent_scope = self.parent_scope;
980+
let parent = parent_scope.module;
983981
let expansion = parent_scope.expansion;
984982

985983
let (used, module, decl) = if orig_name.is_none() && ident.name == kw::SelfLower {
@@ -1009,7 +1007,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10091007
let import = self.r.arenas.alloc_import(ImportData {
10101008
kind: ImportKind::ExternCrate { source: orig_name, target: ident, id: item.id },
10111009
root_id: item.id,
1012-
parent_scope: self.parent_scope,
1010+
parent_scope,
10131011
imported_module: CmCell::new(module),
10141012
has_attributes: !item.attrs.is_empty(),
10151013
use_span_with_attributes: item.span_with_attributes(),
@@ -1057,7 +1055,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
10571055
}),
10581056
};
10591057
}
1060-
self.r.plant_decl_into_local_module(parent, ident, TypeNS, import_decl);
1058+
self.r.plant_decl_into_local_module(ident, TypeNS, import_decl);
10611059
}
10621060

10631061
/// Constructs the reduced graph for one foreign item.
@@ -1300,14 +1298,19 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13001298
} else {
13011299
Visibility::Restricted(CRATE_DEF_ID)
13021300
};
1303-
let decl = self.r.arenas.new_def_decl(res, vis.to_def_id(), span, expansion);
1304-
self.r.set_decl_parent_module(decl, parent_scope.module);
1301+
let decl = self.r.arenas.new_def_decl(
1302+
res,
1303+
vis.to_def_id(),
1304+
span,
1305+
expansion,
1306+
Some(parent_scope.module),
1307+
);
13051308
self.r.all_macro_rules.insert(ident.name);
13061309
if is_macro_export {
13071310
let import = self.r.arenas.alloc_import(ImportData {
13081311
kind: ImportKind::MacroExport,
13091312
root_id: item.id,
1310-
parent_scope: self.parent_scope,
1313+
parent_scope: ParentScope { module: self.r.graph_root, ..parent_scope },
13111314
imported_module: CmCell::new(None),
13121315
has_attributes: false,
13131316
use_span_with_attributes: span,
@@ -1320,7 +1323,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
13201323
});
13211324
self.r.import_use_map.insert(import, Used::Other);
13221325
let import_decl = self.r.new_import_decl(decl, import);
1323-
self.r.plant_decl_into_local_module(self.r.graph_root, ident, MacroNS, import_decl);
1326+
self.r.plant_decl_into_local_module(ident, MacroNS, import_decl);
13241327
} else {
13251328
self.r.check_reserved_macro_name(ident.0, res);
13261329
self.insert_unused_macro(ident.0, def_id, item.id);

compiler/rustc_resolve/src/diagnostics.rs

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

211211
pub(crate) fn report_conflict(
212212
&mut self,
213-
parent: Module<'_>,
214213
ident: Ident,
215214
ns: Namespace,
216215
new_binding: Decl<'ra>,
217216
old_binding: Decl<'ra>,
218217
) {
219218
// Error on the second of two conflicting names
220219
if old_binding.span.lo() > new_binding.span.lo() {
221-
return self.report_conflict(parent, ident, ns, old_binding, new_binding);
220+
return self.report_conflict(ident, ns, old_binding, new_binding);
222221
}
223222

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

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

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

compiler/rustc_resolve/src/imports.rs

Lines changed: 3 additions & 6 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: Macros20NormalizedIdent,
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.0, 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,
516515
key.ns,
517516
import_decl,
@@ -535,7 +534,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
535534
self.per_ns(|this, ns| {
536535
let module = import.parent_scope.module;
537536
let ident = Macros20NormalizedIdent::new(target);
538-
let _ = this.try_plant_decl_into_local_module(module, ident, ns, dummy_decl, false);
537+
let _ = this.try_plant_decl_into_local_module(ident, ns, dummy_decl, false);
539538
// Don't remove underscores from `single_imports`, they were never added.
540539
if target.name != kw::Underscore {
541540
let key = BindingKey::new(ident, ns);
@@ -916,7 +915,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
916915
// We need the `target`, `source` can be extracted.
917916
let import_decl = this.new_import_decl(binding, import);
918917
this.get_mut_unchecked().plant_decl_into_local_module(
919-
parent,
920918
Macros20NormalizedIdent::new(target),
921919
ns,
922920
import_decl,
@@ -1542,7 +1540,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15421540
.and_then(|r| r.binding())
15431541
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15441542
let _ = self.try_plant_decl_into_local_module(
1545-
import.parent_scope.module,
15461543
key.ident,
15471544
key.ns,
15481545
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,
@@ -922,7 +922,6 @@ struct AmbiguityError<'ra> {
922922
ident: Ident,
923923
b1: Decl<'ra>,
924924
b2: Decl<'ra>,
925-
// `empty_module` in module scope serves as an unknown module here.
926925
scope1: Scope<'ra>,
927926
scope2: Scope<'ra>,
928927
warning: Option<AmbiguityWarning>,
@@ -1186,7 +1185,6 @@ pub struct Resolver<'ra, 'tcx> {
11861185
local_module_map: FxIndexMap<LocalDefId, Module<'ra>>,
11871186
/// Lazily populated cache of modules loaded from external crates.
11881187
extern_module_map: CacheRefCell<FxIndexMap<DefId, Module<'ra>>>,
1189-
decl_parent_modules: FxHashMap<Decl<'ra>, Module<'ra>>,
11901188

11911189
/// Maps glob imports to the names of items actually imported.
11921190
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
@@ -1349,6 +1347,7 @@ impl<'ra> ResolverArenas<'ra> {
13491347
vis: Visibility<DefId>,
13501348
span: Span,
13511349
expansion: LocalExpnId,
1350+
parent_module: Option<Module<'ra>>,
13521351
) -> Decl<'ra> {
13531352
self.alloc_decl(DeclData {
13541353
kind: DeclKind::Def(res),
@@ -1357,11 +1356,12 @@ impl<'ra> ResolverArenas<'ra> {
13571356
vis: CmCell::new(vis),
13581357
span,
13591358
expansion,
1359+
parent_module,
13601360
})
13611361
}
13621362

13631363
fn new_pub_def_decl(&'ra self, res: Res, span: Span, expn_id: LocalExpnId) -> Decl<'ra> {
1364-
self.new_def_decl(res, Visibility::Public, span, expn_id)
1364+
self.new_def_decl(res, Visibility::Public, span, expn_id, None)
13651365
}
13661366

13671367
fn new_module(
@@ -1616,7 +1616,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16161616
local_module_map,
16171617
extern_module_map: Default::default(),
16181618
block_map: Default::default(),
1619-
decl_parent_modules: FxHashMap::default(),
16201619
ast_transform_scopes: FxHashMap::default(),
16211620

16221621
glob_map: Default::default(),
@@ -2071,8 +2070,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
20712070
ident,
20722071
b1: used_decl,
20732072
b2,
2074-
scope1: Scope::ModuleGlobs(self.empty_module, None),
2075-
scope2: Scope::ModuleGlobs(self.empty_module, None),
2073+
scope1: Scope::ModuleGlobs(used_decl.parent_module.unwrap(), None),
2074+
scope2: Scope::ModuleGlobs(b2.parent_module.unwrap(), None),
20762075
warning: if warn_ambiguity { Some(AmbiguityWarning::GlobImport) } else { None },
20772076
};
20782077
if !self.matches_previous_ambiguity_error(&ambiguity_error) {
@@ -2237,14 +2236,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22372236
vis.is_accessible_from(module.nearest_parent_mod(), self.tcx)
22382237
}
22392238

2240-
fn set_decl_parent_module(&mut self, decl: Decl<'ra>, module: Module<'ra>) {
2241-
if let Some(old_module) = self.decl_parent_modules.insert(decl, module) {
2242-
if module != old_module {
2243-
span_bug!(decl.span, "parent module is reset for a name declaration");
2244-
}
2245-
}
2246-
}
2247-
22482239
fn disambiguate_macro_rules_vs_modularized(
22492240
&self,
22502241
macro_rules: Decl<'ra>,
@@ -2254,13 +2245,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
22542245
// is disambiguated to mitigate regressions from macro modularization.
22552246
// Scoping for `macro_rules` behaves like scoping for `let` at module level, in general.
22562247
//
2257-
// panic on index should be impossible, the only name_bindings passed in should be from
2248+
// Panic on unwrap should be impossible, the only name_bindings passed in should be from
22582249
// `resolve_ident_in_scope_set` which will always refer to a local binding from an
2259-
// import or macro definition
2260-
let macro_rules = &self.decl_parent_modules[&macro_rules];
2261-
let modularized = &self.decl_parent_modules[&modularized];
2250+
// import or macro definition.
2251+
let macro_rules = macro_rules.parent_module.unwrap();
2252+
let modularized = modularized.parent_module.unwrap();
22622253
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
2263-
&& modularized.is_ancestor_of(*macro_rules)
2254+
&& modularized.is_ancestor_of(macro_rules)
22642255
}
22652256

22662257
fn extern_prelude_get_item<'r>(

0 commit comments

Comments
 (0)