@@ -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 ) ,
@@ -456,28 +458,42 @@ struct BindingKey {
456
458
type Resolutions < ' a > = RefCell < FxIndexMap < BindingKey , & ' a RefCell < NameResolution < ' a > > > > ;
457
459
458
460
/// 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.
459
471
pub struct ModuleData < ' a > {
472
+ /// The direct parent module (it may not be a `mod`, however).
460
473
parent : Option < Module < ' a > > ,
474
+ /// What kind of module this is, because this may not be a `mod`.
461
475
kind : ModuleKind ,
462
476
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 ,
465
480
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.
468
483
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.
470
485
populate_on_access : Cell < bool > ,
471
486
472
- // Macro invocations that can expand into items in this module.
487
+ /// Macro invocations that can expand into items in this module.
473
488
unexpanded_invocations : RefCell < FxHashSet < ExpnId > > ,
474
489
490
+ /// Whether `#[no_implicit_prelude]` is active.
475
491
no_implicit_prelude : bool ,
476
492
477
493
glob_importers : RefCell < Vec < & ' a Import < ' a > > > ,
478
494
globs : RefCell < Vec < & ' a Import < ' a > > > ,
479
495
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.
481
497
traits : RefCell < Option < Box < [ ( Ident , & ' a NameBinding < ' a > ) ] > > > ,
482
498
483
499
/// Span of the module itself. Used for error reporting.
@@ -492,16 +508,16 @@ impl<'a> ModuleData<'a> {
492
508
fn new (
493
509
parent : Option < Module < ' a > > ,
494
510
kind : ModuleKind ,
495
- normal_ancestor_id : DefId ,
511
+ nearest_parent_mod : DefId ,
496
512
expansion : ExpnId ,
497
513
span : Span ,
498
514
) -> Self {
499
515
ModuleData {
500
516
parent,
501
517
kind,
502
- normal_ancestor_id ,
518
+ nearest_parent_mod ,
503
519
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 ( ) ) ,
505
521
unexpanded_invocations : Default :: default ( ) ,
506
522
no_implicit_prelude : false ,
507
523
glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
@@ -1519,11 +1535,11 @@ impl<'a> Resolver<'a> {
1519
1535
& self ,
1520
1536
parent : Module < ' a > ,
1521
1537
kind : ModuleKind ,
1522
- normal_ancestor_id : DefId ,
1538
+ nearest_parent_mod : DefId ,
1523
1539
expn_id : ExpnId ,
1524
1540
span : Span ,
1525
1541
) -> 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) ;
1527
1543
self . arenas . alloc_module ( module)
1528
1544
}
1529
1545
@@ -2116,7 +2132,7 @@ impl<'a> Resolver<'a> {
2116
2132
return self . graph_root ;
2117
2133
}
2118
2134
} ;
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 } ) ;
2120
2136
debug ! (
2121
2137
"resolve_crate_root({:?}): got module {:?} ({:?}) (ident.span = {:?})" ,
2122
2138
ident,
@@ -2128,10 +2144,10 @@ impl<'a> Resolver<'a> {
2128
2144
}
2129
2145
2130
2146
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 ) ;
2132
2148
while module. span . ctxt ( ) . normalize_to_macros_2_0 ( ) != * ctxt {
2133
2149
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 ) ;
2135
2151
}
2136
2152
module
2137
2153
}
@@ -2793,7 +2809,7 @@ impl<'a> Resolver<'a> {
2793
2809
}
2794
2810
2795
2811
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 )
2797
2813
}
2798
2814
2799
2815
fn set_binding_parent_module ( & mut self , binding : & ' a NameBinding < ' a > , module : Module < ' a > ) {
@@ -2817,7 +2833,7 @@ impl<'a> Resolver<'a> {
2817
2833
self . binding_parent_modules . get ( & PtrKey ( modularized) ) ,
2818
2834
) {
2819
2835
( 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
2821
2837
&& modularized. is_ancestor_of ( macro_rules)
2822
2838
}
2823
2839
_ => false ,
0 commit comments