Skip to content

Commit 4d7fa7c

Browse files
committed
add ScopeSet::Module
1 parent 5caedeb commit 4d7fa7c

File tree

3 files changed

+82
-81
lines changed

3 files changed

+82
-81
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10521052
}
10531053
}
10541054
}
1055-
Scope::NonGlobModule(module, _) | Scope::GlobModule(module, _) => {
1055+
Scope::NonGlobModule(module, _) => {
10561056
this.add_module_candidates(module, suggestions, filter_fn, None);
10571057
}
10581058
Scope::GlobModule(..) => {

compiler/rustc_resolve/src/ident.rs

Lines changed: 73 additions & 80 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, Shadowing, Used, Weak, errors,
2323
};
2424

2525
#[derive(Copy, Clone)]
@@ -34,12 +34,6 @@ impl From<UsePrelude> for bool {
3434
}
3535
}
3636

37-
#[derive(Debug, PartialEq, Clone, Copy)]
38-
enum Shadowing {
39-
Restricted,
40-
Unrestricted,
41-
}
42-
4337
bitflags::bitflags! {
4438
#[derive(Clone, Copy, Debug)]
4539
struct Flags: u8 {
@@ -113,20 +107,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
113107
let (ns, macro_kind) = match scope_set {
114108
ScopeSet::All(ns)
115109
| ScopeSet::ModuleAndExternPrelude(ns, _)
110+
| ScopeSet::Module(_, ns, _)
116111
| ScopeSet::Late(ns, ..) => (ns, None),
117112
ScopeSet::ExternPrelude => (TypeNS, None),
118113
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
119114
};
120115
let module = match scope_set {
121116
// Start with the specified module.
122-
ScopeSet::Late(_, module, _) | ScopeSet::ModuleAndExternPrelude(_, module) => module,
117+
ScopeSet::Late(_, module, _)
118+
| ScopeSet::ModuleAndExternPrelude(_, module)
119+
| ScopeSet::Module(module, ..) => module,
123120
// Jump out of trait or enum modules, they do not act as scopes.
124121
_ => parent_scope.module.nearest_item_scope(),
125122
};
123+
124+
let module_scope = matches!(scope_set, ScopeSet::Module(..));
126125
let module_and_extern_prelude = matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..));
127126
let extern_prelude = matches!(scope_set, ScopeSet::ExternPrelude);
128127
let mut scope = match ns {
129-
_ if module_and_extern_prelude => Scope::NonGlobModule(module, None),
128+
_ if (module_and_extern_prelude || module_scope) => Scope::NonGlobModule(module, None),
130129
_ if extern_prelude => Scope::ExternPreludeItems,
131130
TypeNS | ValueNS => Scope::NonGlobModule(module, None),
132131
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
@@ -203,6 +202,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
203202
}
204203
ValueNS | MacroNS => break,
205204
},
205+
Scope::GlobModule(..) if module_scope => break,
206206
Scope::NonGlobModule(module, prev_lint_id) => {
207207
use_prelude = !module.no_implicit_prelude;
208208
Scope::GlobModule(module, prev_lint_id)
@@ -418,6 +418,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
418418
let (ns, macro_kind) = match scope_set {
419419
ScopeSet::All(ns)
420420
| ScopeSet::ModuleAndExternPrelude(ns, _)
421+
| ScopeSet::Module(_, ns, _)
421422
| ScopeSet::Late(ns, ..) => (ns, None),
422423
ScopeSet::ExternPrelude => (TypeNS, None),
423424
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
@@ -502,7 +503,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
502503
Scope::NonGlobModule(module, derive_fallback_lint_id) => {
503504
// FIXME: use `finalize_scope` here.
504505
let (adjusted_parent_scope, adjusted_finalize) =
505-
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
506+
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..))
507+
|| matches!(scope_set, ScopeSet::Module(..))
508+
{
506509
(parent_scope, finalize)
507510
} else {
508511
(
@@ -516,10 +519,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
516519
ident,
517520
ns,
518521
adjusted_parent_scope,
519-
if matches!(scope_set, ScopeSet::Late(..)) {
520-
Shadowing::Unrestricted
521-
} else {
522-
Shadowing::Restricted
522+
match scope_set {
523+
ScopeSet::Late(..) => Shadowing::Unrestricted,
524+
ScopeSet::Module(_, _, shadowing) => shadowing,
525+
_ => Shadowing::Restricted,
523526
},
524527
adjusted_finalize,
525528
ignore_binding,
@@ -541,6 +544,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
541544
);
542545
}
543546

547+
// Don't visit Scope::GlobModule after successful resolution in
548+
// Scope::NonGlobModule with ScopeSet::Module.
549+
if matches!(scope_set, ScopeSet::Module(..)) {
550+
return Some(Ok(binding));
551+
}
552+
544553
let misc_flags = this.create_module_misc_flags(module);
545554
Ok((binding, Flags::NON_GLOB_MODULE | misc_flags))
546555
}
@@ -550,12 +559,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
550559
Err((Determinacy::Undetermined, Weak::Yes)) => {
551560
Err(Determinacy::Undetermined)
552561
}
553-
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
562+
Err((Determinacy::Determined, weak)) => {
563+
// Only go through Glob Scope with `Weak::Yes` errors in ScopeSet::Module
564+
if matches!(scope_set, ScopeSet::Module(..))
565+
&& matches!(weak, Weak::No)
566+
{
567+
return Some(Err(Determinacy::Determined));
568+
}
569+
570+
Err(Determinacy::Determined)
571+
}
554572
}
555573
}
556574
Scope::GlobModule(module, derive_fallback_lint_id) => {
557575
let (adjusted_parent_scope, finalize) =
558-
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
576+
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..))
577+
|| matches!(scope_set, ScopeSet::Module(..))
578+
{
559579
(parent_scope, finalize)
560580
} else {
561581
(
@@ -569,10 +589,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
569589
ident,
570590
ns,
571591
adjusted_parent_scope,
572-
if matches!(scope_set, ScopeSet::Late(..)) {
573-
Shadowing::Unrestricted
574-
} else {
575-
Shadowing::Restricted
592+
match scope_set {
593+
ScopeSet::Late(..) => Shadowing::Unrestricted,
594+
ScopeSet::Module(_, _, shadowing) => shadowing,
595+
_ => Shadowing::Restricted,
576596
},
577597
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
578598
ignore_binding,
@@ -828,7 +848,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
828848
ignore_import: Option<Import<'ra>>,
829849
) -> Result<NameBinding<'ra>, Determinacy> {
830850
self.resolve_ident_in_module(module, ident, ns, parent_scope, None, None, ignore_import)
831-
.map_err(|(determinacy, _)| determinacy)
832851
}
833852

834853
#[instrument(level = "debug", skip(self))]
@@ -841,7 +860,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
841860
finalize: Option<Finalize>,
842861
ignore_binding: Option<NameBinding<'ra>>,
843862
ignore_import: Option<Import<'ra>>,
844-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
863+
) -> Result<NameBinding<'ra>, Determinacy> {
845864
let tmp_parent_scope;
846865
let mut adjusted_parent_scope = parent_scope;
847866
match module {
@@ -874,7 +893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
874893
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
875894
#[instrument(level = "debug", skip(self))]
876895
fn resolve_ident_in_module_unadjusted<'r>(
877-
mut self: CmResolver<'r, 'ra, 'tcx>,
896+
self: CmResolver<'r, 'ra, 'tcx>,
878897
module: ModuleOrUniformRoot<'ra>,
879898
ident: Ident,
880899
ns: Namespace,
@@ -885,37 +904,43 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
885904
// "self-confirming" import resolutions during import validation and checking.
886905
ignore_binding: Option<NameBinding<'ra>>,
887906
ignore_import: Option<Import<'ra>>,
888-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
889-
let module = match module {
890-
ModuleOrUniformRoot::Module(module) => module,
907+
) -> Result<NameBinding<'ra>, Determinacy> {
908+
match module {
909+
ModuleOrUniformRoot::Module(module) => self.early_resolve_ident_in_lexical_scope(
910+
ident,
911+
ScopeSet::Module(module, ns, shadowing),
912+
parent_scope,
913+
finalize,
914+
finalize.is_some(),
915+
ignore_binding,
916+
ignore_import,
917+
),
891918
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => {
892919
assert_eq!(shadowing, Shadowing::Unrestricted);
893-
let binding = self.early_resolve_ident_in_lexical_scope(
920+
self.early_resolve_ident_in_lexical_scope(
894921
ident,
895922
ScopeSet::ModuleAndExternPrelude(ns, module),
896923
parent_scope,
897924
finalize,
898925
finalize.is_some(),
899926
ignore_binding,
900927
ignore_import,
901-
);
902-
return binding.map_err(|determinacy| (determinacy, Weak::No));
928+
)
903929
}
904930
ModuleOrUniformRoot::ExternPrelude => {
905931
assert_eq!(shadowing, Shadowing::Unrestricted);
906932
return if ns != TypeNS {
907-
Err((Determined, Weak::No))
933+
Err(Determined)
908934
} else {
909-
let binding = self.early_resolve_ident_in_lexical_scope(
935+
self.early_resolve_ident_in_lexical_scope(
910936
ident,
911937
ScopeSet::ExternPrelude,
912938
parent_scope,
913939
finalize,
914940
finalize.is_some(),
915941
ignore_binding,
916942
ignore_import,
917-
);
918-
return binding.map_err(|determinacy| (determinacy, Weak::No));
943+
)
919944
};
920945
}
921946
ModuleOrUniformRoot::CurrentScope => {
@@ -931,47 +956,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
931956
}
932957
}
933958

934-
let binding = self.early_resolve_ident_in_lexical_scope(
959+
self.early_resolve_ident_in_lexical_scope(
935960
ident,
936961
ScopeSet::All(ns),
937962
parent_scope,
938963
finalize,
939964
finalize.is_some(),
940965
ignore_binding,
941966
ignore_import,
942-
);
943-
return binding.map_err(|determinacy| (determinacy, Weak::No));
944-
}
945-
};
946-
947-
match self.reborrow().resolve_ident_in_non_glob_module_unadjusted(
948-
module,
949-
ident,
950-
ns,
951-
parent_scope,
952-
shadowing,
953-
finalize,
954-
ignore_binding,
955-
ignore_import,
956-
) {
957-
Ok(binding) => return Ok(binding),
958-
Err((_, Weak::No)) => {
959-
return Err((Determined, Weak::No));
967+
)
960968
}
961-
// no non-glob binding was found, check for glob binding
962-
Err((_, Weak::Yes)) => {}
963969
}
964-
965-
self.reborrow().resolve_ident_in_glob_module_unadjusted(
966-
module,
967-
ident,
968-
ns,
969-
parent_scope,
970-
shadowing,
971-
finalize,
972-
ignore_binding,
973-
ignore_import,
974-
)
975970
}
976971

977972
fn resolve_ident_in_non_glob_module_unadjusted<'r>(
@@ -1175,13 +1170,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11751170
);
11761171

11771172
match result {
1178-
Err((Determined, _)) => continue,
1173+
Err(Determined) => continue,
11791174
Ok(binding)
11801175
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
11811176
{
11821177
continue;
11831178
}
1184-
Ok(_) | Err((Undetermined, _)) => return (Undetermined, Weak::Yes),
1179+
Ok(_) | Err(Undetermined) => return (Undetermined, Weak::Yes),
11851180
}
11861181
}
11871182

@@ -1333,13 +1328,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13331328
ignore_binding,
13341329
ignore_import,
13351330
) {
1336-
Err((Determined, _)) => continue,
1331+
Err(Determined) => continue,
13371332
Ok(binding)
13381333
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
13391334
{
13401335
continue;
13411336
}
1342-
Ok(_) | Err((Undetermined, _)) => {
1337+
Ok(_) | Err(Undetermined) => {
13431338
return true;
13441339
}
13451340
}
@@ -1818,17 +1813,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18181813
}
18191814

18201815
let binding = if let Some(module) = module {
1821-
self.reborrow()
1822-
.resolve_ident_in_module(
1823-
module,
1824-
ident,
1825-
ns,
1826-
parent_scope,
1827-
finalize,
1828-
ignore_binding,
1829-
ignore_import,
1830-
)
1831-
.map_err(|(determinacy, _)| determinacy)
1816+
self.reborrow().resolve_ident_in_module(
1817+
module,
1818+
ident,
1819+
ns,
1820+
parent_scope,
1821+
finalize,
1822+
ignore_binding,
1823+
ignore_import,
1824+
)
18321825
} else if let Some(ribs) = ribs
18331826
&& let Some(TypeNS | ValueNS) = opt_ns
18341827
{

compiler/rustc_resolve/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ enum Scope<'ra> {
147147
BuiltinTypes,
148148
}
149149

150+
#[derive(Debug, PartialEq, Clone, Copy)]
151+
enum Shadowing {
152+
Restricted,
153+
Unrestricted,
154+
}
155+
150156
/// Names from different contexts may want to visit different subsets of all specific scopes
151157
/// with different restrictions when looking up the resolution.
152158
#[derive(Clone, Copy, Debug)]
@@ -162,6 +168,8 @@ enum ScopeSet<'ra> {
162168
/// All scopes with the given namespace, used for partially performing late resolution.
163169
/// The node id enables lints and is used for reporting them.
164170
Late(Namespace, Module<'ra>, Option<NodeId>),
171+
/// Scope::NonGlobModule and Scope::GlobModule.
172+
Module(Module<'ra>, Namespace, Shadowing),
165173
}
166174

167175
/// Everything you need to know about a name's location to resolve it.

0 commit comments

Comments
 (0)