@@ -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 > ) {
@@ -486,7 +490,7 @@ struct BindingInfo {
486
490
binding_mode : BindingMode ,
487
491
}
488
492
489
- // Map from the name in a pattern to its binding mode.
493
+ /// Map from the name in a pattern to its binding mode.
490
494
type BindingMap = FxHashMap < Ident , BindingInfo > ;
491
495
492
496
#[ derive( Copy , Clone , PartialEq , Eq , Debug ) ]
@@ -681,13 +685,17 @@ impl<'a> PathSource<'a> {
681
685
}
682
686
}
683
687
688
+ /// Different kinds of symbols don't influence each other.
689
+ ///
690
+ /// Therefore, they have a separate universe (namespace).
684
691
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
685
692
pub enum Namespace {
686
693
TypeNS ,
687
694
ValueNS ,
688
695
MacroNS ,
689
696
}
690
697
698
+ /// Just a helper ‒ separate structure for each namespace.
691
699
#[ derive( Clone , Default , Debug ) ]
692
700
pub struct PerNS < T > {
693
701
value_ns : T ,
@@ -784,6 +792,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
784
792
}
785
793
}
786
794
795
+ /// This thing walks the whole crate in DFS manner, visiting each item, resolving names as it goes.
787
796
impl < ' a , ' tcx > Visitor < ' tcx > for Resolver < ' a > {
788
797
fn visit_item ( & mut self , item : & ' tcx Item ) {
789
798
self . resolve_item ( item) ;
@@ -910,7 +919,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
910
919
fn visit_generics ( & mut self , generics : & ' tcx Generics ) {
911
920
// For type parameter defaults, we have to ban access
912
921
// to following type parameters, as the Substs can only
913
- // provide previous type parameters as they're built.
922
+ // provide previous type parameters as they're built. We
923
+ // put all the parameters on the ban list and then remove
924
+ // them one by one as they are processed and become available.
914
925
let mut default_ban_rib = Rib :: new ( ForwardTyParamBanRibKind ) ;
915
926
default_ban_rib. bindings . extend ( generics. params . iter ( )
916
927
. filter_map ( |p| if let GenericParam :: Type ( ref tp) = * p { Some ( tp) } else { None } )
@@ -986,6 +997,17 @@ enum RibKind<'a> {
986
997
}
987
998
988
999
/// One local scope.
1000
+ ///
1001
+ /// A rib represents a scope names can live in. Note that these appear in many places, not just
1002
+ /// around braces. At any place where the list of accessible names (of the given namespace)
1003
+ /// changes or a new restrictions on the name accessibility are introduced, a new rib is put onto a
1004
+ /// stack. This may be, for example, a `let` statement (because it introduces variables), a macro,
1005
+ /// etc.
1006
+ ///
1007
+ /// Different [rib kinds](enum.RibKind) are transparent for different names.
1008
+ ///
1009
+ /// The resolution keeps a separate stack of ribs as it traverses the AST for each namespace. When
1010
+ /// resolving, the name is looked up from inside out.
989
1011
#[ derive( Debug ) ]
990
1012
struct Rib < ' a > {
991
1013
bindings : FxHashMap < Ident , Def > ,
@@ -1001,6 +1023,11 @@ impl<'a> Rib<'a> {
1001
1023
}
1002
1024
}
1003
1025
1026
+ /// An intermediate resolution result.
1027
+ ///
1028
+ /// This refers to the thing referred by a name. The difference between `Def` and `Item` is that
1029
+ /// items are visible in their whole block, while defs only from the place they are defined
1030
+ /// forward.
1004
1031
enum LexicalScopeBinding < ' a > {
1005
1032
Item ( & ' a NameBinding < ' a > ) ,
1006
1033
Def ( Def ) ,
@@ -1031,7 +1058,26 @@ enum PathResult<'a> {
1031
1058
}
1032
1059
1033
1060
enum ModuleKind {
1061
+ /// An anonymous module, eg. just a block.
1062
+ ///
1063
+ /// ```
1064
+ /// fn main() {
1065
+ /// fn f() {} // (1)
1066
+ /// { // This is an anonymous module
1067
+ /// f(); // This resolves to (2) as we are inside the block.
1068
+ /// fn f() {} // (2)
1069
+ /// }
1070
+ /// f(); // Resolves to (1)
1071
+ /// }
1072
+ /// ```
1034
1073
Block ( NodeId ) ,
1074
+ /// Any module with a name.
1075
+ ///
1076
+ /// This could be:
1077
+ ///
1078
+ /// * A normal module ‒ either `mod from_file;` or `mod from_block { }`.
1079
+ /// * A trait or an enum (it implicitly contains associated types, methods and variant
1080
+ /// constructors).
1035
1081
Def ( Def , Name ) ,
1036
1082
}
1037
1083
@@ -1316,6 +1362,9 @@ impl<'a> NameBinding<'a> {
1316
1362
}
1317
1363
1318
1364
/// Interns the names of the primitive types.
1365
+ ///
1366
+ /// All other types are defined somewhere and possibly imported, but the primitive ones need
1367
+ /// special handling, since they have no place of origin.
1319
1368
struct PrimitiveTypeTable {
1320
1369
primitive_types : FxHashMap < Name , PrimTy > ,
1321
1370
}
@@ -1350,6 +1399,8 @@ impl PrimitiveTypeTable {
1350
1399
}
1351
1400
1352
1401
/// The main resolver class.
1402
+ ///
1403
+ /// This is the visitor that walks the whole crate.
1353
1404
pub struct Resolver < ' a > {
1354
1405
session : & ' a Session ,
1355
1406
cstore : & ' a CrateStore ,
@@ -1481,6 +1532,7 @@ pub struct Resolver<'a> {
1481
1532
injected_crate : Option < Module < ' a > > ,
1482
1533
}
1483
1534
1535
+ /// Nothing really interesting here, it just provides memory for the rest of the crate.
1484
1536
pub struct ResolverArenas < ' a > {
1485
1537
modules : arena:: TypedArena < ModuleData < ' a > > ,
1486
1538
local_modules : RefCell < Vec < Module < ' a > > > ,
@@ -1526,10 +1578,12 @@ impl<'a, 'b: 'a> ty::DefIdTree for &'a Resolver<'b> {
1526
1578
match id. krate {
1527
1579
LOCAL_CRATE => self . definitions . def_key ( id. index ) . parent ,
1528
1580
_ => self . cstore . def_key ( id) . parent ,
1529
- } . map ( |index| DefId { index : index , ..id } )
1581
+ } . map ( |index| DefId { index, ..id } )
1530
1582
}
1531
1583
}
1532
1584
1585
+ /// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that
1586
+ /// the resolver is no longer needed as all the relevant information is inline.
1533
1587
impl < ' a > hir:: lowering:: Resolver for Resolver < ' a > {
1534
1588
fn resolve_hir_path ( & mut self , path : & mut hir:: Path , is_value : bool ) {
1535
1589
self . resolve_hir_path_cb ( path, is_value,
@@ -1752,6 +1806,7 @@ impl<'a> Resolver<'a> {
1752
1806
}
1753
1807
}
1754
1808
1809
+ /// Runs the function on each namespace.
1755
1810
fn per_ns < T , F : FnMut ( & mut Self , Namespace ) -> T > ( & mut self , mut f : F ) -> PerNS < T > {
1756
1811
PerNS {
1757
1812
type_ns : f ( self , TypeNS ) ,
0 commit comments