@@ -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 {
456458type 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.
459471pub 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 ,
0 commit comments