@@ -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.
464471pub 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 ,
0 commit comments