Skip to content

Commit 8066e76

Browse files
authored
Rollup merge of #145597 - petrochenkov:nolateset, r=b-naber
resolve: Remove `ScopeSet::Late` It's better to decouple the late/early stage from scope set, because in #144131 (comment) we'll need the stage for `ScopeSet::Module` as well. See individual commits for the refactoring details. r? ``@b-naber``
2 parents 45d5109 + e26b175 commit 8066e76

File tree

6 files changed

+62
-53
lines changed

6 files changed

+62
-53
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,11 +1020,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10201020
&mut self,
10211021
suggestions: &mut Vec<TypoSuggestion>,
10221022
scope_set: ScopeSet<'ra>,
1023-
parent_scope: &ParentScope<'ra>,
1023+
ps: &ParentScope<'ra>,
10241024
ctxt: SyntaxContext,
10251025
filter_fn: &impl Fn(Res) -> bool,
10261026
) {
1027-
self.cm().visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
1027+
self.cm().visit_scopes(scope_set, ps, ctxt, None, |this, scope, use_prelude, _| {
10281028
match scope {
10291029
Scope::DeriveHelpers(expn_id) => {
10301030
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
@@ -1557,7 +1557,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15571557
});
15581558
}
15591559
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
1560-
let Ok(binding) = self.cm().early_resolve_ident_in_lexical_scope(
1560+
let Ok(binding) = self.cm().resolve_ident_in_scope_set(
15611561
ident,
15621562
ScopeSet::All(ns),
15631563
parent_scope,
@@ -2371,7 +2371,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
23712371
}
23722372
} else {
23732373
self.cm()
2374-
.early_resolve_ident_in_lexical_scope(
2374+
.resolve_ident_in_scope_set(
23752375
ident,
23762376
ScopeSet::All(ns_to_try),
23772377
parent_scope,
@@ -2474,7 +2474,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
24742474
},
24752475
)
24762476
});
2477-
if let Ok(binding) = self.cm().early_resolve_ident_in_lexical_scope(
2477+
if let Ok(binding) = self.cm().resolve_ident_in_scope_set(
24782478
ident,
24792479
ScopeSet::All(ValueNS),
24802480
parent_scope,

compiler/rustc_resolve/src/ident.rs

Lines changed: 28 additions & 31 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)]
@@ -49,6 +49,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949
scope_set: ScopeSet<'ra>,
5050
parent_scope: &ParentScope<'ra>,
5151
ctxt: SyntaxContext,
52+
derive_fallback_lint_id: Option<NodeId>,
5253
mut visitor: impl FnMut(
5354
&mut CmResolver<'r, 'ra, 'tcx>,
5455
Scope<'ra>,
@@ -99,15 +100,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
99100

100101
let rust_2015 = ctxt.edition().is_rust_2015();
101102
let (ns, macro_kind) = match scope_set {
102-
ScopeSet::All(ns)
103-
| ScopeSet::ModuleAndExternPrelude(ns, _)
104-
| ScopeSet::Late(ns, ..) => (ns, None),
103+
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None),
105104
ScopeSet::ExternPrelude => (TypeNS, None),
106105
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
107106
};
108107
let module = match scope_set {
109108
// Start with the specified module.
110-
ScopeSet::Late(_, module, _) | ScopeSet::ModuleAndExternPrelude(_, module) => module,
109+
ScopeSet::ModuleAndExternPrelude(_, module) => module,
111110
// Jump out of trait or enum modules, they do not act as scopes.
112111
_ => parent_scope.module.nearest_item_scope(),
113112
};
@@ -193,10 +192,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
193192
},
194193
Scope::Module(module, prev_lint_id) => {
195194
use_prelude = !module.no_implicit_prelude;
196-
let derive_fallback_lint_id = match scope_set {
197-
ScopeSet::Late(.., lint_id) => lint_id,
198-
_ => None,
199-
};
200195
match self.hygienic_lexical_parent(module, &mut ctxt, derive_fallback_lint_id) {
201196
Some((parent_module, lint_id)) => {
202197
Scope::Module(parent_module, lint_id.or(prev_lint_id))
@@ -349,11 +344,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
349344
return Some(LexicalScopeBinding::Item(binding));
350345
} else if let RibKind::Module(module) = rib.kind {
351346
// Encountered a module item, abandon ribs and look into that module and preludes.
347+
let parent_scope = &ParentScope { module, ..*parent_scope };
348+
let finalize = finalize.map(|f| Finalize { stage: Stage::Late, ..f });
352349
return self
353350
.cm()
354-
.early_resolve_ident_in_lexical_scope(
351+
.resolve_ident_in_scope_set(
355352
orig_ident,
356-
ScopeSet::Late(ns, module, finalize.map(|finalize| finalize.node_id)),
353+
ScopeSet::All(ns),
357354
parent_scope,
358355
finalize,
359356
finalize.is_some(),
@@ -376,13 +373,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
376373
unreachable!()
377374
}
378375

379-
/// Resolve an identifier in lexical scope.
380-
/// This is a variation of `fn resolve_ident_in_lexical_scope` that can be run during
381-
/// expansion and import resolution (perhaps they can be merged in the future).
382-
/// The function is used for resolving initial segments of macro paths (e.g., `foo` in
383-
/// `foo::bar!();` or `foo!();`) and also for import paths on 2018 edition.
376+
/// Resolve an identifier in the specified set of scopes.
384377
#[instrument(level = "debug", skip(self))]
385-
pub(crate) fn early_resolve_ident_in_lexical_scope<'r>(
378+
pub(crate) fn resolve_ident_in_scope_set<'r>(
386379
self: CmResolver<'r, 'ra, 'tcx>,
387380
orig_ident: Ident,
388381
scope_set: ScopeSet<'ra>,
@@ -411,9 +404,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
411404
}
412405

413406
let (ns, macro_kind) = match scope_set {
414-
ScopeSet::All(ns)
415-
| ScopeSet::ModuleAndExternPrelude(ns, _)
416-
| ScopeSet::Late(ns, ..) => (ns, None),
407+
ScopeSet::All(ns) | ScopeSet::ModuleAndExternPrelude(ns, _) => (ns, None),
417408
ScopeSet::ExternPrelude => (TypeNS, None),
418409
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
419410
};
@@ -437,10 +428,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
437428
}
438429

439430
// Go through all the scopes and try to resolve the name.
431+
let derive_fallback_lint_id = match finalize {
432+
Some(Finalize { node_id, stage: Stage::Late, .. }) => Some(node_id),
433+
_ => None,
434+
};
440435
let break_result = self.visit_scopes(
441436
scope_set,
442437
parent_scope,
443438
orig_ident.span.ctxt(),
439+
derive_fallback_lint_id,
444440
|this, scope, use_prelude, ctxt| {
445441
let ident = Ident::new(orig_ident.name, orig_ident.span.with_ctxt(ctxt));
446442
let result = match scope {
@@ -510,11 +506,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
510506
ident,
511507
ns,
512508
adjusted_parent_scope,
513-
if matches!(scope_set, ScopeSet::Late(..)) {
514-
Shadowing::Unrestricted
515-
} else {
516-
Shadowing::Restricted
517-
},
509+
Shadowing::Restricted,
518510
adjusted_finalize,
519511
ignore_binding,
520512
ignore_import,
@@ -643,7 +635,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
643635
return None;
644636
}
645637

646-
if finalize.is_none() || matches!(scope_set, ScopeSet::Late(..)) {
638+
// Below we report various ambiguity errors.
639+
// We do not need to report them if we are either in speculative resolution,
640+
// or in late resolution when everything is already imported and expanded
641+
// and no ambiguities exist.
642+
if matches!(finalize, None | Some(Finalize { stage: Stage::Late, .. })) {
647643
return Some(Ok(binding));
648644
}
649645

@@ -811,7 +807,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
811807
ModuleOrUniformRoot::Module(module) => module,
812808
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => {
813809
assert_eq!(shadowing, Shadowing::Unrestricted);
814-
let binding = self.early_resolve_ident_in_lexical_scope(
810+
let binding = self.resolve_ident_in_scope_set(
815811
ident,
816812
ScopeSet::ModuleAndExternPrelude(ns, module),
817813
parent_scope,
@@ -827,7 +823,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
827823
return if ns != TypeNS {
828824
Err((Determined, Weak::No))
829825
} else {
830-
let binding = self.early_resolve_ident_in_lexical_scope(
826+
let binding = self.resolve_ident_in_scope_set(
831827
ident,
832828
ScopeSet::ExternPrelude,
833829
parent_scope,
@@ -852,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
852848
}
853849
}
854850

855-
let binding = self.early_resolve_ident_in_lexical_scope(
851+
let binding = self.resolve_ident_in_scope_set(
856852
ident,
857853
ScopeSet::All(ns),
858854
parent_scope,
@@ -945,7 +941,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
945941
// Now we are in situation when new item/import can appear only from a glob or a macro
946942
// expansion. With restricted shadowing names from globs and macro expansions cannot
947943
// shadow names from outer scopes, so we can freely fallback from module search to search
948-
// in outer scopes. For `early_resolve_ident_in_lexical_scope` to continue search in outer
944+
// in outer scopes. For `resolve_ident_in_scope_set` to continue search in outer
949945
// scopes we return `Undetermined` with `Weak::Yes`.
950946

951947
// Check if one of unexpanded macros can still define the name,
@@ -1040,6 +1036,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10401036
// Forbid expanded shadowing to avoid time travel.
10411037
if let Some(shadowed_glob) = shadowed_glob
10421038
&& shadowing == Shadowing::Restricted
1039+
&& finalize.stage == Stage::Early
10431040
&& binding.expansion != LocalExpnId::ROOT
10441041
&& binding.res() != shadowed_glob.res()
10451042
{
@@ -1635,7 +1632,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16351632
_ => Err(Determinacy::determined(finalize.is_some())),
16361633
}
16371634
} else {
1638-
self.reborrow().early_resolve_ident_in_lexical_scope(
1635+
self.reborrow().resolve_ident_in_scope_set(
16391636
ident,
16401637
ScopeSet::All(ns),
16411638
parent_scope,

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14461446
return;
14471447
}
14481448

1449-
match this.cm().early_resolve_ident_in_lexical_scope(
1449+
match this.cm().resolve_ident_in_scope_set(
14501450
target,
14511451
ScopeSet::All(ns),
14521452
&import.parent_scope,

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ use crate::late::{
3838
};
3939
use crate::ty::fast_reject::SimplifiedType;
4040
use crate::{
41-
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, ScopeSet, Segment,
42-
errors, path_names_to_string,
41+
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Resolver,
42+
ScopeSet, Segment, errors, path_names_to_string,
4343
};
4444

4545
type Res = def::Res<ast::NodeId>;
@@ -2460,10 +2460,11 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24602460
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
24612461
} else if let RibKind::Module(module) = rib.kind {
24622462
// Encountered a module item, abandon ribs and look into that module and preludes.
2463+
let parent_scope = &ParentScope { module, ..self.parent_scope };
24632464
self.r.add_scope_set_candidates(
24642465
&mut names,
2465-
ScopeSet::Late(ns, module, None),
2466-
&self.parent_scope,
2466+
ScopeSet::All(ns),
2467+
parent_scope,
24672468
ctxt,
24682469
filter_fn,
24692470
);

compiler/rustc_resolve/src/lib.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +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-
/// The node id enables lints and is used for reporting them.
163-
Late(Namespace, Module<'ra>, Option<NodeId>),
164161
}
165162

166163
/// Everything you need to know about a name's location to resolve it.
@@ -1888,7 +1885,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18881885
}
18891886
}
18901887

1891-
self.cm().visit_scopes(ScopeSet::All(TypeNS), parent_scope, ctxt, |this, scope, _, _| {
1888+
let scope_set = ScopeSet::All(TypeNS);
1889+
self.cm().visit_scopes(scope_set, parent_scope, ctxt, None, |this, scope, _, _| {
18921890
match scope {
18931891
Scope::Module(module, _) => {
18941892
this.get_mut().traits_in_module(module, assoc_item, &mut found_traits);
@@ -2455,6 +2453,17 @@ fn module_to_string(mut module: Module<'_>) -> Option<String> {
24552453
Some(names_to_string(names.iter().rev().copied()))
24562454
}
24572455

2456+
#[derive(Copy, Clone, PartialEq, Debug)]
2457+
enum Stage {
2458+
/// Resolving an import or a macro.
2459+
/// Used when macro expansion is either not yet finished, or we are finalizing its results.
2460+
/// Used by default as a more restrictive variant that can produce additional errors.
2461+
Early,
2462+
/// Resolving something in late resolution when all imports are resolved
2463+
/// and all macros are expanded.
2464+
Late,
2465+
}
2466+
24582467
#[derive(Copy, Clone, Debug)]
24592468
struct Finalize {
24602469
/// Node ID for linting.
@@ -2467,9 +2476,11 @@ struct Finalize {
24672476
root_span: Span,
24682477
/// Whether to report privacy errors or silently return "no resolution" for them,
24692478
/// similarly to speculative resolution.
2470-
report_private: bool,
2479+
report_private: bool = true,
24712480
/// Tracks whether an item is used in scope or used relatively to a module.
2472-
used: Used,
2481+
used: Used = Used::Other,
2482+
/// Finalizing early or late resolution.
2483+
stage: Stage = Stage::Early,
24732484
}
24742485

24752486
impl Finalize {
@@ -2478,7 +2489,7 @@ impl Finalize {
24782489
}
24792490

24802491
fn with_root_span(node_id: NodeId, path_span: Span, root_span: Span) -> Finalize {
2481-
Finalize { node_id, path_span, root_span, report_private: true, used: Used::Other }
2492+
Finalize { node_id, path_span, root_span, .. }
24822493
}
24832494
}
24842495

compiler/rustc_resolve/src/macros.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
789789
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
790790
res
791791
} else {
792-
let binding = self.reborrow().early_resolve_ident_in_lexical_scope(
792+
let binding = self.reborrow().resolve_ident_in_scope_set(
793793
path[0].ident,
794794
ScopeSet::Macro(kind),
795795
parent_scope,
@@ -951,7 +951,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
951951
// FIXME: Should be an output of Speculative Resolution.
952952
let macro_resolutions = self.single_segment_macro_resolutions.take();
953953
for (ident, kind, parent_scope, initial_binding, sugg_span) in macro_resolutions {
954-
match self.cm().early_resolve_ident_in_lexical_scope(
954+
match self.cm().resolve_ident_in_scope_set(
955955
ident,
956956
ScopeSet::Macro(kind),
957957
&parent_scope,
@@ -1006,7 +1006,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10061006

10071007
let builtin_attrs = mem::take(&mut self.builtin_attrs);
10081008
for (ident, parent_scope) in builtin_attrs {
1009-
let _ = self.cm().early_resolve_ident_in_lexical_scope(
1009+
let _ = self.cm().resolve_ident_in_scope_set(
10101010
ident,
10111011
ScopeSet::Macro(MacroKind::Attr),
10121012
&parent_scope,
@@ -1112,7 +1112,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11121112
// If such resolution is successful and gives the same result
11131113
// (e.g. if the macro is re-imported), then silence the lint.
11141114
let no_macro_rules = self.arenas.alloc_macro_rules_scope(MacroRulesScope::Empty);
1115-
let fallback_binding = self.reborrow().early_resolve_ident_in_lexical_scope(
1115+
let fallback_binding = self.reborrow().resolve_ident_in_scope_set(
11161116
path.segments[0].ident,
11171117
ScopeSet::Macro(MacroKind::Bang),
11181118
&ParentScope { macro_rules: no_macro_rules, ..*parent_scope },

0 commit comments

Comments
 (0)