Skip to content

Commit b80aba2

Browse files
committed
replace binding and shadowed_glob on NameResolution with non_glob_binding and glob_binding
1 parent 765722f commit b80aba2

File tree

7 files changed

+151
-109
lines changed

7 files changed

+151
-109
lines changed

compiler/rustc_resolve/src/check_unused.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ impl Resolver<'_, '_> {
511511
for (_key, resolution) in self.resolutions(*module).borrow().iter() {
512512
let resolution = resolution.borrow();
513513

514-
if let Some(binding) = resolution.binding
514+
if let Some(binding) = resolution.late_binding()
515515
&& let NameBindingKind::Import { import, .. } = binding.kind
516516
&& let ImportKind::Single { id, .. } = import.kind
517517
{

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1441,7 +1441,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14411441
|(key, name_resolution)| {
14421442
if key.ns == TypeNS
14431443
&& key.ident == ident
1444-
&& let Some(binding) = name_resolution.borrow().binding
1444+
&& let Some(binding) = name_resolution.borrow().late_binding()
14451445
{
14461446
match binding.res() {
14471447
// No disambiguation needed if the identically named item we

compiler/rustc_resolve/src/ident.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -904,15 +904,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
904904
let break_result = self.visit_module_scopes(ns, |this, scope| match scope {
905905
ModuleScope::NonGlobal => {
906906
// Non-glob bindings are "strong" and can be resolved immediately.
907-
if let Some(binding) = resolution.binding
908-
&& !binding.is_glob_import()
907+
if let Some(binding) = resolution.non_glob_binding
909908
&& ignore_binding.map_or(true, |b| binding != b)
910909
{
911910
if let Some(finalize) = finalize {
912-
return Some(this.finalize_module_binding(
911+
return Some(this.finalize_non_glob_module_binding(
913912
ident,
914-
Some(binding),
915-
resolution.shadowed_glob,
913+
binding,
914+
resolution.glob_binding,
916915
parent_scope,
917916
finalize,
918917
shadowing,
@@ -927,21 +926,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
927926
ModuleScope::Global => {
928927
// If we are here, any primary `resolution.binding` is either a glob, None,
929928
// or should be ignored.
930-
let binding = [resolution.binding, resolution.shadowed_glob]
931-
.into_iter()
932-
.find_map(|binding| if binding == ignore_binding { None } else { binding });
929+
let binding = resolution.glob_binding;
933930

934931
if let Some(binding) = binding {
935932
if !binding.is_glob_import() {
936-
return None;
933+
panic!("binding should be glob import {:?}", binding)
937934
}
938935
}
939936

940937
if let Some(finalize) = finalize {
941-
return Some(this.finalize_module_binding(
938+
return Some(this.finalize_glob_module_binding(
942939
ident,
943940
binding,
944-
resolution.shadowed_glob,
945941
parent_scope,
946942
finalize,
947943
shadowing,
@@ -1045,21 +1041,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10451041
Err((Determined, Weak::No))
10461042
}
10471043

1048-
fn finalize_module_binding(
1044+
fn finalize_non_glob_module_binding(
10491045
&mut self,
10501046
ident: Ident,
1051-
binding: Option<NameBinding<'ra>>,
1052-
shadowed_glob: Option<NameBinding<'ra>>,
1047+
binding: NameBinding<'ra>,
1048+
glob_binding: Option<NameBinding<'ra>>,
10531049
parent_scope: &ParentScope<'ra>,
10541050
finalize: Finalize,
10551051
shadowing: Shadowing,
10561052
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
10571053
let Finalize { path_span, report_private, used, root_span, .. } = finalize;
10581054

1059-
let Some(binding) = binding else {
1060-
return Err((Determined, Weak::No));
1061-
};
1062-
10631055
if !self.is_accessible_from(binding.vis, parent_scope.module) {
10641056
if report_private {
10651057
self.privacy_errors.push(PrivacyError {
@@ -1076,7 +1068,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10761068
}
10771069

10781070
// Forbid expanded shadowing to avoid time travel.
1079-
if let Some(shadowed_glob) = shadowed_glob
1071+
if let Some(shadowed_glob) = glob_binding
10801072
&& shadowing == Shadowing::Restricted
10811073
&& binding.expansion != LocalExpnId::ROOT
10821074
&& binding.res() != shadowed_glob.res()
@@ -1104,6 +1096,47 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11041096
return Ok(binding);
11051097
}
11061098

1099+
fn finalize_glob_module_binding(
1100+
&mut self,
1101+
ident: Ident,
1102+
binding: Option<NameBinding<'ra>>,
1103+
parent_scope: &ParentScope<'ra>,
1104+
finalize: Finalize,
1105+
shadowing: Shadowing,
1106+
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
1107+
let Finalize { path_span, report_private, used, root_span, .. } = finalize;
1108+
1109+
let Some(binding) = binding else {
1110+
return Err((Determined, Weak::No));
1111+
};
1112+
1113+
if !self.is_accessible_from(binding.vis, parent_scope.module) {
1114+
if report_private {
1115+
self.privacy_errors.push(PrivacyError {
1116+
ident,
1117+
binding,
1118+
dedup_span: path_span,
1119+
outermost_res: None,
1120+
parent_scope: *parent_scope,
1121+
single_nested: path_span != root_span,
1122+
});
1123+
} else {
1124+
return Err((Determined, Weak::No));
1125+
}
1126+
}
1127+
1128+
if shadowing == Shadowing::Unrestricted
1129+
&& binding.expansion != LocalExpnId::ROOT
1130+
&& let NameBindingKind::Import { import, .. } = binding.kind
1131+
&& matches!(import.kind, ImportKind::MacroExport)
1132+
{
1133+
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
1134+
}
1135+
1136+
self.record_use(ident, binding, used);
1137+
return Ok(binding);
1138+
}
1139+
11071140
// Checks if a single import can define the `Ident` corresponding to `binding`.
11081141
// This is used to check whether we can definitively accept a glob as a resolution.
11091142
fn single_import_can_define_name(

compiler/rustc_resolve/src/imports.rs

Lines changed: 74 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,20 @@ pub(crate) struct NameResolution<'ra> {
235235
/// Single imports that may define the name in the namespace.
236236
/// Imports are arena-allocated, so it's ok to use pointers as keys.
237237
pub single_imports: FxIndexSet<Import<'ra>>,
238-
/// The least shadowable known binding for this name, or None if there are no known bindings.
239-
pub binding: Option<NameBinding<'ra>>,
240-
pub shadowed_glob: Option<NameBinding<'ra>>,
238+
/// The least shadowable known non-glob binding for this name, or None if there are no known bindings.
239+
pub non_glob_binding: Option<NameBinding<'ra>>,
240+
pub glob_binding: Option<NameBinding<'ra>>,
241241
}
242242

243243
impl<'ra> NameResolution<'ra> {
244244
/// Returns the binding for the name if it is known or None if it not known.
245245
pub(crate) fn binding(&self) -> Option<NameBinding<'ra>> {
246-
self.binding.and_then(|binding| {
247-
if !binding.is_glob_import() || self.single_imports.is_empty() {
248-
Some(binding)
249-
} else {
250-
None
251-
}
252-
})
246+
self.non_glob_binding
247+
.or_else(|| if self.single_imports.is_empty() { self.glob_binding } else { None })
248+
}
249+
250+
pub(crate) fn late_binding(&self) -> Option<NameBinding<'ra>> {
251+
self.non_glob_binding.or_else(|| self.glob_binding)
253252
}
254253
}
255254

@@ -331,77 +330,79 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
331330
self.check_reserved_macro_name(key.ident, res);
332331
self.set_binding_parent_module(binding, module);
333332
self.update_resolution(module, key, warn_ambiguity, |this, resolution| {
334-
if let Some(old_binding) = resolution.binding {
335-
if res == Res::Err && old_binding.res() != Res::Err {
333+
if let Some(old_non_glob_binding) = resolution.non_glob_binding {
334+
if res == Res::Err && old_non_glob_binding.res() != Res::Err {
336335
// Do not override real bindings with `Res::Err`s from error recovery.
337336
return Ok(());
338337
}
339-
match (old_binding.is_glob_import(), binding.is_glob_import()) {
340-
(true, true) => {
341-
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
342-
if !binding.is_ambiguity_recursive()
343-
&& let NameBindingKind::Import { import: old_import, .. } =
344-
old_binding.kind
345-
&& let NameBindingKind::Import { import, .. } = binding.kind
346-
&& old_import == import
347-
{
348-
// We should replace the `old_binding` with `binding` regardless
349-
// of whether they has same resolution or not when they are
350-
// imported from the same glob-import statement.
351-
resolution.binding = Some(binding);
352-
} else if res != old_binding.res() {
353-
resolution.binding = Some(this.new_ambiguity_binding(
354-
AmbiguityKind::GlobVsGlob,
355-
old_binding,
356-
binding,
357-
warn_ambiguity,
358-
));
359-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
360-
// We are glob-importing the same item but with greater visibility.
361-
resolution.binding = Some(binding);
362-
} else if binding.is_ambiguity_recursive() {
363-
resolution.binding = Some(this.new_warn_ambiguity_binding(binding));
364-
}
338+
339+
if binding.is_glob_import() {
340+
let (glob_binding, nonglob_binding) = (binding, old_non_glob_binding);
341+
342+
if key.ns == MacroNS
343+
&& nonglob_binding.expansion != LocalExpnId::ROOT
344+
&& glob_binding.res() != nonglob_binding.res()
345+
{
346+
resolution.non_glob_binding = Some(this.new_ambiguity_binding(
347+
AmbiguityKind::GlobVsExpanded,
348+
nonglob_binding,
349+
glob_binding,
350+
false,
351+
));
365352
}
366-
(old_glob @ true, false) | (old_glob @ false, true) => {
367-
let (glob_binding, nonglob_binding) =
368-
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
369-
if key.ns == MacroNS
370-
&& nonglob_binding.expansion != LocalExpnId::ROOT
371-
&& glob_binding.res() != nonglob_binding.res()
372-
{
373-
resolution.binding = Some(this.new_ambiguity_binding(
374-
AmbiguityKind::GlobVsExpanded,
375-
nonglob_binding,
353+
354+
if let Some(old_glob) = resolution.glob_binding {
355+
if glob_binding.res() != old_glob.res() {
356+
resolution.glob_binding = Some(this.new_ambiguity_binding(
357+
AmbiguityKind::GlobVsGlob,
358+
old_glob,
376359
glob_binding,
377-
false,
360+
warn_ambiguity,
378361
));
379-
} else {
380-
resolution.binding = Some(nonglob_binding);
381-
}
382-
383-
if let Some(old_shadowed_glob) = resolution.shadowed_glob {
384-
assert!(old_shadowed_glob.is_glob_import());
385-
if glob_binding.res() != old_shadowed_glob.res() {
386-
resolution.shadowed_glob = Some(this.new_ambiguity_binding(
387-
AmbiguityKind::GlobVsGlob,
388-
old_shadowed_glob,
389-
glob_binding,
390-
false,
391-
));
392-
} else if !old_shadowed_glob.vis.is_at_least(binding.vis, this.tcx) {
393-
resolution.shadowed_glob = Some(glob_binding);
394-
}
395-
} else {
396-
resolution.shadowed_glob = Some(glob_binding);
362+
} else if !old_glob.vis.is_at_least(glob_binding.vis, this.tcx) {
363+
resolution.glob_binding = Some(glob_binding);
397364
}
365+
} else {
366+
resolution.glob_binding = Some(glob_binding);
398367
}
399-
(false, false) => {
400-
return Err(old_binding);
368+
} else {
369+
return Err(old_non_glob_binding);
370+
}
371+
} else if let Some(old_glob_binding) = resolution.glob_binding {
372+
if binding.is_glob_import() {
373+
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
374+
if !binding.is_ambiguity_recursive()
375+
&& let NameBindingKind::Import { import: old_import, .. } =
376+
old_glob_binding.kind
377+
&& let NameBindingKind::Import { import, .. } = binding.kind
378+
&& old_import == import
379+
{
380+
// We should replace the `old_binding` with `binding` regardless
381+
// of whether they has same resolution or not when they are
382+
// imported from the same glob-import statement.
383+
resolution.glob_binding = Some(binding);
384+
} else if res != old_glob_binding.res() {
385+
resolution.glob_binding = Some(this.new_ambiguity_binding(
386+
AmbiguityKind::GlobVsGlob,
387+
old_glob_binding,
388+
binding,
389+
warn_ambiguity,
390+
));
391+
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
392+
// We are glob-importing the same item but with greater visibility.
393+
resolution.glob_binding = Some(binding);
394+
} else if binding.is_ambiguity_recursive() {
395+
resolution.glob_binding = Some(this.new_warn_ambiguity_binding(binding));
401396
}
397+
} else {
398+
resolution.non_glob_binding = Some(binding);
402399
}
403400
} else {
404-
resolution.binding = Some(binding);
401+
if binding.is_glob_import() {
402+
resolution.glob_binding = Some(binding);
403+
} else {
404+
resolution.non_glob_binding = Some(binding);
405+
}
405406
}
406407

407408
Ok(())
@@ -619,7 +620,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
619620
for (key, resolution) in self.resolutions(*module).borrow().iter() {
620621
let resolution = resolution.borrow();
621622

622-
let Some(binding) = resolution.binding else { continue };
623+
let Some(binding) = resolution.non_glob_binding else { continue };
623624

624625
if let NameBindingKind::Import { import, .. } = binding.kind
625626
&& let Some((amb_binding, _)) = binding.ambiguity
@@ -639,7 +640,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
639640
);
640641
}
641642

642-
if let Some(glob_binding) = resolution.shadowed_glob {
643+
if let Some(glob_binding) = resolution.glob_binding {
643644
if binding.res() != Res::Err
644645
&& glob_binding.res() != Res::Err
645646
&& let NameBindingKind::Import { import: glob_import, .. } =
@@ -1162,7 +1163,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11621163
return None;
11631164
} // Never suggest the same name
11641165
match *resolution.borrow() {
1165-
NameResolution { binding: Some(name_binding), .. } => {
1166+
NameResolution { non_glob_binding: Some(name_binding), .. } => {
11661167
match name_binding.kind {
11671168
NameBindingKind::Import { binding, .. } => {
11681169
match binding.kind {

compiler/rustc_resolve/src/late.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3478,7 +3478,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34783478
};
34793479
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
34803480
let key = BindingKey::new(ident, ns);
3481-
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3481+
let mut binding =
3482+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34823483
debug!(?binding);
34833484
if binding.is_none() {
34843485
// We could not find the trait item in the correct namespace.
@@ -3489,7 +3490,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34893490
_ => ns,
34903491
};
34913492
let key = BindingKey::new(ident, ns);
3492-
binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3493+
binding =
3494+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34933495
debug!(?binding);
34943496
}
34953497

0 commit comments

Comments
 (0)