Skip to content

Commit 68d6edd

Browse files
committed
Some comments and documentation for name resolution crate
1 parent c9b86a9 commit 68d6edd

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

Diff for: src/librustc_resolve/lib.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ enum ResolutionError<'a> {
162162
ForwardDeclaredTyParam,
163163
}
164164

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.
165169
fn resolve_error<'sess, 'a>(resolver: &'sess Resolver,
166170
span: Span,
167171
resolution_error: ResolutionError<'a>) {
@@ -364,7 +368,7 @@ struct BindingInfo {
364368
binding_mode: BindingMode,
365369
}
366370

367-
// Map from the name in a pattern to its binding mode.
371+
/// Map from the name in a pattern to its binding mode.
368372
type BindingMap = FxHashMap<Ident, BindingInfo>;
369373

370374
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
@@ -559,13 +563,17 @@ impl<'a> PathSource<'a> {
559563
}
560564
}
561565

566+
/// Different kinds of symbols don't influence each other.
567+
///
568+
/// Therefore, they have a separate universe (namespace).
562569
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
563570
pub enum Namespace {
564571
TypeNS,
565572
ValueNS,
566573
MacroNS,
567574
}
568575

576+
/// Just a helper ‒ separate structure for each namespace.
569577
#[derive(Clone, Default, Debug)]
570578
pub struct PerNS<T> {
571579
value_ns: T,
@@ -662,6 +670,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder {
662670
}
663671
}
664672

673+
/// This thing walks the whole crate in DFS manner, visiting each item, resolving names as it goes.
665674
impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
666675
fn visit_item(&mut self, item: &'tcx Item) {
667676
self.resolve_item(item);
@@ -788,7 +797,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> {
788797
fn visit_generics(&mut self, generics: &'tcx Generics) {
789798
// For type parameter defaults, we have to ban access
790799
// 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.
792803
let mut default_ban_rib = Rib::new(ForwardTyParamBanRibKind);
793804
default_ban_rib.bindings.extend(generics.params.iter()
794805
.filter_map(|p| if let GenericParam::Type(ref tp) = *p { Some(tp) } else { None })
@@ -864,6 +875,16 @@ enum RibKind<'a> {
864875
}
865876

866877
/// 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.
867888
#[derive(Debug)]
868889
struct Rib<'a> {
869890
bindings: FxHashMap<Ident, Def>,
@@ -879,6 +900,11 @@ impl<'a> Rib<'a> {
879900
}
880901
}
881902

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.
882908
enum LexicalScopeBinding<'a> {
883909
Item(&'a NameBinding<'a>),
884910
Def(Def),
@@ -909,7 +935,11 @@ enum PathResult<'a> {
909935
}
910936

911937
enum ModuleKind {
938+
/// Inline `mod something { ... }`.
912939
Block(NodeId),
940+
/// Module from another file.
941+
///
942+
/// Also called a normal module in the following code.
913943
Def(Def, Name),
914944
}
915945

@@ -1194,6 +1224,9 @@ impl<'a> NameBinding<'a> {
11941224
}
11951225

11961226
/// 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.
11971230
struct PrimitiveTypeTable {
11981231
primitive_types: FxHashMap<Name, PrimTy>,
11991232
}
@@ -1228,6 +1261,8 @@ impl PrimitiveTypeTable {
12281261
}
12291262

12301263
/// The main resolver class.
1264+
///
1265+
/// This is the visitor that walks the whole crate.
12311266
pub struct Resolver<'a> {
12321267
session: &'a Session,
12331268
cstore: &'a CrateStore,
@@ -1359,6 +1394,7 @@ pub struct Resolver<'a> {
13591394
injected_crate: Option<Module<'a>>,
13601395
}
13611396

1397+
/// Nothing really interesting here, it just provides memory for the rest of the crate.
13621398
pub struct ResolverArenas<'a> {
13631399
modules: arena::TypedArena<ModuleData<'a>>,
13641400
local_modules: RefCell<Vec<Module<'a>>>,
@@ -1404,10 +1440,12 @@ impl<'a, 'b: 'a> ty::DefIdTree for &'a Resolver<'b> {
14041440
match id.krate {
14051441
LOCAL_CRATE => self.definitions.def_key(id.index).parent,
14061442
_ => self.cstore.def_key(id).parent,
1407-
}.map(|index| DefId { index: index, ..id })
1443+
}.map(|index| DefId { index, ..id })
14081444
}
14091445
}
14101446

1447+
/// This is the interface through which the rest of the compiler asks about name resolution after
1448+
/// the whole AST has been indexed.
14111449
impl<'a> hir::lowering::Resolver for Resolver<'a> {
14121450
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool) {
14131451
self.resolve_hir_path_cb(path, is_value,
@@ -1630,6 +1668,7 @@ impl<'a> Resolver<'a> {
16301668
}
16311669
}
16321670

1671+
/// Runs the function on each namespace.
16331672
fn per_ns<T, F: FnMut(&mut Self, Namespace) -> T>(&mut self, mut f: F) -> PerNS<T> {
16341673
PerNS {
16351674
type_ns: f(self, TypeNS),

0 commit comments

Comments
 (0)