@@ -162,6 +162,10 @@ enum ResolutionError<'a> {
162
162
ForwardDeclaredTyParam ,
163
163
}
164
164
165
+ /// Combines an error with provided span and emits it
166
+ ///
167
+ /// This takes the error provided, combines it with the span and any additional spans inside the
168
+ /// error and emits it.
165
169
fn resolve_error < ' sess , ' a > ( resolver : & ' sess Resolver ,
166
170
span : Span ,
167
171
resolution_error : ResolutionError < ' a > ) {
@@ -364,7 +368,7 @@ struct BindingInfo {
364
368
binding_mode : BindingMode ,
365
369
}
366
370
367
- // Map from the name in a pattern to its binding mode.
371
+ /// Map from the name in a pattern to its binding mode.
368
372
type BindingMap = FxHashMap < Ident , BindingInfo > ;
369
373
370
374
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -559,13 +563,17 @@ impl<'a> PathSource<'a> {
559
563
}
560
564
}
561
565
566
+ /// Different kinds of symbols don't influence each other.
567
+ ///
568
+ /// Therefore, they have a separate universe (namespace).
562
569
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
563
570
pub enum Namespace {
564
571
TypeNS ,
565
572
ValueNS ,
566
573
MacroNS ,
567
574
}
568
575
576
+ /// Just a helper ‒ separate structure for each namespace.
569
577
#[ derive( Clone , Default , Debug ) ]
570
578
pub struct PerNS < T > {
571
579
value_ns : T ,
@@ -662,6 +670,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
662
670
}
663
671
}
664
672
673
+ /// This thing walks the whole crate in DFS manner, visiting each item, resolving names as it goes.
665
674
impl < ' a , ' tcx > Visitor < ' tcx > for Resolver < ' a > {
666
675
fn visit_item ( & mut self , item : & ' tcx Item ) {
667
676
self . resolve_item ( item) ;
@@ -788,7 +797,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
788
797
fn visit_generics ( & mut self , generics : & ' tcx Generics ) {
789
798
// For type parameter defaults, we have to ban access
790
799
// to following type parameters, as the Substs can only
791
- // provide previous type parameters as they're built.
800
+ // provide previous type parameters as they're built. We
801
+ // put all the parameters on the ban list and then remove
802
+ // them one by one as they are processed and become available.
792
803
let mut default_ban_rib = Rib :: new ( ForwardTyParamBanRibKind ) ;
793
804
default_ban_rib. bindings . extend ( generics. params . iter ( )
794
805
. filter_map ( |p| if let GenericParam :: Type ( ref tp) = * p { Some ( tp) } else { None } )
@@ -864,6 +875,16 @@ enum RibKind<'a> {
864
875
}
865
876
866
877
/// One local scope.
878
+ ///
879
+ /// A rib represents a scope names can live in. Note that these appear in many places, not just
880
+ /// around braces. At any place where the list of accessible names (of the given namespace)
881
+ /// changes, a new rib is put onto a stack. This may be, for example, a `let` statement (because it
882
+ /// introduces variables), a macro, etc.
883
+ ///
884
+ /// Different [rib kinds](enum.RibKind) are transparent for different names.
885
+ ///
886
+ /// The resolution keeps a separate stack of ribs as it traverses the AST for each namespace. When
887
+ /// resolving, the name is looked up from inside out.
867
888
#[ derive( Debug ) ]
868
889
struct Rib < ' a > {
869
890
bindings : FxHashMap < Ident , Def > ,
@@ -879,6 +900,11 @@ impl<'a> Rib<'a> {
879
900
}
880
901
}
881
902
903
+ /// An intermediate resolution result.
904
+ ///
905
+ /// This refers to the thing referred by a name. The difference between `Def` and `Item` is that
906
+ /// items are visible in their whole block, while defs only from the place they are defined
907
+ /// forward.
882
908
enum LexicalScopeBinding < ' a > {
883
909
Item ( & ' a NameBinding < ' a > ) ,
884
910
Def ( Def ) ,
@@ -909,7 +935,11 @@ enum PathResult<'a> {
909
935
}
910
936
911
937
enum ModuleKind {
938
+ /// Inline `mod something { ... }`.
912
939
Block ( NodeId ) ,
940
+ /// Module from another file.
941
+ ///
942
+ /// Also called a normal module in the following code.
913
943
Def ( Def , Name ) ,
914
944
}
915
945
@@ -1194,6 +1224,9 @@ impl<'a> NameBinding<'a> {
1194
1224
}
1195
1225
1196
1226
/// Interns the names of the primitive types.
1227
+ ///
1228
+ /// All other types are defined somewhere and possibly imported, but the primitive ones need
1229
+ /// special handling, since they have no place of origin.
1197
1230
struct PrimitiveTypeTable {
1198
1231
primitive_types : FxHashMap < Name , PrimTy > ,
1199
1232
}
@@ -1228,6 +1261,8 @@ impl PrimitiveTypeTable {
1228
1261
}
1229
1262
1230
1263
/// The main resolver class.
1264
+ ///
1265
+ /// This is the visitor that walks the whole crate.
1231
1266
pub struct Resolver < ' a > {
1232
1267
session : & ' a Session ,
1233
1268
cstore : & ' a CrateStore ,
@@ -1359,6 +1394,7 @@ pub struct Resolver<'a> {
1359
1394
injected_crate : Option < Module < ' a > > ,
1360
1395
}
1361
1396
1397
+ /// Nothing really interesting here, it just provides memory for the rest of the crate.
1362
1398
pub struct ResolverArenas < ' a > {
1363
1399
modules : arena:: TypedArena < ModuleData < ' a > > ,
1364
1400
local_modules : RefCell < Vec < Module < ' a > > > ,
@@ -1404,10 +1440,12 @@ impl<'a, 'b: 'a> ty::DefIdTree for &'a Resolver<'b> {
1404
1440
match id. krate {
1405
1441
LOCAL_CRATE => self . definitions . def_key ( id. index ) . parent ,
1406
1442
_ => self . cstore . def_key ( id) . parent ,
1407
- } . map ( |index| DefId { index : index , ..id } )
1443
+ } . map ( |index| DefId { index, ..id } )
1408
1444
}
1409
1445
}
1410
1446
1447
+ /// This is the interface through which the rest of the compiler asks about name resolution after
1448
+ /// the whole AST has been indexed.
1411
1449
impl < ' a > hir:: lowering:: Resolver for Resolver < ' a > {
1412
1450
fn resolve_hir_path ( & mut self , path : & mut hir:: Path , is_value : bool ) {
1413
1451
self . resolve_hir_path_cb ( path, is_value,
@@ -1630,6 +1668,7 @@ impl<'a> Resolver<'a> {
1630
1668
}
1631
1669
}
1632
1670
1671
+ /// Runs the function on each namespace.
1633
1672
fn per_ns < T , F : FnMut ( & mut Self , Namespace ) -> T > ( & mut self , mut f : F ) -> PerNS < T > {
1634
1673
PerNS {
1635
1674
type_ns : f ( self , TypeNS ) ,
0 commit comments