Skip to content

Commit ff75da8

Browse files
committed
Rename to nearest_parent_mod
* Rename `ModuleData.normal_ancestor_id` to `nearest_parent_mod` `normal_ancestor_id` is a very confusing name if you don't already understand what it means. Adding docs helps, but using a clearer and more obvious name is also important. * Rename `Resolver::nearest_mod_parent` to `nearest_parent_mod` * Add more docs
1 parent 0693198 commit ff75da8

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
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

+25-17
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),
@@ -457,18 +459,24 @@ type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution
457459

458460
/// One node in the tree of modules.
459461
///
460-
/// Note that "module" is a loose term here; it does not necessarily mean
461-
/// a `mod` that you declare in Rust code. It may also be, e.g., a trait
462-
/// or an enum. See [`ModuleKind`] (accessible through [`ModuleData::kind`]
463-
/// for all of the kinds of "modules" that resolve deals with.
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.
464471
pub struct ModuleData<'a> {
465472
/// The direct parent module (it may not be a `mod`, however).
466473
parent: Option<Module<'a>>,
467474
/// What kind of module this is, because this may not be a `mod`.
468475
kind: ModuleKind,
469476

470-
/// The [`DefId`] of the closest `mod` item ancestor (which may be this module), including crate root.
471-
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,
472480

473481
/// Mapping between names and their (possibly in-progress) resolutions in this module.
474482
/// Resolutions in modules from other crates are not populated until accessed.
@@ -500,16 +508,16 @@ impl<'a> ModuleData<'a> {
500508
fn new(
501509
parent: Option<Module<'a>>,
502510
kind: ModuleKind,
503-
normal_ancestor_id: DefId,
511+
nearest_parent_mod: DefId,
504512
expansion: ExpnId,
505513
span: Span,
506514
) -> Self {
507515
ModuleData {
508516
parent,
509517
kind,
510-
normal_ancestor_id,
518+
nearest_parent_mod,
511519
lazy_resolutions: Default::default(),
512-
populate_on_access: Cell::new(!normal_ancestor_id.is_local()),
520+
populate_on_access: Cell::new(!nearest_parent_mod.is_local()),
513521
unexpanded_invocations: Default::default(),
514522
no_implicit_prelude: false,
515523
glob_importers: RefCell::new(Vec::new()),
@@ -1527,11 +1535,11 @@ impl<'a> Resolver<'a> {
15271535
&self,
15281536
parent: Module<'a>,
15291537
kind: ModuleKind,
1530-
normal_ancestor_id: DefId,
1538+
nearest_parent_mod: DefId,
15311539
expn_id: ExpnId,
15321540
span: Span,
15331541
) -> Module<'a> {
1534-
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);
15351543
self.arenas.alloc_module(module)
15361544
}
15371545

@@ -2124,7 +2132,7 @@ impl<'a> Resolver<'a> {
21242132
return self.graph_root;
21252133
}
21262134
};
2127-
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 });
21282136
debug!(
21292137
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
21302138
ident,
@@ -2136,10 +2144,10 @@ impl<'a> Resolver<'a> {
21362144
}
21372145

21382146
fn resolve_self(&mut self, ctxt: &mut SyntaxContext, module: Module<'a>) -> Module<'a> {
2139-
let mut module = self.get_module(module.normal_ancestor_id);
2147+
let mut module = self.get_module(module.nearest_parent_mod);
21402148
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
21412149
let parent = module.parent.unwrap_or_else(|| self.macro_def_scope(ctxt.remove_mark()));
2142-
module = self.get_module(parent.normal_ancestor_id);
2150+
module = self.get_module(parent.nearest_parent_mod);
21432151
}
21442152
module
21452153
}
@@ -2801,7 +2809,7 @@ impl<'a> Resolver<'a> {
28012809
}
28022810

28032811
fn is_accessible_from(&self, vis: ty::Visibility, module: Module<'a>) -> bool {
2804-
vis.is_accessible_from(module.normal_ancestor_id, self)
2812+
vis.is_accessible_from(module.nearest_parent_mod, self)
28052813
}
28062814

28072815
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
@@ -2825,7 +2833,7 @@ impl<'a> Resolver<'a> {
28252833
self.binding_parent_modules.get(&PtrKey(modularized)),
28262834
) {
28272835
(Some(macro_rules), Some(modularized)) => {
2828-
macro_rules.normal_ancestor_id == modularized.normal_ancestor_id
2836+
macro_rules.nearest_parent_mod == modularized.nearest_parent_mod
28292837
&& modularized.is_ancestor_of(macro_rules)
28302838
}
28312839
_ => 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)