Skip to content

Commit dcdb35a

Browse files
authored
Rollup merge of #143734 - LorrensP-2158466:refactor-resolve-resolution-bindings, r=petrochenkov
Refactor resolve resolution bindings This pr does the work asked in #142547 (comment). This part: > move the `(non)_glob_binding` change r? ````@petrochenkov````
2 parents acbf5e4 + 9ed5378 commit dcdb35a

File tree

7 files changed

+77
-55
lines changed

7 files changed

+77
-55
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.best_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().best_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().best_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,15 +875,15 @@ 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

882882
if let Some(finalize) = finalize {
883883
return self.finalize_module_binding(
884884
ident,
885885
binding,
886-
resolution.shadowed_glob,
886+
if resolution.non_glob_binding.is_some() { resolution.glob_binding } else { None },
887887
parent_scope,
888888
finalize,
889889
shadowing,

compiler/rustc_resolve/src/imports.rs

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,27 @@ pub(crate) struct NameResolution<'ra> {
244244
/// Single imports that may define the name in the namespace.
245245
/// Imports are arena-allocated, so it's ok to use pointers as keys.
246246
pub single_imports: FxIndexSet<Import<'ra>>,
247-
/// The least shadowable known binding for this name, or None if there are no known bindings.
248-
pub binding: Option<NameBinding<'ra>>,
249-
pub shadowed_glob: Option<NameBinding<'ra>>,
247+
/// The non-glob binding for this name, if it is known to exist.
248+
pub non_glob_binding: Option<NameBinding<'ra>>,
249+
/// The glob binding for this name, if it is known to exist.
250+
pub glob_binding: Option<NameBinding<'ra>>,
250251
}
251252

252253
impl<'ra> NameResolution<'ra> {
253254
/// Returns the binding for the name if it is known or None if it not known.
254255
pub(crate) fn binding(&self) -> Option<NameBinding<'ra>> {
255-
self.binding.and_then(|binding| {
256+
self.best_binding().and_then(|binding| {
256257
if !binding.is_glob_import() || self.single_imports.is_empty() {
257258
Some(binding)
258259
} else {
259260
None
260261
}
261262
})
262263
}
264+
265+
pub(crate) fn best_binding(&self) -> Option<NameBinding<'ra>> {
266+
self.non_glob_binding.or(self.glob_binding)
267+
}
263268
}
264269

265270
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved
@@ -340,77 +345,83 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
340345
self.check_reserved_macro_name(key.ident, res);
341346
self.set_binding_parent_module(binding, module);
342347
self.update_resolution(module, key, warn_ambiguity, |this, resolution| {
343-
if let Some(old_binding) = resolution.binding {
348+
if let Some(old_binding) = resolution.best_binding() {
344349
if res == Res::Err && old_binding.res() != Res::Err {
345350
// Do not override real bindings with `Res::Err`s from error recovery.
346351
return Ok(());
347352
}
348353
match (old_binding.is_glob_import(), binding.is_glob_import()) {
349354
(true, true) => {
355+
let (glob_binding, old_glob_binding) = (binding, old_binding);
350356
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
351357
if !binding.is_ambiguity_recursive()
352358
&& let NameBindingKind::Import { import: old_import, .. } =
353-
old_binding.kind
354-
&& let NameBindingKind::Import { import, .. } = binding.kind
359+
old_glob_binding.kind
360+
&& let NameBindingKind::Import { import, .. } = glob_binding.kind
355361
&& old_import == import
356362
{
357-
// We should replace the `old_binding` with `binding` regardless
358-
// of whether they has same resolution or not when they are
359-
// imported from the same glob-import statement.
360-
resolution.binding = Some(binding);
361-
} else if res != old_binding.res() {
362-
resolution.binding = Some(this.new_ambiguity_binding(
363+
// When imported from the same glob-import statement, we should replace
364+
// `old_glob_binding` with `glob_binding`, regardless of whether
365+
// they have the same resolution or not.
366+
resolution.glob_binding = Some(glob_binding);
367+
} else if res != old_glob_binding.res() {
368+
resolution.glob_binding = Some(this.new_ambiguity_binding(
363369
AmbiguityKind::GlobVsGlob,
364-
old_binding,
365-
binding,
370+
old_glob_binding,
371+
glob_binding,
366372
warn_ambiguity,
367373
));
368374
} else if !old_binding.vis.is_at_least(binding.vis, this.tcx) {
369375
// We are glob-importing the same item but with greater visibility.
370-
resolution.binding = Some(binding);
376+
resolution.glob_binding = Some(glob_binding);
371377
} else if binding.is_ambiguity_recursive() {
372-
resolution.binding = Some(this.new_warn_ambiguity_binding(binding));
378+
resolution.glob_binding =
379+
Some(this.new_warn_ambiguity_binding(glob_binding));
373380
}
374381
}
375382
(old_glob @ true, false) | (old_glob @ false, true) => {
376-
let (glob_binding, nonglob_binding) =
383+
let (glob_binding, non_glob_binding) =
377384
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
378385
if key.ns == MacroNS
379-
&& nonglob_binding.expansion != LocalExpnId::ROOT
380-
&& glob_binding.res() != nonglob_binding.res()
386+
&& non_glob_binding.expansion != LocalExpnId::ROOT
387+
&& glob_binding.res() != non_glob_binding.res()
381388
{
382-
resolution.binding = Some(this.new_ambiguity_binding(
389+
resolution.non_glob_binding = Some(this.new_ambiguity_binding(
383390
AmbiguityKind::GlobVsExpanded,
384-
nonglob_binding,
391+
non_glob_binding,
385392
glob_binding,
386393
false,
387394
));
388395
} else {
389-
resolution.binding = Some(nonglob_binding);
396+
resolution.non_glob_binding = Some(non_glob_binding);
390397
}
391398

392-
if let Some(old_shadowed_glob) = resolution.shadowed_glob {
393-
assert!(old_shadowed_glob.is_glob_import());
394-
if glob_binding.res() != old_shadowed_glob.res() {
395-
resolution.shadowed_glob = Some(this.new_ambiguity_binding(
399+
if let Some(old_glob_binding) = resolution.glob_binding {
400+
assert!(old_glob_binding.is_glob_import());
401+
if glob_binding.res() != old_glob_binding.res() {
402+
resolution.glob_binding = Some(this.new_ambiguity_binding(
396403
AmbiguityKind::GlobVsGlob,
397-
old_shadowed_glob,
404+
old_glob_binding,
398405
glob_binding,
399406
false,
400407
));
401-
} else if !old_shadowed_glob.vis.is_at_least(binding.vis, this.tcx) {
402-
resolution.shadowed_glob = Some(glob_binding);
408+
} else if !old_glob_binding.vis.is_at_least(binding.vis, this.tcx) {
409+
resolution.glob_binding = Some(glob_binding);
403410
}
404411
} else {
405-
resolution.shadowed_glob = Some(glob_binding);
412+
resolution.glob_binding = Some(glob_binding);
406413
}
407414
}
408415
(false, false) => {
409416
return Err(old_binding);
410417
}
411418
}
412419
} else {
413-
resolution.binding = Some(binding);
420+
if binding.is_glob_import() {
421+
resolution.glob_binding = Some(binding);
422+
} else {
423+
resolution.non_glob_binding = Some(binding);
424+
}
414425
}
415426

416427
Ok(())
@@ -631,7 +642,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
631642
for (key, resolution) in self.resolutions(*module).borrow().iter() {
632643
let resolution = resolution.borrow();
633644

634-
let Some(binding) = resolution.binding else { continue };
645+
let Some(binding) = resolution.best_binding() else { continue };
635646

636647
if let NameBindingKind::Import { import, .. } = binding.kind
637648
&& let Some((amb_binding, _)) = binding.ambiguity
@@ -651,7 +662,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
651662
);
652663
}
653664

654-
if let Some(glob_binding) = resolution.shadowed_glob {
665+
if let Some(glob_binding) = resolution.glob_binding
666+
&& resolution.non_glob_binding.is_some()
667+
{
655668
if binding.res() != Res::Err
656669
&& glob_binding.res() != Res::Err
657670
&& let NameBindingKind::Import { import: glob_import, .. } =
@@ -1172,7 +1185,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11721185
return None;
11731186
} // Never suggest the same name
11741187
match *resolution.borrow() {
1175-
NameResolution { binding: Some(name_binding), .. } => {
1188+
ref resolution
1189+
if let Some(name_binding) = resolution.best_binding() =>
1190+
{
11761191
match name_binding.kind {
11771192
NameBindingKind::Import { binding, .. } => {
11781193
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.best_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.best_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
@@ -880,8 +880,10 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
880880
fn lookup_doc_alias_name(&mut self, path: &[Segment], ns: Namespace) -> Option<(DefId, Ident)> {
881881
let find_doc_alias_name = |r: &mut Resolver<'ra, '_>, m: Module<'ra>, item_name: Symbol| {
882882
for resolution in r.resolutions(m).borrow().values() {
883-
let Some(did) =
884-
resolution.borrow().binding.and_then(|binding| binding.res().opt_def_id())
883+
let Some(did) = resolution
884+
.borrow()
885+
.best_binding()
886+
.and_then(|binding| binding.res().opt_def_id())
885887
else {
886888
continue;
887889
};
@@ -1464,15 +1466,16 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
14641466
self.resolve_path(mod_path, None, None)
14651467
{
14661468
let resolutions = self.r.resolutions(module).borrow();
1467-
let targets: Vec<_> =
1468-
resolutions
1469-
.iter()
1470-
.filter_map(|(key, resolution)| {
1471-
resolution.borrow().binding.map(|binding| binding.res()).and_then(
1472-
|res| if filter_fn(res) { Some((key, res)) } else { None },
1473-
)
1474-
})
1475-
.collect();
1469+
let targets: Vec<_> = resolutions
1470+
.iter()
1471+
.filter_map(|(key, resolution)| {
1472+
resolution
1473+
.borrow()
1474+
.best_binding()
1475+
.map(|binding| binding.res())
1476+
.and_then(|res| if filter_fn(res) { Some((key, res)) } else { None })
1477+
})
1478+
.collect();
14761479
if let [target] = targets.as_slice() {
14771480
return Some(TypoSuggestion::single_item_from_ident(target.0.ident, target.1));
14781481
}
@@ -2305,7 +2308,9 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
23052308
let targets = resolutions
23062309
.borrow()
23072310
.iter()
2308-
.filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding.res())))
2311+
.filter_map(|(key, res)| {
2312+
res.borrow().best_binding().map(|binding| (key, binding.res()))
2313+
})
23092314
.filter(|(_, res)| match (kind, res) {
23102315
(AssocItemKind::Const(..), Res::Def(DefKind::AssocConst, _)) => true,
23112316
(AssocItemKind::Fn(_), Res::Def(DefKind::AssocFn, _)) => true,

compiler/rustc_resolve/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ 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+
if let Some(binding) = name_resolution.borrow().best_binding() {
645645
f(resolver, key.ident, key.ns, binding);
646646
}
647647
}

0 commit comments

Comments
 (0)