Skip to content

Commit e9ed7ed

Browse files
committed
replace binding and shadowed_glob on NameResolution with non_glob_binding and glob_binding
1 parent a1d845b commit e9ed7ed

File tree

7 files changed

+160
-110
lines changed

7 files changed

+160
-110
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,7 +1442,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14421442
|(key, name_resolution)| {
14431443
if key.ns == TypeNS
14441444
&& key.ident == ident
1445-
&& let Some(binding) = name_resolution.borrow().binding
1445+
&& let Some(binding) = name_resolution.borrow().late_binding()
14461446
{
14471447
match binding.res() {
14481448
// No disambiguation needed if the identically named item we
@@ -1496,7 +1496,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14961496
return None;
14971497
};
14981498
for (_, resolution) in this.resolutions(m).borrow().iter() {
1499-
let Some(binding) = resolution.borrow().binding else {
1499+
let Some(binding) = resolution.borrow().late_binding() else {
15001500
continue;
15011501
};
15021502
let Res::Def(DefKind::Macro(MacroKind::Derive | MacroKind::Attr), def_id) =

compiler/rustc_resolve/src/ident.rs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -905,15 +905,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
905905
let break_result = self.visit_module_scopes(ns, |this, scope| match scope {
906906
ModuleScope::NonGlobal => {
907907
// Non-glob bindings are "strong" and can be resolved immediately.
908-
if let Some(binding) = resolution.binding
909-
&& !binding.is_glob_import()
908+
if let Some(binding) = resolution.non_glob_binding
910909
&& ignore_binding.map_or(true, |b| binding != b)
911910
{
912911
if let Some(finalize) = finalize {
913-
return Some(this.finalize_module_binding(
912+
return Some(this.finalize_non_glob_module_binding(
914913
ident,
915-
Some(binding),
916-
resolution.shadowed_glob,
914+
binding,
915+
resolution.glob_binding,
917916
parent_scope,
918917
finalize,
919918
shadowing,
@@ -928,15 +927,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
928927
ModuleScope::Global => {
929928
// If we are here, any primary `resolution.binding` is either a glob, None,
930929
// or should be ignored.
931-
let binding = [resolution.binding, resolution.shadowed_glob]
932-
.into_iter()
933-
.find_map(|binding| if binding == ignore_binding { None } else { binding });
930+
let binding = resolution.glob_binding;
931+
932+
if let Some(binding) = binding {
933+
if !binding.is_glob_import() {
934+
panic!("binding should be glob import {:?}", binding)
935+
}
936+
}
934937

935938
if let Some(finalize) = finalize {
936-
return Some(this.finalize_module_binding(
939+
return Some(this.finalize_glob_module_binding(
937940
ident,
938941
binding,
939-
resolution.shadowed_glob,
940942
parent_scope,
941943
finalize,
942944
shadowing,
@@ -1040,21 +1042,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10401042
Err((Determined, Weak::No))
10411043
}
10421044

1043-
fn finalize_module_binding(
1045+
fn finalize_non_glob_module_binding(
10441046
&mut self,
10451047
ident: Ident,
1046-
binding: Option<NameBinding<'ra>>,
1047-
shadowed_glob: Option<NameBinding<'ra>>,
1048+
binding: NameBinding<'ra>,
1049+
glob_binding: Option<NameBinding<'ra>>,
10481050
parent_scope: &ParentScope<'ra>,
10491051
finalize: Finalize,
10501052
shadowing: Shadowing,
10511053
) -> Result<NameBinding<'ra>, (Determinacy, Weak)> {
10521054
let Finalize { path_span, report_private, used, root_span, .. } = finalize;
10531055

1054-
let Some(binding) = binding else {
1055-
return Err((Determined, Weak::No));
1056-
};
1057-
10581056
if !self.is_accessible_from(binding.vis, parent_scope.module) {
10591057
if report_private {
10601058
self.privacy_errors.push(PrivacyError {
@@ -1071,7 +1069,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10711069
}
10721070

10731071
// Forbid expanded shadowing to avoid time travel.
1074-
if let Some(shadowed_glob) = shadowed_glob
1072+
if let Some(shadowed_glob) = glob_binding
10751073
&& shadowing == Shadowing::Restricted
10761074
&& binding.expansion != LocalExpnId::ROOT
10771075
&& binding.res() != shadowed_glob.res()
@@ -1099,6 +1097,47 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10991097
return Ok(binding);
11001098
}
11011099

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

compiler/rustc_resolve/src/imports.rs

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
2222
use rustc_span::hygiene::LocalExpnId;
2323
use rustc_span::{Ident, Span, Symbol, kw, sym};
2424
use smallvec::SmallVec;
25-
use tracing::debug;
25+
use tracing::{debug, instrument};
2626

2727
use crate::Determinacy::{self, *};
2828
use crate::Namespace::*;
@@ -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(())
@@ -611,15 +612,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
611612
self.throw_unresolved_import_error(errors, glob_error);
612613
}
613614

615+
#[instrument(skip(self), level = "debug")]
614616
pub(crate) fn check_hidden_glob_reexports(
615617
&mut self,
616618
exported_ambiguities: FxHashSet<NameBinding<'ra>>,
617619
) {
618620
for module in self.arenas.local_modules().iter() {
619621
for (key, resolution) in self.resolutions(*module).borrow().iter() {
620622
let resolution = resolution.borrow();
623+
debug!(?resolution);
621624

622-
let Some(binding) = resolution.binding else { continue };
625+
let Some(binding) = resolution.late_binding() else { continue };
623626

624627
if let NameBindingKind::Import { import, .. } = binding.kind
625628
&& let Some((amb_binding, _)) = binding.ambiguity
@@ -639,7 +642,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
639642
);
640643
}
641644

642-
if let Some(glob_binding) = resolution.shadowed_glob {
645+
if let Some(glob_binding) = resolution.glob_binding {
643646
if binding.res() != Res::Err
644647
&& glob_binding.res() != Res::Err
645648
&& let NameBindingKind::Import { import: glob_import, .. } =
@@ -1162,7 +1165,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11621165
return None;
11631166
} // Never suggest the same name
11641167
match *resolution.borrow() {
1165-
NameResolution { binding: Some(name_binding), .. } => {
1168+
NameResolution { non_glob_binding: Some(name_binding), .. } => {
11661169
match name_binding.kind {
11671170
NameBindingKind::Import { binding, .. } => {
11681171
match binding.kind {

compiler/rustc_resolve/src/late.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,7 +3441,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34413441
};
34423442
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
34433443
let key = BindingKey::new(ident, ns);
3444-
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3444+
let mut binding =
3445+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34453446
debug!(?binding);
34463447
if binding.is_none() {
34473448
// We could not find the trait item in the correct namespace.
@@ -3452,7 +3453,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34523453
_ => ns,
34533454
};
34543455
let key = BindingKey::new(ident, ns);
3455-
binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3456+
binding =
3457+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34563458
debug!(?binding);
34573459
}
34583460

0 commit comments

Comments
 (0)