@@ -422,7 +422,9 @@ enum ModuleKind {
422
422
///
423
423
/// This could be:
424
424
///
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`].
426
428
/// * A trait or an enum (it implicitly contains associated types, methods and variant
427
429
/// constructors).
428
430
Def(DefKind, DefId, Symbol),
@@ -457,18 +459,24 @@ type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution
457
459
458
460
/// One node in the tree of modules.
459
461
///
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.
464
471
pub struct ModuleData<'a> {
465
472
/// The direct parent module (it may not be a `mod`, however).
466
473
parent: Option<Module<'a>>,
467
474
/// What kind of module this is, because this may not be a `mod`.
468
475
kind: ModuleKind,
469
476
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,
472
480
473
481
/// Mapping between names and their (possibly in-progress) resolutions in this module.
474
482
/// Resolutions in modules from other crates are not populated until accessed.
@@ -500,16 +508,16 @@ impl<'a> ModuleData<'a> {
500
508
fn new(
501
509
parent: Option<Module<'a>>,
502
510
kind: ModuleKind,
503
- normal_ancestor_id : DefId,
511
+ nearest_parent_mod : DefId,
504
512
expansion: ExpnId,
505
513
span: Span,
506
514
) -> Self {
507
515
ModuleData {
508
516
parent,
509
517
kind,
510
- normal_ancestor_id ,
518
+ nearest_parent_mod ,
511
519
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()),
513
521
unexpanded_invocations: Default::default(),
514
522
no_implicit_prelude: false,
515
523
glob_importers: RefCell::new(Vec::new()),
@@ -1527,11 +1535,11 @@ impl<'a> Resolver<'a> {
1527
1535
&self,
1528
1536
parent: Module<'a>,
1529
1537
kind: ModuleKind,
1530
- normal_ancestor_id : DefId,
1538
+ nearest_parent_mod : DefId,
1531
1539
expn_id: ExpnId,
1532
1540
span: Span,
1533
1541
) -> 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);
1535
1543
self.arenas.alloc_module(module)
1536
1544
}
1537
1545
@@ -2124,7 +2132,7 @@ impl<'a> Resolver<'a> {
2124
2132
return self.graph_root;
2125
2133
}
2126
2134
};
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 });
2128
2136
debug!(
2129
2137
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})",
2130
2138
ident,
@@ -2136,10 +2144,10 @@ impl<'a> Resolver<'a> {
2136
2144
}
2137
2145
2138
2146
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 );
2140
2148
while module.span.ctxt().normalize_to_macros_2_0() != *ctxt {
2141
2149
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 );
2143
2151
}
2144
2152
module
2145
2153
}
@@ -2801,7 +2809,7 @@ impl<'a> Resolver<'a> {
2801
2809
}
2802
2810
2803
2811
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)
2805
2813
}
2806
2814
2807
2815
fn set_binding_parent_module(&mut self, binding: &'a NameBinding<'a>, module: Module<'a>) {
@@ -2825,7 +2833,7 @@ impl<'a> Resolver<'a> {
2825
2833
self.binding_parent_modules.get(&PtrKey(modularized)),
2826
2834
) {
2827
2835
(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
2829
2837
&& modularized.is_ancestor_of(macro_rules)
2830
2838
}
2831
2839
_ => false,
0 commit comments