Skip to content

Commit b37407e

Browse files
committed
resolve: Remove ScopeSet::Late
The difference between `Late` and `All` only matters when `finalize` is enabled. So add a `stage` field to `Finalize` and use it instead.
1 parent 1232141 commit b37407e

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingKey, CmResolver, Determinacy,
2020
Finalize, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot,
2121
NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res, ResolutionError,
22-
Resolver, Scope, ScopeSet, Segment, Used, Weak, errors,
22+
Resolver, Scope, ScopeSet, Segment, Stage, Used, Weak, errors,
2323
};
2424

2525
#[derive(Copy, Clone)]
@@ -100,9 +100,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
100100

101101
let rust_2015 = ctxt.edition().is_rust_2015();
102102
let (ns, macro_kind) = match scope_set {
103-
ScopeSet::All(ns)
104-
| ScopeSet::ModuleAndExternPrelude(ns, _)
105-
| ScopeSet::Late(ns, ..) => (ns, None),
103+
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None),
106104
ScopeSet::ExternPrelude => (TypeNS, None),
107105
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
108106
};
@@ -347,11 +345,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
347345
} else if let RibKind::Module(module) = rib.kind {
348346
// Encountered a module item, abandon ribs and look into that module and preludes.
349347
let parent_scope = &ParentScope { module, ..*parent_scope };
348+
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
350349
return self
351350
.cm()
352351
.resolve_ident_in_scope_set(
353352
orig_ident,
354-
ScopeSet::Late(ns),
353+
ScopeSet::All(ns),
355354
parent_scope,
356355
finalize,
357356
finalize.is_some(),
@@ -405,9 +404,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
405404
}
406405

407406
let (ns, macro_kind) = match scope_set {
408-
ScopeSet::All(ns)
409-
| ScopeSet::ModuleAndExternPrelude(ns, _)
410-
| ScopeSet::Late(ns, ..) => (ns, None),
407+
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None),
411408
ScopeSet::ExternPrelude => (TypeNS, None),
412409
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
413410
};
@@ -431,8 +428,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
431428
}
432429

433430
// Go through all the scopes and try to resolve the name.
434-
let derive_fallback_lint_id = match (finalize, scope_set) {
435-
(Some(finalize), ScopeSet::Late(..)) => Some(finalize.node_id),
431+
let derive_fallback_lint_id = match finalize {
432+
Some(Finalize { node_id, stage: Stage::Late, .. }) => Some(node_id),
436433
_ => None,
437434
};
438435
let break_result = self.visit_scopes(
@@ -509,11 +506,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
509506
ident,
510507
ns,
511508
adjusted_parent_scope,
512-
if matches!(scope_set, ScopeSet::Late(..)) {
513-
Shadowing::Unrestricted
514-
} else {
515-
Shadowing::Restricted
516-
},
509+
Shadowing::Restricted,
517510
adjusted_finalize,
518511
ignore_binding,
519512
ignore_import,
@@ -642,7 +635,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
642635
return None;
643636
}
644637

645-
if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
638+
if matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })) {
646639
return Some(Ok(binding));
647640
}
648641

@@ -1039,6 +1032,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10391032
// Forbid expanded shadowing to avoid time travel.
10401033
if let Some(shadowed_glob) = shadowed_glob
10411034
&& shadowing == Shadowing::Restricted
1035+
&& finalize.stage == Stage::Early
10421036
&& binding.expansion != LocalExpnId::ROOT
10431037
&& binding.res() != shadowed_glob.res()
10441038
{

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24632463
let parent_scope = &ParentScope { module, ..self.parent_scope };
24642464
self.r.add_scope_set_candidates(
24652465
&mut names,
2466-
ScopeSet::Late(ns),
2466+
ScopeSet::All(ns),
24672467
parent_scope,
24682468
ctxt,
24692469
filter_fn,

compiler/rustc_resolve/src/lib.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,8 @@ enum ScopeSet<'ra> {
156156
ModuleAndExternPrelude(Namespace, Module<'ra>),
157157
/// Just two extern prelude scopes.
158158
ExternPrelude,
159-
/// All scopes with macro namespace and the given macro kind restriction.
159+
/// Same as `All(MacroNS)`, but with the given macro kind restriction.
160160
Macro(MacroKind),
161-
/// All scopes with the given namespace, used for partially performing late resolution.
162-
Late(Namespace),
163161
}
164162

165163
/// Everything you need to know about a name's location to resolve it.
@@ -2448,6 +2446,17 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
24482446
Some(names_to_string(names.iter().rev().copied()))
24492447
}
24502448

2449+
#[derive(Copy, Clone, PartialEq, Debug)]
2450+
enum Stage {
2451+
/// Resolving an import or a macro.
2452+
/// Used when macro expansion is either not yet finished, or we are finalizing its results.
2453+
/// Used by default as a more restrictive variant that can produce additional errors.
2454+
Early,
2455+
/// Resolving something in late resolution when all imports are resolved
2456+
/// and all macros are expanded.
2457+
Late,
2458+
}
2459+
24512460
#[derive(Copy, Clone, Debug)]
24522461
struct Finalize {
24532462
/// Node ID for linting.
@@ -2460,9 +2469,11 @@ struct Finalize {
24602469
root_span: Span,
24612470
/// Whether to report privacy errors or silently return "no resolution" for them,
24622471
/// similarly to speculative resolution.
2463-
report_private: bool,
2472+
report_private: bool = true,
24642473
/// Tracks whether an item is used in scope or used relatively to a module.
2465-
used: Used,
2474+
used: Used = Used::Other,
2475+
/// Finalizing early or late resolution.
2476+
stage: Stage = Stage::Early,
24662477
}
24672478

24682479
impl Finalize {
@@ -2471,7 +2482,7 @@ impl Finalize {
24712482
}
24722483

24732484
fn with_root_span(node_id: NodeId, path_span: Span, root_span: Span) -> Finalize {
2474-
Finalize { node_id, path_span, root_span, report_private: true, used: Used::Other }
2485+
Finalize { node_id, path_span, root_span, .. }
24752486
}
24762487
}
24772488

0 commit comments

Comments
 (0)