Skip to content

Commit 0d95920

Browse files
committed
Auto merge of #144172 - lqd:revert-144013, r=petrochenkov
Prepare revert of 144013 This is a possible revert for #144013 causing issue #144168 (imo p-crit) to give us time to figure out a correct fix for #144013 without pressure. Feel free to close if it's an easy fix instead: r? `@petrochenkov`
2 parents f63685d + 9613ce0 commit 0d95920

File tree

4 files changed

+45
-58
lines changed

4 files changed

+45
-58
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4949
ns: Namespace,
5050
binding: NameBinding<'ra>,
5151
) {
52-
if let Err(old_binding) = self.try_define(parent, ident, ns, binding, false) {
52+
let key = self.new_disambiguated_key(ident, ns);
53+
if let Err(old_binding) = self.try_define(parent, key, binding, false) {
5354
self.report_conflict(parent, ident, ns, old_binding, binding);
5455
}
5556
}
@@ -441,18 +442,16 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
441442

442443
self.r.indeterminate_imports.push(import);
443444
match import.kind {
445+
// Don't add unresolved underscore imports to modules
446+
ImportKind::Single { target: Ident { name: kw::Underscore, .. }, .. } => {}
444447
ImportKind::Single { target, type_ns_only, .. } => {
445-
// Don't add underscore imports to `single_imports`
446-
// because they cannot define any usable names.
447-
if target.name != kw::Underscore {
448-
self.r.per_ns(|this, ns| {
449-
if !type_ns_only || ns == TypeNS {
450-
let key = BindingKey::new(target, ns);
451-
let mut resolution = this.resolution(current_module, key).borrow_mut();
452-
resolution.single_imports.insert(import);
453-
}
454-
});
455-
}
448+
self.r.per_ns(|this, ns| {
449+
if !type_ns_only || ns == TypeNS {
450+
let key = BindingKey::new(target, ns);
451+
let mut resolution = this.resolution(current_module, key).borrow_mut();
452+
resolution.single_imports.insert(import);
453+
}
454+
});
456455
}
457456
// We don't add prelude imports to the globs since they only affect lexical scopes,
458457
// which are not relevant to import resolution.
@@ -1409,12 +1408,9 @@ impl<'a, 'ra, 'tcx> Visitor<'a> for BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
14091408
let parent = self.parent_scope.module;
14101409
let expansion = self.parent_scope.expansion;
14111410
self.r.define(parent, ident, ns, self.res(def_id), vis, item.span, expansion);
1412-
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob)
1413-
&& ident.name != kw::Underscore
1414-
{
1415-
// Don't add underscore names, they cannot be looked up anyway.
1411+
} else if !matches!(&item.kind, AssocItemKind::Delegation(deleg) if deleg.from_glob) {
14161412
let impl_def_id = self.r.tcx.local_parent(local_def_id);
1417-
let key = BindingKey::new(ident, ns);
1413+
let key = BindingKey::new(ident.normalize_to_macros_2_0(), ns);
14181414
self.r.impl_binding_keys.entry(impl_def_id).or_default().insert(key);
14191415
}
14201416

compiler/rustc_resolve/src/imports.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_span::{Ident, Span, Symbol, kw, sym};
2525
use smallvec::SmallVec;
2626
use tracing::debug;
2727

28-
use crate::Namespace::{self, *};
28+
use crate::Namespace::*;
2929
use crate::diagnostics::{DiagMode, Suggestion, import_candidates};
3030
use crate::errors::{
3131
CannotBeReexportedCratePublic, CannotBeReexportedCratePublicNS, CannotBeReexportedPrivate,
@@ -338,20 +338,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338338
pub(crate) fn try_define(
339339
&mut self,
340340
module: Module<'ra>,
341-
ident: Ident,
342-
ns: Namespace,
341+
key: BindingKey,
343342
binding: NameBinding<'ra>,
344343
warn_ambiguity: bool,
345344
) -> Result<(), NameBinding<'ra>> {
346345
let res = binding.res();
347-
self.check_reserved_macro_name(ident, res);
346+
self.check_reserved_macro_name(key.ident, res);
348347
self.set_binding_parent_module(binding, module);
349-
// Even if underscore names cannot be looked up, we still need to add them to modules,
350-
// because they can be fetched by glob imports from those modules, and bring traits
351-
// into scope both directly and through glob imports.
352-
let key = BindingKey::new_disambiguated(ident, ns, || {
353-
(module.0.0.lazy_resolutions.borrow().len() + 1).try_into().unwrap()
354-
});
355348
self.update_resolution(module, key, warn_ambiguity, |this, resolution| {
356349
if let Some(old_binding) = resolution.best_binding() {
357350
if res == Res::Err && old_binding.res() != Res::Err {
@@ -390,7 +383,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
390383
(old_glob @ true, false) | (old_glob @ false, true) => {
391384
let (glob_binding, non_glob_binding) =
392385
if old_glob { (old_binding, binding) } else { (binding, old_binding) };
393-
if ns == MacroNS
386+
if key.ns == MacroNS
394387
&& non_glob_binding.expansion != LocalExpnId::ROOT
395388
&& glob_binding.res() != non_glob_binding.res()
396389
{
@@ -496,10 +489,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
496489
};
497490
if self.is_accessible_from(binding.vis, scope) {
498491
let imported_binding = self.import(binding, *import);
492+
let key = BindingKey { ident, ..key };
499493
let _ = self.try_define(
500494
import.parent_scope.module,
501-
ident,
502-
key.ns,
495+
key,
503496
imported_binding,
504497
warn_ambiguity,
505498
);
@@ -521,15 +514,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
521514
let dummy_binding = self.dummy_binding;
522515
let dummy_binding = self.import(dummy_binding, import);
523516
self.per_ns(|this, ns| {
524-
let module = import.parent_scope.module;
525-
let _ = this.try_define(module, target, ns, dummy_binding, false);
526-
// Don't remove underscores from `single_imports`, they were never added.
527-
if target.name != kw::Underscore {
528-
let key = BindingKey::new(target, ns);
529-
this.update_resolution(module, key, false, |_, resolution| {
530-
resolution.single_imports.swap_remove(&import);
531-
})
532-
}
517+
let key = BindingKey::new(target, ns);
518+
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
519+
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
520+
resolution.single_imports.swap_remove(&import);
521+
})
533522
});
534523
self.record_use(target, dummy_binding, Used::Other);
535524
} else if import.imported_module.get().is_none() {
@@ -906,7 +895,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
906895
PendingBinding::Ready(Some(imported_binding))
907896
}
908897
Err(Determinacy::Determined) => {
909-
// Don't remove underscores from `single_imports`, they were never added.
898+
// Don't update the resolution for underscores, because it was never added.
910899
if target.name != kw::Underscore {
911900
let key = BindingKey::new(target, ns);
912901
this.update_resolution(parent, key, false, |_, resolution| {
@@ -1521,8 +1510,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15211510
.is_some_and(|binding| binding.warn_ambiguity_recursive());
15221511
let _ = self.try_define(
15231512
import.parent_scope.module,
1524-
key.ident,
1525-
key.ns,
1513+
key,
15261514
imported_binding,
15271515
warn_ambiguity,
15281516
);

compiler/rustc_resolve/src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,26 +532,15 @@ struct BindingKey {
532532
/// identifier.
533533
ident: Ident,
534534
ns: Namespace,
535-
/// When we add an underscore binding (with ident `_`) to some module, this field has
536-
/// a non-zero value that uniquely identifies this binding in that module.
537-
/// For non-underscore bindings this field is zero.
538-
/// When a key is constructed for name lookup (as opposed to name definition), this field is
539-
/// also zero, even for underscore names, so for underscores the lookup will never succeed.
535+
/// 0 if ident is not `_`, otherwise a value that's unique to the specific
536+
/// `_` in the expanded AST that introduced this binding.
540537
disambiguator: u32,
541538
}
542539

543540
impl BindingKey {
544541
fn new(ident: Ident, ns: Namespace) -> Self {
545-
BindingKey { ident: ident.normalize_to_macros_2_0(), ns, disambiguator: 0 }
546-
}
547-
548-
fn new_disambiguated(
549-
ident: Ident,
550-
ns: Namespace,
551-
disambiguator: impl FnOnce() -> u32,
552-
) -> BindingKey {
553-
let disambiguator = if ident.name == kw::Underscore { disambiguator() } else { 0 };
554-
BindingKey { ident: ident.normalize_to_macros_2_0(), ns, disambiguator }
542+
let ident = ident.normalize_to_macros_2_0();
543+
BindingKey { ident, ns, disambiguator: 0 }
555544
}
556545
}
557546

@@ -1098,6 +1087,8 @@ pub struct Resolver<'ra, 'tcx> {
10981087
extern_module_map: RefCell<FxIndexMap<DefId, Module<'ra>>>,
10991088
binding_parent_modules: FxHashMap<NameBinding<'ra>, Module<'ra>>,
11001089

1090+
underscore_disambiguator: u32,
1091+
11011092
/// Maps glob imports to the names of items actually imported.
11021093
glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
11031094
glob_error: Option<ErrorGuaranteed>,
@@ -1509,6 +1500,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15091500
extern_crate_map: Default::default(),
15101501
module_children: Default::default(),
15111502
trait_map: NodeMap::default(),
1503+
underscore_disambiguator: 0,
15121504
empty_module,
15131505
local_module_map,
15141506
extern_module_map: Default::default(),
@@ -1889,6 +1881,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18891881
import_ids
18901882
}
18911883

1884+
fn new_disambiguated_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
1885+
let ident = ident.normalize_to_macros_2_0();
1886+
let disambiguator = if ident.name == kw::Underscore {
1887+
self.underscore_disambiguator += 1;
1888+
self.underscore_disambiguator
1889+
} else {
1890+
0
1891+
};
1892+
BindingKey { ident, ns, disambiguator }
1893+
}
1894+
18921895
fn resolutions(&mut self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
18931896
if module.populate_on_access.get() {
18941897
module.populate_on_access.set(false);

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
530530
target_trait.for_each_child(self, |this, ident, ns, _binding| {
531531
// FIXME: Adjust hygiene for idents from globs, like for glob imports.
532532
if let Some(overriding_keys) = this.impl_binding_keys.get(&impl_def_id)
533-
&& overriding_keys.contains(&BindingKey::new(ident, ns))
533+
&& overriding_keys.contains(&BindingKey::new(ident.normalize_to_macros_2_0(), ns))
534534
{
535535
// The name is overridden, do not produce it from the glob delegation.
536536
} else {

0 commit comments

Comments
 (0)