-
Notifications
You must be signed in to change notification settings - Fork 13.7k
resolve: Remove ScopeSet::Late
#145597
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
Open
petrochenkov
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
petrochenkov:nolateset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+62
−53
Open
resolve: Remove ScopeSet::Late
#145597
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
774d96a
resolve: `early_resolve_ident_in_lexical_scope` -> `resolve_ident_in_…
petrochenkov b82b947
resolve: Remove `Module` from `ScopeSet::Late`
petrochenkov a47c372
resolve: Remove derive fallback lint id from `ScopeSet::Late`
petrochenkov e26b175
resolve: Remove `ScopeSet::Late`
petrochenkov 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -19,7 +19,7 @@ use crate::{ | |
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, CmResolver, Determinacy, | ||
Finalize, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot, | ||
NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res, ResolutionError, | ||
Resolver, Scope, ScopeSet, Segment, Used, Weak, errors, | ||
Resolver, Scope, ScopeSet, Segment, Stage, Used, Weak, errors, | ||
}; | ||
|
||
#[derive(Copy, Clone)] | ||
|
@@ -49,6 +49,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
scope_set: ScopeSet<'ra>, | ||
parent_scope: &ParentScope<'ra>, | ||
ctxt: SyntaxContext, | ||
derive_fallback_lint_id: Option<NodeId>, | ||
mut visitor: impl FnMut( | ||
&mut CmResolver<'r, 'ra, 'tcx>, | ||
Scope<'ra>, | ||
|
@@ -99,15 +100,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
|
||
let rust_2015 = ctxt.edition().is_rust_2015(); | ||
let (ns, macro_kind) = match scope_set { | ||
ScopeSet::All(ns) | ||
| ScopeSet::ModuleAndExternPrelude(ns, _) | ||
| ScopeSet::Late(ns, ..) => (ns, None), | ||
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None), | ||
ScopeSet::ExternPrelude => (TypeNS, None), | ||
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)), | ||
}; | ||
let module = match scope_set { | ||
// Start with the specified module. | ||
ScopeSet::Late(_, module, _) | ScopeSet::ModuleAndExternPrelude(_, module) => module, | ||
ScopeSet::ModuleAndExternPrelude(_, module) => module, | ||
// Jump out of trait or enum modules, they do not act as scopes. | ||
_ => parent_scope.module.nearest_item_scope(), | ||
}; | ||
|
@@ -193,10 +192,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
}, | ||
Scope::Module(module, prev_lint_id) => { | ||
use_prelude = !module.no_implicit_prelude; | ||
let derive_fallback_lint_id = match scope_set { | ||
ScopeSet::Late(.., lint_id) => lint_id, | ||
_ => None, | ||
}; | ||
match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) { | ||
Some((parent_module, lint_id)) => { | ||
Scope::Module(parent_module, lint_id.or(prev_lint_id)) | ||
|
@@ -349,11 +344,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
return Some(LexicalScopeBinding::Item(binding)); | ||
} else if let RibKind::Module(module) = rib.kind { | ||
// Encountered a module item, abandon ribs and look into that module and preludes. | ||
let parent_scope = &ParentScope { module, ..*parent_scope }; | ||
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f }); | ||
return self | ||
.cm() | ||
.early_resolve_ident_in_lexical_scope( | ||
.resolve_ident_in_scope_set( | ||
orig_ident, | ||
ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)), | ||
ScopeSet::All(ns), | ||
parent_scope, | ||
finalize, | ||
finalize.is_some(), | ||
|
@@ -376,13 +373,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
unreachable!() | ||
} | ||
|
||
/// Resolve an identifier in lexical scope. | ||
/// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during | ||
/// expansion and import resolution (perhaps they can be merged in the future). | ||
/// The function is used for resolving initial segments of macro paths (e.g., `foo` in | ||
/// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition. | ||
/// Resolve an identifier in the specified set of scopes. | ||
#[instrument(level = "debug", skip(self))] | ||
pub(crate) fn early_resolve_ident_in_lexical_scope<'r>( | ||
pub(crate) fn resolve_ident_in_scope_set<'r>( | ||
self: CmResolver<'r, 'ra, 'tcx>, | ||
orig_ident: Ident, | ||
scope_set: ScopeSet<'ra>, | ||
|
@@ -411,9 +404,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
} | ||
|
||
let (ns, macro_kind) = match scope_set { | ||
ScopeSet::All(ns) | ||
| ScopeSet::ModuleAndExternPrelude(ns, _) | ||
| ScopeSet::Late(ns, ..) => (ns, None), | ||
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None), | ||
ScopeSet::ExternPrelude => (TypeNS, None), | ||
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)), | ||
}; | ||
|
@@ -437,10 +428,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
} | ||
|
||
// Go through all the scopes and try to resolve the name. | ||
let derive_fallback_lint_id = match finalize { | ||
Some(Finalize { node_id, stage: Stage::Late, .. }) => Some(node_id), | ||
_ => None, | ||
}; | ||
let break_result = self.visit_scopes( | ||
scope_set, | ||
parent_scope, | ||
orig_ident.span.ctxt(), | ||
derive_fallback_lint_id, | ||
|this, scope, use_prelude, ctxt| { | ||
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt)); | ||
let result = match scope { | ||
|
@@ -510,11 +506,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
ident, | ||
ns, | ||
adjusted_parent_scope, | ||
if matches!(scope_set, ScopeSet::Late(..)) { | ||
Shadowing::Unrestricted | ||
} else { | ||
Shadowing::Restricted | ||
}, | ||
Shadowing::Restricted, | ||
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. Replaced with a more fine-grained stage check in |
||
adjusted_finalize, | ||
ignore_binding, | ||
ignore_import, | ||
|
@@ -643,7 +635,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
return None; | ||
} | ||
|
||
if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) { | ||
// Below we report various ambiguity errors. | ||
// We do not need to report them if we are either in speculative resolution, | ||
// or in late resolution when everything is already imported and expanded | ||
// and no ambiguities exist. | ||
if matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })) { | ||
petrochenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Some(Ok(binding)); | ||
} | ||
|
||
|
@@ -811,7 +807,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
ModuleOrUniformRoot::Module(module) => module, | ||
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => { | ||
assert_eq!(shadowing, Shadowing::Unrestricted); | ||
let binding = self.early_resolve_ident_in_lexical_scope( | ||
let binding = self.resolve_ident_in_scope_set( | ||
ident, | ||
ScopeSet::ModuleAndExternPrelude(ns, module), | ||
parent_scope, | ||
|
@@ -827,7 +823,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
return if ns != TypeNS { | ||
Err((Determined, Weak::No)) | ||
} else { | ||
let binding = self.early_resolve_ident_in_lexical_scope( | ||
let binding = self.resolve_ident_in_scope_set( | ||
ident, | ||
ScopeSet::ExternPrelude, | ||
parent_scope, | ||
|
@@ -852,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
} | ||
} | ||
|
||
let binding = self.early_resolve_ident_in_lexical_scope( | ||
let binding = self.resolve_ident_in_scope_set( | ||
ident, | ||
ScopeSet::All(ns), | ||
parent_scope, | ||
|
@@ -945,7 +941,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
// Now we are in situation when new item/import can appear only from a glob or a macro | ||
// expansion. With restricted shadowing names from globs and macro expansions cannot | ||
// shadow names from outer scopes, so we can freely fallback from module search to search | ||
// in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer | ||
// in outer scopes. For `resolve_ident_in_scope_set` to continue search in outer | ||
// scopes we return `Undetermined` with `Weak::Yes`. | ||
|
||
// Check if one of unexpanded macros can still define the name, | ||
|
@@ -1040,6 +1036,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
// Forbid expanded shadowing to avoid time travel. | ||
if let Some(shadowed_glob) = shadowed_glob | ||
&& shadowing == Shadowing::Restricted | ||
&& finalize.stage == Stage::Early | ||
&& binding.expansion != LocalExpnId::ROOT | ||
&& binding.res() != shadowed_glob.res() | ||
{ | ||
|
@@ -1635,7 +1632,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |
_ => Err(Determinacy::determined(finalize.is_some())), | ||
} | ||
} else { | ||
self.reborrow().early_resolve_ident_in_lexical_scope( | ||
self.reborrow().resolve_ident_in_scope_set( | ||
ident, | ||
ScopeSet::All(ns), | ||
parent_scope, | ||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
The formatting becomes awful if the
visit_scopes
call below becomes too long.