Skip to content

Commit 8443ea9

Browse files
committed
resolve: Do not cache nearest parent mod in ModuleData
1 parent 9bb77da commit 8443ea9

File tree

4 files changed

+31
-56
lines changed

4 files changed

+31
-56
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+6-25
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ impl<'a> Resolver<'a> {
146146
let module = self.arenas.alloc_module(ModuleData::new(
147147
parent,
148148
kind,
149-
def_id,
150149
self.cstore().module_expansion_untracked(def_id, &self.session),
151150
self.cstore().get_span_untracked(def_id, &self.session),
152151
));
@@ -274,7 +273,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
274273
self.r.visibilities[&def_id.expect_local()]
275274
}
276275
// Otherwise, the visibility is restricted to the nearest parent `mod` item.
277-
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod),
276+
_ => ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod()),
278277
})
279278
}
280279
ast::VisibilityKind::Restricted { ref path, id, .. } => {
@@ -773,13 +772,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
773772
no_implicit_prelude: parent.no_implicit_prelude || {
774773
self.r.session.contains_name(&item.attrs, sym::no_implicit_prelude)
775774
},
776-
..ModuleData::new(
777-
Some(parent),
778-
module_kind,
779-
def_id,
780-
expansion.to_expn_id(),
781-
item.span,
782-
)
775+
..ModuleData::new(Some(parent), module_kind, expansion.to_expn_id(), item.span)
783776
});
784777
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
785778
self.r.module_map.insert(local_def_id, module);
@@ -814,13 +807,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
814807

815808
ItemKind::Enum(_, _) => {
816809
let module_kind = ModuleKind::Def(DefKind::Enum, def_id, ident.name);
817-
let module = self.r.new_module(
818-
parent,
819-
module_kind,
820-
parent.nearest_parent_mod,
821-
expansion.to_expn_id(),
822-
item.span,
823-
);
810+
let module =
811+
self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span);
824812
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
825813
self.parent_scope.module = module;
826814
}
@@ -889,13 +877,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
889877
ItemKind::Trait(..) => {
890878
// Add all the items within to a new module.
891879
let module_kind = ModuleKind::Def(DefKind::Trait, def_id, ident.name);
892-
let module = self.r.new_module(
893-
parent,
894-
module_kind,
895-
parent.nearest_parent_mod,
896-
expansion.to_expn_id(),
897-
item.span,
898-
);
880+
let module =
881+
self.r.new_module(parent, module_kind, expansion.to_expn_id(), item.span);
899882
self.r.define(parent, ident, TypeNS, (module, vis, sp, expansion));
900883
self.parent_scope.module = module;
901884
}
@@ -935,7 +918,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
935918
let module = self.r.new_module(
936919
parent,
937920
ModuleKind::Block(block.id),
938-
parent.nearest_parent_mod,
939921
expansion.to_expn_id(),
940922
block.span,
941923
);
@@ -956,7 +938,6 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
956938
let module = self.r.new_module(
957939
parent,
958940
ModuleKind::Def(kind, def_id, ident.name),
959-
def_id,
960941
expansion.to_expn_id(),
961942
span,
962943
);

compiler/rustc_resolve/src/late.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18721872
if this.should_report_errs() {
18731873
let (err, candidates) = this.smart_resolve_report_errors(path, span, source, res);
18741874

1875-
let def_id = this.parent_scope.module.nearest_parent_mod;
1875+
let def_id = this.parent_scope.module.nearest_parent_mod();
18761876
let instead = res.is_some();
18771877
let suggestion =
18781878
if res.is_none() { this.report_missing_type_error(path) } else { None };
@@ -1940,7 +1940,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19401940

19411941
drop(parent_err);
19421942

1943-
let def_id = this.parent_scope.module.nearest_parent_mod;
1943+
let def_id = this.parent_scope.module.nearest_parent_mod();
19441944

19451945
if this.should_report_errs() {
19461946
this.r.use_injections.push(UseError {

compiler/rustc_resolve/src/lib.rs

+22-28
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,6 @@ pub struct ModuleData<'a> {
505505
/// What kind of module this is, because this may not be a `mod`.
506506
kind: ModuleKind,
507507

508-
/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
509-
/// This may be the crate root.
510-
nearest_parent_mod: DefId,
511-
512508
/// Mapping between names and their (possibly in-progress) resolutions in this module.
513509
/// Resolutions in modules from other crates are not populated until accessed.
514510
lazy_resolutions: Resolutions<'a>,
@@ -536,19 +532,16 @@ pub struct ModuleData<'a> {
536532
type Module<'a> = &'a ModuleData<'a>;
537533

538534
impl<'a> ModuleData<'a> {
539-
fn new(
540-
parent: Option<Module<'a>>,
541-
kind: ModuleKind,
542-
nearest_parent_mod: DefId,
543-
expansion: ExpnId,
544-
span: Span,
545-
) -> Self {
535+
fn new(parent: Option<Module<'a>>, kind: ModuleKind, expansion: ExpnId, span: Span) -> Self {
536+
let is_foreign = match kind {
537+
ModuleKind::Def(_, def_id, _) => !def_id.is_local(),
538+
ModuleKind::Block(_) => false,
539+
};
546540
ModuleData {
547541
parent,
548542
kind,
549-
nearest_parent_mod,
550543
lazy_resolutions: Default::default(),
551-
populate_on_access: Cell::new(!nearest_parent_mod.is_local()),
544+
populate_on_access: Cell::new(is_foreign),
552545
unexpanded_invocations: Default::default(),
553546
no_implicit_prelude: false,
554547
glob_importers: RefCell::new(Vec::new()),
@@ -559,6 +552,13 @@ impl<'a> ModuleData<'a> {
559552
}
560553
}
561554

555+
fn nearest_parent_mod(&self) -> DefId {
556+
match self.kind {
557+
ModuleKind::Def(DefKind::Mod, def_id, _) => def_id,
558+
_ => self.parent.expect("non-root module without parent").nearest_parent_mod(),
559+
}
560+
}
561+
562562
fn for_each_child<R, F>(&'a self, resolver: &mut R, mut f: F)
563563
where
564564
R: AsMut<Resolver<'a>>,
@@ -1260,18 +1260,12 @@ impl<'a> Resolver<'a> {
12601260
let root_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
12611261
let graph_root = arenas.alloc_module(ModuleData {
12621262
no_implicit_prelude: session.contains_name(&krate.attrs, sym::no_implicit_prelude),
1263-
..ModuleData::new(None, root_module_kind, root_def_id, ExpnId::root(), krate.span)
1263+
..ModuleData::new(None, root_module_kind, ExpnId::root(), krate.span)
12641264
});
12651265
let empty_module_kind = ModuleKind::Def(DefKind::Mod, root_def_id, kw::Empty);
12661266
let empty_module = arenas.alloc_module(ModuleData {
12671267
no_implicit_prelude: true,
1268-
..ModuleData::new(
1269-
Some(graph_root),
1270-
empty_module_kind,
1271-
root_def_id,
1272-
ExpnId::root(),
1273-
DUMMY_SP,
1274-
)
1268+
..ModuleData::new(Some(graph_root), empty_module_kind, ExpnId::root(), DUMMY_SP)
12751269
});
12761270
let mut module_map = FxHashMap::default();
12771271
module_map.insert(root_local_def_id, graph_root);
@@ -1636,11 +1630,10 @@ impl<'a> Resolver<'a> {
16361630
&self,
16371631
parent: Module<'a>,
16381632
kind: ModuleKind,
1639-
nearest_parent_mod: DefId,
16401633
expn_id: ExpnId,
16411634
span: Span,
16421635
) -> Module<'a> {
1643-
let module = ModuleData::new(Some(parent), kind, nearest_parent_mod, expn_id, span);
1636+
let module = ModuleData::new(Some(parent), kind, expn_id, span);
16441637
self.arenas.alloc_module(module)
16451638
}
16461639

@@ -2167,7 +2160,8 @@ impl<'a> Resolver<'a> {
21672160
return self.graph_root;
21682161
}
21692162
};
2170-
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod });
2163+
let module =
2164+
self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod() });
21712165
debug!(
21722166
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
21732167
ident,
@@ -2179,10 +2173,10 @@ impl<'a> Resolver<'a> {
21792173
}
21802174

21812175
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
2182-
let mut module = self.get_module(module.nearest_parent_mod);
2176+
let mut module = self.get_module(module.nearest_parent_mod());
21832177
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
21842178
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
2185-
module = self.get_module(parent.nearest_parent_mod);
2179+
module = self.get_module(parent.nearest_parent_mod());
21862180
}
21872181
module
21882182
}
@@ -2896,7 +2890,7 @@ impl<'a> Resolver<'a> {
28962890
}
28972891

28982892
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
2899-
vis.is_accessible_from(module.nearest_parent_mod, self)
2893+
vis.is_accessible_from(module.nearest_parent_mod(), self)
29002894
}
29012895

29022896
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
@@ -2920,7 +2914,7 @@ impl<'a> Resolver<'a> {
29202914
self.binding_parent_modules.get(&PtrKey(modularized)),
29212915
) {
29222916
(Some(macro_rules), Some(modularized)) => {
2923-
macro_rules.nearest_parent_mod == modularized.nearest_parent_mod
2917+
macro_rules.nearest_parent_mod() == modularized.nearest_parent_mod()
29242918
&& modularized.is_ancestor_of(macro_rules)
29252919
}
29262920
_ => false,

compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
305305
fast_print_path(path),
306306
res.opt_def_id(),
307307
res.opt_def_id().map(|macro_def_id| {
308-
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
308+
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod()
309309
}),
310310
),
311311
self.create_stable_hashing_context(),

0 commit comments

Comments
 (0)