Skip to content

Commit ef2a512

Browse files
committed
resolve: Move parent to (Local,Extern)ModuleData
1 parent 344faa7 commit ef2a512

4 files changed

Lines changed: 31 additions & 25 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
11361136
if let Some(Attribute::Parsed(AttributeKind::MacroUse { span, arguments })) =
11371137
AttributeParser::parse_limited(self.r.tcx.sess, &item.attrs, &[sym::macro_use])
11381138
{
1139-
if self.parent_scope.module.parent.is_some() {
1139+
if self.parent_scope.module.expect_local().parent.is_some() {
11401140
self.r
11411141
.dcx()
11421142
.emit_err(errors::ExternCrateLoadingMacroNotAtCrateRoot { span: item.span });

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
31283128
return None;
31293129
};
31303130

3131-
while let Some(parent) = crate_module.parent {
3131+
while let Some(parent) = crate_module.parent() {
31323132
crate_module = parent;
31333133
}
31343134

compiler/rustc_resolve/src/ident.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
248248
}
249249

250250
if let ModuleKind::Block = module.kind {
251-
return Some((module.parent.unwrap().nearest_item_scope(), None));
251+
return Some((module.parent().unwrap().nearest_item_scope(), None));
252252
}
253253

254254
// We need to support the next case under a deprecation warning
@@ -263,7 +263,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
263263
// ```
264264
// So we have to fall back to the module's parent during lexical resolution in this case.
265265
if derive_fallback_lint_id.is_some()
266-
&& let Some(parent) = module.parent
266+
&& let Some(parent) = module.parent()
267267
// Inner module is inside the macro
268268
&& module.expansion != parent.expansion
269269
// Parent module is outside of the macro
@@ -961,7 +961,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
961961
let mut ctxt = ident.span.ctxt().normalize_to_macros_2_0();
962962
module
963963
.unwrap_or_else(|| self.resolve_self(&mut ctxt, parent_scope.module))
964-
.parent
964+
.parent()
965965
.map(|parent| self.resolve_self(&mut ctxt, parent))
966966
}
967967

compiler/rustc_resolve/src/lib.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,6 @@ type Resolutions<'ra> = CmRefCell<FxIndexMap<BindingKey, &'ra CmRefCell<NameReso
657657
///
658658
/// You can use [`CommonModuleData::kind`] to determine the kind of module this is.
659659
struct CommonModuleData<'ra> {
660-
/// The direct parent module (it may not be a `mod`, however).
661-
parent: Option<Module<'ra>>,
662660
/// What kind of module this is, because this may not be a `mod`.
663661
kind: ModuleKind,
664662
/// Mapping between names and their (possibly in-progress) resolutions in this module.
@@ -680,6 +678,8 @@ struct CommonModuleData<'ra> {
680678

681679
struct LocalModuleData<'ra> {
682680
common: CommonModuleData<'ra>,
681+
/// The direct parent module (it may not be a `mod`, however).
682+
parent: Option<LocalModule<'ra>>,
683683
/// Used to disambiguate underscore items (`const _: T = ...`) in the module.
684684
underscore_disambiguator: CmCell<u32>,
685685
/// Macro invocations that can expand into items in this module.
@@ -690,6 +690,8 @@ struct LocalModuleData<'ra> {
690690

691691
struct ExternModuleData<'ra> {
692692
common: CommonModuleData<'ra>,
693+
/// The direct parent module (it may not be a `mod`, however).
694+
parent: Option<ExternModule<'ra>>,
693695
/// True if this is a module from other crate that needs to be populated on access.
694696
populate_on_access: CacheCell<bool>,
695697
}
@@ -715,7 +717,6 @@ struct ExternModule<'ra>(Interned<'ra, ExternModuleData<'ra>>);
715717

716718
impl<'ra> CommonModuleData<'ra> {
717719
fn new(
718-
parent: Option<Module<'ra>>,
719720
kind: ModuleKind,
720721
vis: Visibility<DefId>,
721722
expansion: ExpnId,
@@ -731,7 +732,6 @@ impl<'ra> CommonModuleData<'ra> {
731732
ModuleKind::Block => None,
732733
};
733734
CommonModuleData {
734-
parent,
735735
kind,
736736
lazy_resolutions: Default::default(),
737737
no_implicit_prelude,
@@ -819,7 +819,7 @@ impl<'ra> Module<'ra> {
819819
fn nearest_item_scope(self) -> Module<'ra> {
820820
match self.kind {
821821
ModuleKind::Def(DefKind::Enum | DefKind::Trait, ..) => {
822-
self.parent.expect("enum or trait module without a parent")
822+
self.parent().expect("enum or trait module without a parent")
823823
}
824824
_ => self,
825825
}
@@ -830,7 +830,7 @@ impl<'ra> Module<'ra> {
830830
fn nearest_parent_mod(self) -> DefId {
831831
match self.kind {
832832
ModuleKind::Def(DefKind::Mod, def_id, _, _) => def_id,
833-
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
833+
_ => self.parent().expect("non-root module without parent").nearest_parent_mod(),
834834
}
835835
}
836836

@@ -839,13 +839,15 @@ impl<'ra> Module<'ra> {
839839
fn nearest_parent_mod_node_id(self) -> NodeId {
840840
match self.kind {
841841
ModuleKind::Def(DefKind::Mod, _, node_id, _) => node_id,
842-
_ => self.parent.expect("non-root module without parent").nearest_parent_mod_node_id(),
842+
_ => {
843+
self.parent().expect("non-root module without parent").nearest_parent_mod_node_id()
844+
}
843845
}
844846
}
845847

846848
fn is_ancestor_of(self, mut other: Self) -> bool {
847849
while self != other {
848-
if let Some(parent) = other.parent {
850+
if let Some(parent) = other.parent() {
849851
other = parent;
850852
} else {
851853
return false;
@@ -883,11 +885,18 @@ impl<'ra> Module<'ra> {
883885
Module::Extern(_) => false,
884886
}
885887
}
888+
889+
fn parent(self) -> Option<Module<'ra>> {
890+
match self {
891+
Module::Local(m) => m.parent.map(Module::Local),
892+
Module::Extern(m) => m.parent.map(Module::Extern),
893+
}
894+
}
886895
}
887896

888897
impl<'ra> LocalModule<'ra> {
889898
fn new(
890-
parent: Option<Module<'ra>>,
899+
parent: Option<LocalModule<'ra>>,
891900
kind: ModuleKind,
892901
vis: Visibility<DefId>,
893902
expn_id: ExpnId,
@@ -896,10 +905,10 @@ impl<'ra> LocalModule<'ra> {
896905
arenas: &'ra ResolverArenas<'ra>,
897906
) -> LocalModule<'ra> {
898907
assert!(kind.is_local());
899-
let common =
900-
CommonModuleData::new(parent, kind, vis, expn_id, span, no_implicit_prelude, arenas);
908+
let common = CommonModuleData::new(kind, vis, expn_id, span, no_implicit_prelude, arenas);
901909
let data = LocalModuleData {
902910
common,
911+
parent,
903912
underscore_disambiguator: CmCell::new(0),
904913
unexpanded_invocations: Default::default(),
905914
glob_importers: Default::default(),
@@ -919,7 +928,7 @@ impl<'ra> LocalModule<'ra> {
919928

920929
impl<'ra> ExternModule<'ra> {
921930
fn new(
922-
parent: Option<Module<'ra>>,
931+
parent: Option<ExternModule<'ra>>,
923932
kind: ModuleKind,
924933
vis: Visibility<DefId>,
925934
expn_id: ExpnId,
@@ -928,9 +937,8 @@ impl<'ra> ExternModule<'ra> {
928937
arenas: &'ra ResolverArenas<'ra>,
929938
) -> ExternModule<'ra> {
930939
assert!(!kind.is_local());
931-
let common =
932-
CommonModuleData::new(parent, kind, vis, expn_id, span, no_implicit_prelude, arenas);
933-
let data = ExternModuleData { common, populate_on_access: CacheCell::new(true) };
940+
let common = CommonModuleData::new(kind, vis, expn_id, span, no_implicit_prelude, arenas);
941+
let data = ExternModuleData { common, parent, populate_on_access: CacheCell::new(true) };
934942
ExternModule(Interned::new_unchecked(arenas.extern_modules.alloc(data)))
935943
}
936944

@@ -1904,7 +1912,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19041912
span: Span,
19051913
no_implicit_prelude: bool,
19061914
) -> LocalModule<'ra> {
1907-
let parent = parent.map(|m| m.to_module());
19081915
let vis =
19091916
kind.opt_def_id().map_or(Visibility::Public, |def_id| self.tcx.visibility(def_id));
19101917
let module =
@@ -1924,7 +1931,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
19241931
span: Span,
19251932
no_implicit_prelude: bool,
19261933
) -> ExternModule<'ra> {
1927-
let parent = parent.map(|m| m.to_module());
19281934
let vis =
19291935
kind.opt_def_id().map_or(Visibility::Public, |def_id| self.tcx.visibility(def_id));
19301936
let module =
@@ -2415,7 +2421,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24152421
fn resolve_self(&self, ctxt: &mut SyntaxContext, module: Module<'ra>) -> Module<'ra> {
24162422
let mut module = self.expect_module(module.nearest_parent_mod());
24172423
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
2418-
let parent = module.parent.unwrap_or_else(|| self.expn_def_scope(ctxt.remove_mark()));
2424+
let parent = module.parent().unwrap_or_else(|| self.expn_def_scope(ctxt.remove_mark()));
24192425
module = self.expect_module(parent.nearest_parent_mod());
24202426
}
24212427
module
@@ -2752,7 +2758,7 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
27522758
let mut names = Vec::new();
27532759
loop {
27542760
if let ModuleKind::Def(.., name) = module.kind {
2755-
if let Some(parent) = module.parent {
2761+
if let Some(parent) = module.parent() {
27562762
// `unwrap` is safe: the presence of a parent means it's not the crate root.
27572763
names.push(name.unwrap());
27582764
module = parent
@@ -2761,7 +2767,7 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
27612767
}
27622768
} else {
27632769
names.push(sym::opaque_module_name_placeholder);
2764-
let Some(parent) = module.parent else {
2770+
let Some(parent) = module.parent() else {
27652771
return None;
27662772
};
27672773
module = parent;

0 commit comments

Comments
 (0)