Skip to content

Commit 5b3d524

Browse files
committed
Auto merge of #80425 - camelid:resolve-moduledata-docs, r=petrochenkov
Document `ModuleData` and improve names - Document `ModuleData` - Rename `ModuleData.normal_ancestor_id` to `nearest_parent_mod` - Rename `Resolver::nearest_mod_parent` to `nearest_parent_mod` cc https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/mentoring/near/221029702 r? `@petrochenkov`
2 parents d7769b9 + ff75da8 commit 5b3d524

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a> Resolver<'a> {
9696

9797
/// Walks up the tree of definitions starting at `def_id`,
9898
/// stopping at the first `DefKind::Mod` encountered
99-
fn nearest_mod_parent(&mut self, def_id: DefId) -> Module<'a> {
99+
fn nearest_parent_mod(&mut self, def_id: DefId) -> Module<'a> {
100100
let def_key = self.cstore().def_key(def_id);
101101

102102
let mut parent_id = DefId {
@@ -137,7 +137,7 @@ impl<'a> Resolver<'a> {
137137
.get_opt_name()
138138
.expect("given a DefId that wasn't a module");
139139

140-
let parent = Some(self.nearest_mod_parent(def_id));
140+
let parent = Some(self.nearest_parent_mod(def_id));
141141
(name, parent)
142142
};
143143

@@ -179,7 +179,7 @@ impl<'a> Resolver<'a> {
179179
// so this hopefully won't be a problem.
180180
//
181181
// See https://github.com/rust-lang/rust/pull/77984#issuecomment-712445508
182-
self.nearest_mod_parent(def_id)
182+
self.nearest_parent_mod(def_id)
183183
}
184184
}
185185

@@ -266,7 +266,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
266266
} else {
267267
// If it's not in an enum, its visibility is restricted to the `mod` item
268268
// that it's defined in.
269-
Ok(ty::Visibility::Restricted(self.parent_scope.module.normal_ancestor_id))
269+
Ok(ty::Visibility::Restricted(self.parent_scope.module.nearest_parent_mod))
270270
}
271271
}
272272
ast::VisibilityKind::Restricted { ref path, id, .. } => {
@@ -803,7 +803,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
803803
let module = self.r.new_module(
804804
parent,
805805
module_kind,
806-
parent.normal_ancestor_id,
806+
parent.nearest_parent_mod,
807807
expansion,
808808
item.span,
809809
);
@@ -878,7 +878,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
878878
let module = self.r.new_module(
879879
parent,
880880
module_kind,
881-
parent.normal_ancestor_id,
881+
parent.nearest_parent_mod,
882882
expansion,
883883
item.span,
884884
);
@@ -921,7 +921,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
921921
let module = self.r.new_module(
922922
parent,
923923
ModuleKind::Block(block.id),
924-
parent.normal_ancestor_id,
924+
parent.nearest_parent_mod,
925925
expansion,
926926
block.span,
927927
);

compiler/rustc_resolve/src/late.rs

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

1778-
let def_id = this.parent_scope.module.normal_ancestor_id;
1778+
let def_id = this.parent_scope.module.nearest_parent_mod;
17791779
let instead = res.is_some();
17801780
let suggestion =
17811781
if res.is_none() { this.report_missing_type_error(path) } else { None };
@@ -1843,7 +1843,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
18431843

18441844
drop(parent_err);
18451845

1846-
let def_id = this.parent_scope.module.normal_ancestor_id;
1846+
let def_id = this.parent_scope.module.nearest_parent_mod;
18471847

18481848
if this.should_report_errs() {
18491849
this.r.use_injections.push(UseError {

compiler/rustc_resolve/src/lib.rs

+34-18
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,9 @@ enum ModuleKind {
422422
///
423423
/// This could be:
424424
///
425-
/// * A normal module ‒ either `mod from_file;` or `mod from_block { }`.
425+
/// * A normal module – either `mod from_file;` or `mod from_block { }` –
426+
/// or the crate root (which is conceptually a top-level module).
427+
/// Note that the crate root's [name][Self::name] will be [`kw::Empty`].
426428
/// * A trait or an enum (it implicitly contains associated types, methods and variant
427429
/// constructors).
428430
Def(DefKind, DefId, Symbol),
@@ -456,28 +458,42 @@ struct BindingKey {
456458
type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution<'a>>>>;
457459

458460
/// One node in the tree of modules.
461+
///
462+
/// Note that a "module" in resolve is broader than a `mod` that you declare in Rust code. It may be one of these:
463+
///
464+
/// * `mod`
465+
/// * crate root (aka, top-level anonymous module)
466+
/// * `enum`
467+
/// * `trait`
468+
/// * curly-braced block with statements
469+
///
470+
/// You can use [`ModuleData::kind`] to determine the kind of module this is.
459471
pub struct ModuleData<'a> {
472+
/// The direct parent module (it may not be a `mod`, however).
460473
parent: Option<Module<'a>>,
474+
/// What kind of module this is, because this may not be a `mod`.
461475
kind: ModuleKind,
462476

463-
// The def id of the closest normal module (`mod`) ancestor (including this module).
464-
normal_ancestor_id: DefId,
477+
/// The [`DefId`] of the nearest `mod` item ancestor (which may be this module).
478+
/// This may be the crate root.
479+
nearest_parent_mod: DefId,
465480

466-
// Mapping between names and their (possibly in-progress) resolutions in this module.
467-
// Resolutions in modules from other crates are not populated until accessed.
481+
/// Mapping between names and their (possibly in-progress) resolutions in this module.
482+
/// Resolutions in modules from other crates are not populated until accessed.
468483
lazy_resolutions: Resolutions<'a>,
469-
// True if this is a module from other crate that needs to be populated on access.
484+
/// True if this is a module from other crate that needs to be populated on access.
470485
populate_on_access: Cell<bool>,
471486

472-
// Macro invocations that can expand into items in this module.
487+
/// Macro invocations that can expand into items in this module.
473488
unexpanded_invocations: RefCell<FxHashSet<ExpnId>>,
474489

490+
/// Whether `#[no_implicit_prelude]` is active.
475491
no_implicit_prelude: bool,
476492

477493
glob_importers: RefCell<Vec<&'a Import<'a>>>,
478494
globs: RefCell<Vec<&'a Import<'a>>>,
479495

480-
// Used to memoize the traits in this module for faster searches through all traits in scope.
496+
/// Used to memoize the traits in this module for faster searches through all traits in scope.
481497
traits: RefCell<Option<Box<[(Ident, &'a NameBinding<'a>)]>>>,
482498

483499
/// Span of the module itself. Used for error reporting.
@@ -492,16 +508,16 @@ impl<'a> ModuleData<'a> {
492508
fn new(
493509
parent: Option<Module<'a>>,
494510
kind: ModuleKind,
495-
normal_ancestor_id: DefId,
511+
nearest_parent_mod: DefId,
496512
expansion: ExpnId,
497513
span: Span,
498514
) -> Self {
499515
ModuleData {
500516
parent,
501517
kind,
502-
normal_ancestor_id,
518+
nearest_parent_mod,
503519
lazy_resolutions: Default::default(),
504-
populate_on_access: Cell::new(!normal_ancestor_id.is_local()),
520+
populate_on_access: Cell::new(!nearest_parent_mod.is_local()),
505521
unexpanded_invocations: Default::default(),
506522
no_implicit_prelude: false,
507523
glob_importers: RefCell::new(Vec::new()),
@@ -1519,11 +1535,11 @@ impl<'a> Resolver<'a> {
15191535
&self,
15201536
parent: Module<'a>,
15211537
kind: ModuleKind,
1522-
normal_ancestor_id: DefId,
1538+
nearest_parent_mod: DefId,
15231539
expn_id: ExpnId,
15241540
span: Span,
15251541
) -> Module<'a> {
1526-
let module = ModuleData::new(Some(parent), kind, normal_ancestor_id, expn_id, span);
1542+
let module = ModuleData::new(Some(parent), kind, nearest_parent_mod, expn_id, span);
15271543
self.arenas.alloc_module(module)
15281544
}
15291545

@@ -2116,7 +2132,7 @@ impl<'a> Resolver<'a> {
21162132
return self.graph_root;
21172133
}
21182134
};
2119-
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.normal_ancestor_id });
2135+
let module = self.get_module(DefId { index: CRATE_DEF_INDEX, ..module.nearest_parent_mod });
21202136
debug!(
21212137
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
21222138
ident,
@@ -2128,10 +2144,10 @@ impl<'a> Resolver<'a> {
21282144
}
21292145

21302146
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
2131-
let mut module = self.get_module(module.normal_ancestor_id);
2147+
let mut module = self.get_module(module.nearest_parent_mod);
21322148
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
21332149
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
2134-
module = self.get_module(parent.normal_ancestor_id);
2150+
module = self.get_module(parent.nearest_parent_mod);
21352151
}
21362152
module
21372153
}
@@ -2793,7 +2809,7 @@ impl<'a> Resolver<'a> {
27932809
}
27942810

27952811
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
2796-
vis.is_accessible_from(module.normal_ancestor_id, self)
2812+
vis.is_accessible_from(module.nearest_parent_mod, self)
27972813
}
27982814

27992815
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
@@ -2817,7 +2833,7 @@ impl<'a> Resolver<'a> {
28172833
self.binding_parent_modules.get(&PtrKey(modularized)),
28182834
) {
28192835
(Some(macro_rules), Some(modularized)) => {
2820-
macro_rules.normal_ancestor_id == modularized.normal_ancestor_id
2836+
macro_rules.nearest_parent_mod == modularized.nearest_parent_mod
28212837
&& modularized.is_ancestor_of(macro_rules)
28222838
}
28232839
_ => false,

compiler/rustc_resolve/src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
328328
if after_derive {
329329
self.session.span_err(span, "macro attributes must be placed before `#[derive]`");
330330
}
331-
let normal_module_def_id = self.macro_def_scope(invoc_id).normal_ancestor_id;
331+
let normal_module_def_id = self.macro_def_scope(invoc_id).nearest_parent_mod;
332332
self.definitions.add_parent_module_of_macro_def(invoc_id, normal_module_def_id);
333333
}
334334

0 commit comments

Comments
 (0)