Skip to content

Commit c856e83

Browse files
committed
add ScopeSet::Module
1 parent d6b1848 commit c856e83

File tree

2 files changed

+82
-80
lines changed

2 files changed

+82
-80
lines changed

compiler/rustc_resolve/src/ident.rs

Lines changed: 74 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,18 +107,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
113107
let (ns, macro_kind) = match scope_set {
114108
ScopeSet::All(ns)
115109
| ScopeSet::ModuleAndExternPrelude(ns, _)
116-
| ScopeSet::Late(ns, ..) => (ns, None),
110+
| ScopeSet::Module(_, ns, _) => (ns, None),
111+
ScopeSet::Late(ns, ..) => (ns, None),
117112
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
118113
};
119114
let module = match scope_set {
120115
// Start with the specified module.
121-
ScopeSet::Late(_, module, _) | ScopeSet::ModuleAndExternPrelude(_, module) => module,
116+
ScopeSet::Late(_, module, _)
117+
| ScopeSet::ModuleAndExternPrelude(_, module)
118+
| ScopeSet::Module(module, ..) => module,
122119
// Jump out of trait or enum modules, they do not act as scopes.
123120
_ => parent_scope.module.nearest_item_scope(),
124121
};
122+
123+
let module_scope = matches!(scope_set, ScopeSet::Module(..));
125124
let module_and_extern_prelude = matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..));
126125
let mut scope = match ns {
127-
_ if module_and_extern_prelude => Scope::NonGlobModule(module, None),
126+
_ if (module_and_extern_prelude || module_scope) => Scope::NonGlobModule(module, None),
128127
TypeNS | ValueNS => Scope::NonGlobModule(module, None),
129128
MacroNS => Scope::DeriveHelpers(parent_scope.expansion),
130129
};
@@ -198,6 +197,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
198197
}
199198
ValueNS | MacroNS => break,
200199
},
200+
Scope::GlobModule(..) if module_scope => break,
201201
Scope::NonGlobModule(module, prev_lint_id) => {
202202
use_prelude = !module.no_implicit_prelude;
203203
Scope::GlobModule(module, prev_lint_id)
@@ -417,7 +417,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
417417
let (ns, macro_kind) = match scope_set {
418418
ScopeSet::All(ns)
419419
| ScopeSet::ModuleAndExternPrelude(ns, _)
420-
| ScopeSet::Late(ns, ..) => (ns, None),
420+
| ScopeSet::Module(_, ns, _) => (ns, None),
421+
ScopeSet::Late(ns, ..) => (ns, None),
421422
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind)),
422423
};
423424

@@ -500,7 +501,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
500501
},
501502
Scope::NonGlobModule(module, derive_fallback_lint_id) => {
502503
let (adjusted_parent_scope, finalize) =
503-
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
504+
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..))
505+
|| matches!(scope_set, ScopeSet::Module(..))
506+
{
504507
(parent_scope, finalize)
505508
} else {
506509
(
@@ -514,10 +517,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
514517
ident,
515518
ns,
516519
adjusted_parent_scope,
517-
if matches!(scope_set, ScopeSet::Late(..)) {
518-
Shadowing::Unrestricted
519-
} else {
520-
Shadowing::Restricted
520+
match scope_set {
521+
ScopeSet::Late(..) => Shadowing::Unrestricted,
522+
ScopeSet::Module(_, _, shadowing) => shadowing,
523+
_ => Shadowing::Restricted,
521524
},
522525
finalize,
523526
ignore_binding,
@@ -539,6 +542,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
539542
);
540543
}
541544

545+
// Don't visit Scope::GlobModule after successful resolution in
546+
// Scope::NonGlobModule with ScopeSet::Module.
547+
if matches!(scope_set, ScopeSet::Module(..)) {
548+
return Some(Ok(binding));
549+
}
550+
542551
let misc_flags = this.create_module_misc_flags(module);
543552
Ok((binding, Flags::NON_GLOB_MODULE | misc_flags))
544553
}
@@ -548,12 +557,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
548557
Err((Determinacy::Undetermined, Weak::Yes)) => {
549558
Err(Determinacy::Undetermined)
550559
}
551-
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
560+
Err((Determinacy::Determined, weak)) => {
561+
// Only go through Glob Scope with `Weak::Yes` errors in ScopeSet::Module
562+
if matches!(scope_set, ScopeSet::Module(..))
563+
&& matches!(weak, Weak::No)
564+
{
565+
return Some(Err(Determinacy::Determined));
566+
}
567+
568+
Err(Determinacy::Determined)
569+
}
552570
}
553571
}
554572
Scope::GlobModule(module, derive_fallback_lint_id) => {
555573
let (adjusted_parent_scope, finalize) =
556-
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..)) {
574+
if matches!(scope_set, ScopeSet::ModuleAndExternPrelude(..))
575+
|| matches!(scope_set, ScopeSet::Module(..))
576+
{
557577
(parent_scope, finalize)
558578
} else {
559579
(
@@ -567,10 +587,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
567587
ident,
568588
ns,
569589
adjusted_parent_scope,
570-
if matches!(scope_set, ScopeSet::Late(..)) {
571-
Shadowing::Unrestricted
572-
} else {
573-
Shadowing::Restricted
590+
match scope_set {
591+
ScopeSet::Late(..) => Shadowing::Unrestricted,
592+
ScopeSet::Module(_, _, shadowing) => shadowing,
593+
_ => Shadowing::Restricted,
574594
},
575595
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
576596
ignore_binding,
@@ -831,7 +851,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
831851
ignore_import: Option<Import<'ra>>,
832852
) -> Result<NameBinding<'ra>, Determinacy> {
833853
self.resolve_ident_in_module(module, ident, ns, parent_scope, None, None, ignore_import)
834-
.map_err(|(determinacy, _)| determinacy)
835854
}
836855

837856
#[instrument(level = "debug", skip(self))]
@@ -844,7 +863,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
844863
finalize: Option<Finalize>,
845864
ignore_binding: Option<NameBinding<'ra>>,
846865
ignore_import: Option<Import<'ra>>,
847-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
866+
) -> Result<NameBinding<'ra>, Determinacy> {
848867
let tmp_parent_scope;
849868
let mut adjusted_parent_scope = parent_scope;
850869
match module {
@@ -888,35 +907,42 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
888907
// "self-confirming" import resolutions during import validation and checking.
889908
ignore_binding: Option<NameBinding<'ra>>,
890909
ignore_import: Option<Import<'ra>>,
891-
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
892-
let module = match module {
893-
ModuleOrUniformRoot::Module(module) => module,
910+
) -> Result<NameBinding<'ra>, Determinacy> {
911+
match module {
912+
ModuleOrUniformRoot::Module(module) => self.early_resolve_ident_in_lexical_scope(
913+
ident,
914+
ScopeSet::Module(module, ns, shadowing),
915+
parent_scope,
916+
finalize,
917+
finalize.is_some(),
918+
ignore_binding,
919+
ignore_import,
920+
),
894921
ModuleOrUniformRoot::ModuleAndExternPrelude(module) => {
895922
assert_eq!(shadowing, Shadowing::Unrestricted);
896-
let binding = self.early_resolve_ident_in_lexical_scope(
923+
self.early_resolve_ident_in_lexical_scope(
897924
ident,
898925
ScopeSet::ModuleAndExternPrelude(ns, module),
899926
parent_scope,
900927
finalize,
901928
finalize.is_some(),
902929
ignore_binding,
903930
ignore_import,
904-
);
905-
return binding.map_err(|determinacy| (determinacy, Weak::No));
931+
)
906932
}
907933
ModuleOrUniformRoot::ExternPrelude => {
908934
assert_eq!(shadowing, Shadowing::Unrestricted);
909935
return if ns != TypeNS {
910-
Err((Determined, Weak::No))
936+
Err(Determined)
911937
} else if let Some(binding) =
912938
self.reborrow().extern_prelude_get(ident, finalize.is_some())
913939
{
914940
Ok(binding)
915941
} else if !self.graph_root.unexpanded_invocations.borrow().is_empty() {
916942
// Macro-expanded `extern crate` items can add names to extern prelude.
917-
Err((Undetermined, Weak::No))
943+
Err(Undetermined)
918944
} else {
919-
Err((Determined, Weak::No))
945+
Err(Determined)
920946
};
921947
}
922948
ModuleOrUniformRoot::CurrentScope => {
@@ -932,47 +958,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
932958
}
933959
}
934960

935-
let binding = self.early_resolve_ident_in_lexical_scope(
961+
self.early_resolve_ident_in_lexical_scope(
936962
ident,
937963
ScopeSet::All(ns),
938964
parent_scope,
939965
finalize,
940966
finalize.is_some(),
941967
ignore_binding,
942968
ignore_import,
943-
);
944-
return binding.map_err(|determinacy| (determinacy, Weak::No));
945-
}
946-
};
947-
948-
match self.reborrow().resolve_ident_in_non_glob_module_unadjusted(
949-
module,
950-
ident,
951-
ns,
952-
parent_scope,
953-
shadowing,
954-
finalize,
955-
ignore_binding,
956-
ignore_import,
957-
) {
958-
Ok(binding) => return Ok(binding),
959-
Err((_, Weak::No)) => {
960-
return Err((Determined, Weak::No));
969+
)
961970
}
962-
// no non-glob binding was found, check for glob binding
963-
Err((_, Weak::Yes)) => {}
964971
}
965-
966-
self.reborrow().resolve_ident_in_glob_module_unadjusted(
967-
module,
968-
ident,
969-
ns,
970-
parent_scope,
971-
shadowing,
972-
finalize,
973-
ignore_binding,
974-
ignore_import,
975-
)
976972
}
977973

978974
fn resolve_ident_in_non_glob_module_unadjusted<'r>(
@@ -1176,13 +1172,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11761172
);
11771173

11781174
match result {
1179-
Err((Determined, _)) => continue,
1175+
Err(Determined) => continue,
11801176
Ok(binding)
11811177
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
11821178
{
11831179
continue;
11841180
}
1185-
Ok(_) | Err((Undetermined, _)) => return (Undetermined, Weak::Yes),
1181+
Ok(_) | Err(Undetermined) => return (Undetermined, Weak::Yes),
11861182
}
11871183
}
11881184

@@ -1332,13 +1328,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13321328
ignore_binding,
13331329
ignore_import,
13341330
) {
1335-
Err((Determined, _)) => continue,
1331+
Err(Determined) => continue,
13361332
Ok(binding)
13371333
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
13381334
{
13391335
continue;
13401336
}
1341-
Ok(_) | Err((Undetermined, _)) => {
1337+
Ok(_) | Err(Undetermined) => {
13421338
return true;
13431339
}
13441340
}
@@ -1803,17 +1799,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18031799
}
18041800

18051801
let binding = if let Some(module) = module {
1806-
self.reborrow()
1807-
.resolve_ident_in_module(
1808-
module,
1809-
ident,
1810-
ns,
1811-
parent_scope,
1812-
finalize,
1813-
ignore_binding,
1814-
ignore_import,
1815-
)
1816-
.map_err(|(determinacy, _)| determinacy)
1802+
self.reborrow().resolve_ident_in_module(
1803+
module,
1804+
ident,
1805+
ns,
1806+
parent_scope,
1807+
finalize,
1808+
ignore_binding,
1809+
ignore_import,
1810+
)
18171811
} else if let Some(ribs) = ribs
18181812
&& let Some(TypeNS | ValueNS) = opt_ns
18191813
{

compiler/rustc_resolve/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ enum Scope<'ra> {
134134
BuiltinTypes,
135135
}
136136

137+
#[derive(Debug, PartialEq, Clone, Copy)]
138+
enum Shadowing {
139+
Restricted,
140+
Unrestricted,
141+
}
142+
137143
/// Names from different contexts may want to visit different subsets of all specific scopes
138144
/// with different restrictions when looking up the resolution.
139145
/// This enum is currently used only for early resolution (imports and macros),
@@ -149,6 +155,8 @@ enum ScopeSet<'ra> {
149155
/// All scopes with the given namespace, used for partially performing late resolution.
150156
/// The node id enables lints and is used for reporting them.
151157
Late(Namespace, Module<'ra>, Option<NodeId>),
158+
/// Scope::NonGlobModule and Scope::GlobModule.
159+
Module(Module<'ra>, Namespace, Shadowing),
152160
}
153161

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

0 commit comments

Comments
 (0)