-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some comments and documentation for name resolution crate #48693
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,10 @@ enum ResolutionError<'a> { | |
ForwardDeclaredTyParam, | ||
} | ||
|
||
/// Combines an error with provided span and emits it | ||
/// | ||
/// This takes the error provided, combines it with the span and any additional spans inside the | ||
/// error and emits it. | ||
fn resolve_error<'sess, 'a>(resolver: &'sess Resolver, | ||
span: Span, | ||
resolution_error: ResolutionError<'a>) { | ||
|
@@ -364,7 +368,7 @@ struct BindingInfo { | |
binding_mode: BindingMode, | ||
} | ||
|
||
// Map from the name in a pattern to its binding mode. | ||
/// Map from the name in a pattern to its binding mode. | ||
type BindingMap = FxHashMap<Ident, BindingInfo>; | ||
|
||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
|
@@ -559,13 +563,17 @@ impl<'a> PathSource<'a> { | |
} | ||
} | ||
|
||
/// Different kinds of symbols don't influence each other. | ||
/// | ||
/// Therefore, they have a separate universe (namespace). | ||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] | ||
pub enum Namespace { | ||
TypeNS, | ||
ValueNS, | ||
MacroNS, | ||
} | ||
|
||
/// Just a helper ‒ separate structure for each namespace. | ||
#[derive(Clone, Default, Debug)] | ||
pub struct PerNS<T> { | ||
value_ns: T, | ||
|
@@ -662,6 +670,7 @@ impl<'tcx> Visitor<'tcx> for UsePlacementFinder { | |
} | ||
} | ||
|
||
/// This thing walks the whole crate in DFS manner, visiting each item, resolving names as it goes. | ||
impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> { | ||
fn visit_item(&mut self, item: &'tcx Item) { | ||
self.resolve_item(item); | ||
|
@@ -788,7 +797,9 @@ impl<'a, 'tcx> Visitor<'tcx> for Resolver<'a> { | |
fn visit_generics(&mut self, generics: &'tcx Generics) { | ||
// For type parameter defaults, we have to ban access | ||
// to following type parameters, as the Substs can only | ||
// provide previous type parameters as they're built. | ||
// provide previous type parameters as they're built. We | ||
// put all the parameters on the ban list and then remove | ||
// them one by one as they are processed and become available. | ||
let mut default_ban_rib = Rib::new(ForwardTyParamBanRibKind); | ||
default_ban_rib.bindings.extend(generics.params.iter() | ||
.filter_map(|p| if let GenericParam::Type(ref tp) = *p { Some(tp) } else { None }) | ||
|
@@ -864,6 +875,17 @@ enum RibKind<'a> { | |
} | ||
|
||
/// One local scope. | ||
/// | ||
/// A rib represents a scope names can live in. Note that these appear in many places, not just | ||
/// around braces. At any place where the list of accessible names (of the given namespace) | ||
/// changes or a new restrictions on the name accessibility are introduced, a new rib is put onto a | ||
/// stack. This may be, for example, a `let` statement (because it introduces variables), a macro, | ||
/// etc. | ||
/// | ||
/// Different [rib kinds](enum.RibKind) are transparent for different names. | ||
/// | ||
/// The resolution keeps a separate stack of ribs as it traverses the AST for each namespace. When | ||
/// resolving, the name is looked up from inside out. | ||
#[derive(Debug)] | ||
struct Rib<'a> { | ||
bindings: FxHashMap<Ident, Def>, | ||
|
@@ -879,6 +901,11 @@ impl<'a> Rib<'a> { | |
} | ||
} | ||
|
||
/// An intermediate resolution result. | ||
/// | ||
/// This refers to the thing referred by a name. The difference between `Def` and `Item` is that | ||
/// items are visible in their whole block, while defs only from the place they are defined | ||
/// forward. | ||
enum LexicalScopeBinding<'a> { | ||
Item(&'a NameBinding<'a>), | ||
Def(Def), | ||
|
@@ -909,7 +936,26 @@ enum PathResult<'a> { | |
} | ||
|
||
enum ModuleKind { | ||
/// An anonymous module, eg. just a block. | ||
/// | ||
/// ``` | ||
/// fn main() { | ||
/// fn f() {} // (1) | ||
/// { // This is an anonymous module | ||
/// f(); // This resolves to (2) as we are inside the block. | ||
/// fn f() {} // (2) | ||
/// } | ||
/// f(); // Resolves to (1) | ||
/// } | ||
/// ``` | ||
Block(NodeId), | ||
/// Any module with a name. | ||
/// | ||
/// This could be: | ||
/// | ||
/// * A normal module ‒ either `mod from_file;` or `mod from_block { }`. | ||
/// * A trait or an enum (it implicitly contains associated types, methods and variant | ||
/// constructors). | ||
Def(Def, Name), | ||
} | ||
|
||
|
@@ -1194,6 +1240,9 @@ impl<'a> NameBinding<'a> { | |
} | ||
|
||
/// Interns the names of the primitive types. | ||
/// | ||
/// All other types are defined somewhere and possibly imported, but the primitive ones need | ||
/// special handling, since they have no place of origin. | ||
struct PrimitiveTypeTable { | ||
primitive_types: FxHashMap<Name, PrimTy>, | ||
} | ||
|
@@ -1228,6 +1277,8 @@ impl PrimitiveTypeTable { | |
} | ||
|
||
/// The main resolver class. | ||
/// | ||
/// This is the visitor that walks the whole crate. | ||
pub struct Resolver<'a> { | ||
session: &'a Session, | ||
cstore: &'a CrateStore, | ||
|
@@ -1359,6 +1410,7 @@ pub struct Resolver<'a> { | |
injected_crate: Option<Module<'a>>, | ||
} | ||
|
||
/// Nothing really interesting here, it just provides memory for the rest of the crate. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: memory -> arenas There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is obvious it contains arenas, my point was that these arenas are there to allocate memory. Should I still change it? |
||
pub struct ResolverArenas<'a> { | ||
modules: arena::TypedArena<ModuleData<'a>>, | ||
local_modules: RefCell<Vec<Module<'a>>>, | ||
|
@@ -1404,10 +1456,12 @@ impl<'a, 'b: 'a> ty::DefIdTree for &'a Resolver<'b> { | |
match id.krate { | ||
LOCAL_CRATE => self.definitions.def_key(id.index).parent, | ||
_ => self.cstore.def_key(id).parent, | ||
}.map(|index| DefId { index: index, ..id }) | ||
}.map(|index| DefId { index, ..id }) | ||
} | ||
} | ||
|
||
/// This interface is used through the AST→HIR step, to embed full paths into the HIR. After that | ||
/// the resolver is no longer needed as all the relevant information is inline. | ||
impl<'a> hir::lowering::Resolver for Resolver<'a> { | ||
fn resolve_hir_path(&mut self, path: &mut hir::Path, is_value: bool) { | ||
self.resolve_hir_path_cb(path, is_value, | ||
|
@@ -1630,6 +1684,7 @@ impl<'a> Resolver<'a> { | |
} | ||
} | ||
|
||
/// Runs the function on each namespace. | ||
fn per_ns<T, F: FnMut(&mut Self, Namespace) -> T>(&mut self, mut f: F) -> PerNS<T> { | ||
PerNS { | ||
type_ns: f(self, TypeNS), | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably better to say it more explicitly - the list of accessible names changes or new restrictions on name accessibility are added.