Skip to content

Commit 81134f5

Browse files
b-naberLorrensP-2158466
authored andcommitted
replace binding and shadowed_glob on NameResolution with non_glob_binding and glob_binding
1 parent cf3fb76 commit 81134f5

File tree

7 files changed

+105
-94
lines changed

7 files changed

+105
-94
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
@@ -1440,7 +1440,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14401440
|(key, name_resolution)| {
14411441
if key.ns == TypeNS
14421442
&& key.ident == ident
1443-
&& let Some(binding) = name_resolution.borrow().binding
1443+
&& let Some(binding) = name_resolution.borrow().late_binding()
14441444
{
14451445
match binding.res() {
14461446
// No disambiguation needed if the identically named item we
@@ -1494,7 +1494,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14941494
return None;
14951495
};
14961496
for (_, resolution) in this.resolutions(m).borrow().iter() {
1497-
let Some(binding) = resolution.borrow().binding else {
1497+
let Some(binding) = resolution.borrow().late_binding() else {
14981498
continue;
14991499
};
15001500
let Res::Def(DefKind::Macro(MacroKind::Derive | MacroKind::Attr), def_id) =

compiler/rustc_resolve/src/ident.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
875875
// binding if it exists. What we really want here is having two separate scopes in
876876
// a module - one for non-globs and one for globs, but until that's done use this
877877
// hack to avoid inconsistent resolution ICEs during import validation.
878-
let binding = [resolution.binding, resolution.shadowed_glob]
878+
let binding = [resolution.non_glob_binding, resolution.glob_binding]
879879
.into_iter()
880880
.find_map(|binding| if binding == ignore_binding { None } else { binding });
881881

@@ -900,7 +900,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
900900
}
901901

902902
// Forbid expanded shadowing to avoid time travel.
903-
if let Some(shadowed_glob) = resolution.shadowed_glob
903+
if let Some(shadowed_glob) = resolution.glob_binding
904904
&& shadowing == Shadowing::Restricted
905905
&& binding.expansion != LocalExpnId::ROOT
906906
&& binding.res() != shadowed_glob.res()

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::*;
@@ -242,21 +242,20 @@ pub(crate) struct NameResolution<'ra> {
242242
/// Single imports that may define the name in the namespace.
243243
/// Imports are arena-allocated, so it's ok to use pointers as keys.
244244
pub single_imports: FxIndexSet<Import<'ra>>,
245-
/// The least shadowable known binding for this name, or None if there are no known bindings.
246-
pub binding: Option<NameBinding<'ra>>,
247-
pub shadowed_glob: Option<NameBinding<'ra>>,
245+
/// The least shadowable known non-glob binding for this name, or None if there are no known bindings.
246+
pub non_glob_binding: Option<NameBinding<'ra>>,
247+
pub glob_binding: Option<NameBinding<'ra>>,
248248
}
249249

250250
impl<'ra> NameResolution<'ra> {
251251
/// Returns the binding for the name if it is known or None if it not known.
252252
pub(crate) fn binding(&self) -> Option<NameBinding<'ra>> {
253-
self.binding.and_then(|binding| {
254-
if !binding.is_glob_import() || self.single_imports.is_empty() {
255-
Some(binding)
256-
} else {
257-
None
258-
}
259-
})
253+
self.non_glob_binding
254+
.or_else(|| if self.single_imports.is_empty() { self.glob_binding } else { None })
255+
}
256+
257+
pub(crate) fn late_binding(&self) -> Option<NameBinding<'ra>> {
258+
self.non_glob_binding.or_else(|| self.glob_binding)
260259
}
261260
}
262261

@@ -338,77 +337,79 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338337
self.check_reserved_macro_name(key.ident, res);
339338
self.set_binding_parent_module(binding, module);
340339
self.update_resolution(module, key, warn_ambiguity, |this, resolution| {
341-
if let Some(old_binding) = resolution.binding {
342-
if res == Res::Err && old_binding.res() != Res::Err {
340+
if let Some(old_non_glob_binding) = resolution.non_glob_binding {
341+
if res == Res::Err && old_non_glob_binding.res() != Res::Err {
343342
// Do not override real bindings with `Res::Err`s from error recovery.
344343
return Ok(());
345344
}
346-
match (old_binding.is_glob_import(), binding.is_glob_import()) {
347-
(true, true) => {
348-
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
349-
if !binding.is_ambiguity_recursive()
350-
&& let NameBindingKind::Import { import: old_import, .. } =
351-
old_binding.kind
352-
&& let NameBindingKind::Import { import, .. } = binding.kind
353-
&& old_import == import
354-
{
355-
// We should replace the `old_binding` with `binding` regardless
356-
// of whether they has same resolution or not when they are
357-
// imported from the same glob-import statement.
358-
resolution.binding = Some(binding);
359-
} else if res != old_binding.res() {
360-
resolution.binding = Some(this.new_ambiguity_binding(
361-
AmbiguityKind::GlobVsGlob,
362-
old_binding,
363-
binding,
364-
warn_ambiguity,
365-
));
366-
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
367-
// We are glob-importing the same item but with greater visibility.
368-
resolution.binding = Some(binding);
369-
} else if binding.is_ambiguity_recursive() {
370-
resolution.binding = Some(this.new_warn_ambiguity_binding(binding));
371-
}
345+
346+
if binding.is_glob_import() {
347+
let (glob_binding, nonglob_binding) = (binding, old_non_glob_binding);
348+
349+
if key.ns == MacroNS
350+
&& nonglob_binding.expansion != LocalExpnId::ROOT
351+
&& glob_binding.res() != nonglob_binding.res()
352+
{
353+
resolution.non_glob_binding = Some(this.new_ambiguity_binding(
354+
AmbiguityKind::GlobVsExpanded,
355+
nonglob_binding,
356+
glob_binding,
357+
false,
358+
));
372359
}
373-
(old_glob @ true, false) | (old_glob @ false, true) => {
374-
let (glob_binding, nonglob_binding) =
375-
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
376-
if key.ns == MacroNS
377-
&& nonglob_binding.expansion != LocalExpnId::ROOT
378-
&& glob_binding.res() != nonglob_binding.res()
379-
{
380-
resolution.binding = Some(this.new_ambiguity_binding(
381-
AmbiguityKind::GlobVsExpanded,
382-
nonglob_binding,
360+
361+
if let Some(old_glob) = resolution.glob_binding {
362+
if glob_binding.res() != old_glob.res() {
363+
resolution.glob_binding = Some(this.new_ambiguity_binding(
364+
AmbiguityKind::GlobVsGlob,
365+
old_glob,
383366
glob_binding,
384-
false,
367+
warn_ambiguity,
385368
));
386-
} else {
387-
resolution.binding = Some(nonglob_binding);
388-
}
389-
390-
if let Some(old_shadowed_glob) = resolution.shadowed_glob {
391-
assert!(old_shadowed_glob.is_glob_import());
392-
if glob_binding.res() != old_shadowed_glob.res() {
393-
resolution.shadowed_glob = Some(this.new_ambiguity_binding(
394-
AmbiguityKind::GlobVsGlob,
395-
old_shadowed_glob,
396-
glob_binding,
397-
false,
398-
));
399-
} else if !old_shadowed_glob.vis.is_at_least(binding.vis, this.tcx) {
400-
resolution.shadowed_glob = Some(glob_binding);
401-
}
402-
} else {
403-
resolution.shadowed_glob = Some(glob_binding);
369+
} else if !old_glob.vis.is_at_least(glob_binding.vis, this.tcx) {
370+
resolution.glob_binding = Some(glob_binding);
404371
}
372+
} else {
373+
resolution.glob_binding = Some(glob_binding);
405374
}
406-
(false, false) => {
407-
return Err(old_binding);
375+
} else {
376+
return Err(old_non_glob_binding);
377+
}
378+
} else if let Some(old_glob_binding) = resolution.glob_binding {
379+
if binding.is_glob_import() {
380+
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
381+
if !binding.is_ambiguity_recursive()
382+
&& let NameBindingKind::Import { import: old_import, .. } =
383+
old_glob_binding.kind
384+
&& let NameBindingKind::Import { import, .. } = binding.kind
385+
&& old_import == import
386+
{
387+
// We should replace the `old_binding` with `binding` regardless
388+
// of whether they has same resolution or not when they are
389+
// imported from the same glob-import statement.
390+
resolution.glob_binding = Some(binding);
391+
} else if res != old_glob_binding.res() {
392+
resolution.glob_binding = Some(this.new_ambiguity_binding(
393+
AmbiguityKind::GlobVsGlob,
394+
old_glob_binding,
395+
binding,
396+
warn_ambiguity,
397+
));
398+
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
399+
// We are glob-importing the same item but with greater visibility.
400+
resolution.glob_binding = Some(binding);
401+
} else if binding.is_ambiguity_recursive() {
402+
resolution.glob_binding = Some(this.new_warn_ambiguity_binding(binding));
408403
}
404+
} else {
405+
resolution.non_glob_binding = Some(binding);
409406
}
410407
} else {
411-
resolution.binding = Some(binding);
408+
if binding.is_glob_import() {
409+
resolution.glob_binding = Some(binding);
410+
} else {
411+
resolution.non_glob_binding = Some(binding);
412+
}
412413
}
413414

414415
Ok(())
@@ -620,15 +621,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
620621
}
621622
}
622623

624+
#[instrument(skip(self), level = "debug")]
623625
pub(crate) fn check_hidden_glob_reexports(
624626
&mut self,
625627
exported_ambiguities: FxHashSet<NameBinding<'ra>>,
626628
) {
627629
for module in self.arenas.local_modules().iter() {
628630
for (key, resolution) in self.resolutions(*module).borrow().iter() {
629631
let resolution = resolution.borrow();
632+
debug!(?resolution);
630633

631-
let Some(binding) = resolution.binding else { continue };
634+
let Some(binding) = resolution.late_binding() else { continue };
632635

633636
if let NameBindingKind::Import { import, .. } = binding.kind
634637
&& let Some((amb_binding, _)) = binding.ambiguity
@@ -648,7 +651,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
648651
);
649652
}
650653

651-
if let Some(glob_binding) = resolution.shadowed_glob {
654+
if let Some(glob_binding) = resolution.glob_binding {
652655
if binding.res() != Res::Err
653656
&& glob_binding.res() != Res::Err
654657
&& let NameBindingKind::Import { import: glob_import, .. } =
@@ -1179,7 +1182,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11791182
return None;
11801183
} // Never suggest the same name
11811184
match *resolution.borrow() {
1182-
NameResolution { binding: Some(name_binding), .. } => {
1185+
NameResolution { non_glob_binding: Some(name_binding), .. } => {
11831186
match name_binding.kind {
11841187
NameBindingKind::Import { binding, .. } => {
11851188
match binding.kind {

compiler/rustc_resolve/src/late.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3437,7 +3437,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34373437
};
34383438
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
34393439
let key = BindingKey::new(ident, ns);
3440-
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3440+
let mut binding =
3441+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34413442
debug!(?binding);
34423443
if binding.is_none() {
34433444
// We could not find the trait item in the correct namespace.
@@ -3448,7 +3449,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
34483449
_ => ns,
34493450
};
34503451
let key = BindingKey::new(ident, ns);
3451-
binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
3452+
binding =
3453+
self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.late_binding());
34523454
debug!(?binding);
34533455
}
34543456

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -881,8 +881,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
881881
fn lookup_doc_alias_name(&mut self, path: &[Segment], ns: Namespace) -> Option<(DefId, Ident)> {
882882
let find_doc_alias_name = |r: &mut Resolver<'ra, '_>, m: Module<'ra>, item_name: Symbol| {
883883
for resolution in r.resolutions(m).borrow().values() {
884-
let Some(did) =
885-
resolution.borrow().binding.and_then(|binding| binding.res().opt_def_id())
884+
let Some(did) = resolution
885+
.borrow()
886+
.late_binding()
887+
.and_then(|binding| binding.res().opt_def_id())
886888
else {
887889
continue;
888890
};
@@ -1465,15 +1467,16 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
14651467
self.resolve_path(mod_path, None, None)
14661468
{
14671469
let resolutions = self.r.resolutions(module).borrow();
1468-
let targets: Vec<_> =
1469-
resolutions
1470-
.iter()
1471-
.filter_map(|(key, resolution)| {
1472-
resolution.borrow().binding.map(|binding| binding.res()).and_then(
1473-
|res| if filter_fn(res) { Some((key, res)) } else { None },
1474-
)
1475-
})
1476-
.collect();
1470+
let targets: Vec<_> = resolutions
1471+
.iter()
1472+
.filter_map(|(key, resolution)| {
1473+
resolution
1474+
.borrow()
1475+
.late_binding()
1476+
.map(|binding| binding.res())
1477+
.and_then(|res| if filter_fn(res) { Some((key, res)) } else { None })
1478+
})
1479+
.collect();
14771480
if let [target] = targets.as_slice() {
14781481
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
14791482
}
@@ -2306,7 +2309,9 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
23062309
let targets = resolutions
23072310
.borrow()
23082311
.iter()
2309-
.filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding.res())))
2312+
.filter_map(|(key, res)| {
2313+
res.borrow().late_binding().map(|binding| (key, binding.res()))
2314+
})
23102315
.filter(|(_, res)| match (kind, res) {
23112316
(AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true,
23122317
(AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,

compiler/rustc_resolve/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ impl<'ra> Module<'ra> {
641641
F: FnMut(&mut R, Ident, Namespace, NameBinding<'ra>),
642642
{
643643
for (key, name_resolution) in resolver.as_mut().resolutions(self).borrow().iter() {
644-
if let Some(binding) = name_resolution.borrow().binding {
644+
let resolution = name_resolution.borrow();
645+
if let Some(binding) = resolution.non_glob_binding.or_else(|| resolution.glob_binding) {
645646
f(resolver, key.ident, key.ns, binding);
646647
}
647648
}

0 commit comments

Comments
 (0)