Skip to content

Commit d3f416d

Browse files
committed
Auto merge of #110493 - bvanjoi:new_disambiguated_key, r=petrochenkov
fix(resolve): only disambiguate binding key during define - close #110164 - discussion: #110264 (comment) r? `@petrochenkov`
2 parents fdd0301 + 5b09810 commit d3f416d

File tree

8 files changed

+114
-16
lines changed

8 files changed

+114
-16
lines changed

compiler/rustc_resolve/src/build_reduced_graph.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ use crate::def_collector::collect_definitions;
99
use crate::imports::{Import, ImportKind};
1010
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
1111
use crate::Namespace::{self, MacroNS, TypeNS, ValueNS};
12-
use crate::{
13-
errors, Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot,
14-
};
15-
use crate::{
16-
MacroData, NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError,
17-
};
12+
use crate::{errors, BindingKey, MacroData};
13+
use crate::{Determinacy, ExternPreludeEntry, Finalize, Module, ModuleKind, ModuleOrUniformRoot};
14+
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PerNS, ResolutionError};
1815
use crate::{Resolver, ResolverArenas, Segment, ToNameBinding, VisResolutionError};
1916

2017
use rustc_ast::visit::{self, AssocCtxt, Visitor};
@@ -72,7 +69,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
7269
T: ToNameBinding<'a>,
7370
{
7471
let binding = def.to_name_binding(self.arenas);
75-
let key = self.new_key(ident, ns);
72+
let key = self.new_disambiguated_key(ident, ns);
7673
if let Err(old_binding) = self.try_define(parent, key, binding) {
7774
self.report_conflict(parent, ident, ns, old_binding, &binding);
7875
}
@@ -379,7 +376,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
379376
ImportKind::Single { target, type_ns_only, .. } => {
380377
self.r.per_ns(|this, ns| {
381378
if !type_ns_only || ns == TypeNS {
382-
let key = this.new_key(target, ns);
379+
let key = BindingKey::new(target, ns);
383380
let mut resolution = this.resolution(current_module, key).borrow_mut();
384381
resolution.add_single_import(import);
385382
}

compiler/rustc_resolve/src/diagnostics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
2828
use rustc_span::{BytePos, Span, SyntaxContext};
2929
use thin_vec::ThinVec;
3030

31-
use crate::errors as errs;
3231
use crate::imports::{Import, ImportKind};
3332
use crate::late::{PatternSource, Rib};
3433
use crate::path_names_to_string;
34+
use crate::{errors as errs, BindingKey};
3535
use crate::{AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, Finalize};
3636
use crate::{HasGenericParams, MacroRulesScope, Module, ModuleKind, ModuleOrUniformRoot};
3737
use crate::{LexicalScopeBinding, NameBinding, NameBindingKind, PrivacyError, VisResolutionError};
@@ -2081,7 +2081,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
20812081
}
20822082

20832083
let resolutions = self.resolutions(crate_module).borrow();
2084-
let resolution = resolutions.get(&self.new_key(ident, MacroNS))?;
2084+
let binding_key = BindingKey::new(ident, MacroNS);
2085+
let resolution = resolutions.get(&binding_key)?;
20852086
let binding = resolution.borrow().binding()?;
20862087
if let Res::Def(DefKind::Macro(MacroKind::Bang), _) = binding.res() {
20872088
let module_name = crate_module.kind.name().unwrap();

compiler/rustc_resolve/src/ident.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::late::{
1818
ConstantHasGenerics, HasGenericParams, NoConstantGenericsReason, PathSource, Rib, RibKind,
1919
};
2020
use crate::macros::{sub_namespace_match, MacroRulesScope};
21+
use crate::BindingKey;
2122
use crate::{errors, AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, Determinacy, Finalize};
2223
use crate::{Import, ImportKind, LexicalScopeBinding, Module, ModuleKind, ModuleOrUniformRoot};
2324
use crate::{NameBinding, NameBindingKind, ParentScope, PathResult, PrivacyError, Res};
@@ -865,7 +866,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
865866
}
866867
};
867868

868-
let key = self.new_key(ident, ns);
869+
let key = BindingKey::new(ident, ns);
869870
let resolution =
870871
self.resolution(module, key).try_borrow_mut().map_err(|_| (Determined, Weak::No))?; // This happens when there is a cycle of imports.
871872

compiler/rustc_resolve/src/imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
415415
let dummy_binding = self.dummy_binding;
416416
let dummy_binding = self.import(dummy_binding, import);
417417
self.per_ns(|this, ns| {
418-
let key = this.new_key(target, ns);
418+
let key = BindingKey::new(target, ns);
419419
let _ = this.try_define(import.parent_scope.module, key, dummy_binding);
420420
});
421421
self.record_use(target, dummy_binding, false);
@@ -712,7 +712,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
712712
.span_label(import.span, "cannot be imported directly")
713713
.emit();
714714
}
715-
let key = this.new_key(target, ns);
715+
let key = BindingKey::new(target, ns);
716716
this.update_resolution(parent, key, |_, resolution| {
717717
resolution.single_imports.remove(&Interned::new_unchecked(import));
718718
});

compiler/rustc_resolve/src/late.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
77
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.
88
9+
use crate::BindingKey;
910
use crate::{path_names_to_string, rustdoc, BindingError, Finalize, LexicalScopeBinding};
1011
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
1112
use crate::{ResolutionError, Resolver, Segment, UseError};
@@ -2967,7 +2968,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29672968
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
29682969
let Some((module, _)) = &self.current_trait_ref else { return; };
29692970
ident.span.normalize_to_macros_2_0_and_adjust(module.expansion);
2970-
let key = self.r.new_key(ident, ns);
2971+
let key = BindingKey::new(ident, ns);
29712972
let mut binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
29722973
debug!(?binding);
29732974
if binding.is_none() {
@@ -2978,7 +2979,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
29782979
TypeNS => ValueNS,
29792980
_ => ns,
29802981
};
2981-
let key = self.r.new_key(ident, ns);
2982+
let key = BindingKey::new(ident, ns);
29822983
binding = self.r.resolution(module, key).try_borrow().ok().and_then(|r| r.binding);
29832984
debug!(?binding);
29842985
}

compiler/rustc_resolve/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,13 @@ struct BindingKey {
469469
disambiguator: u32,
470470
}
471471

472+
impl BindingKey {
473+
fn new(ident: Ident, ns: Namespace) -> Self {
474+
let ident = ident.normalize_to_macros_2_0();
475+
BindingKey { ident, ns, disambiguator: 0 }
476+
}
477+
}
478+
472479
type Resolutions<'a> = RefCell<FxIndexMap<BindingKey, &'a RefCell<NameResolution<'a>>>>;
473480

474481
/// One node in the tree of modules.
@@ -943,6 +950,7 @@ pub struct Resolver<'a, 'tcx> {
943950
empty_module: Module<'a>,
944951
module_map: FxHashMap<DefId, Module<'a>>,
945952
binding_parent_modules: FxHashMap<Interned<'a, NameBinding<'a>>, Module<'a>>,
953+
946954
underscore_disambiguator: u32,
947955

948956
/// Maps glob imports to the names of items actually imported.
@@ -1595,7 +1603,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15951603
import_ids
15961604
}
15971605

1598-
fn new_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
1606+
fn new_disambiguated_key(&mut self, ident: Ident, ns: Namespace) -> BindingKey {
15991607
let ident = ident.normalize_to_macros_2_0();
16001608
let disambiguator = if ident.name == kw::Underscore {
16011609
self.underscore_disambiguator += 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use self::*;
2+
//~^ ERROR unresolved import `self::*`
3+
use crate::*;
4+
//~^ ERROR unresolved import `crate::*`
5+
use _::a;
6+
//~^ ERROR expected identifier, found reserved identifier `_`
7+
//~| ERROR unresolved import `_`
8+
use _::*;
9+
//~^ ERROR expected identifier, found reserved identifier `_`
10+
//~| ERROR unresolved import `_`
11+
12+
fn main() {
13+
use _::a;
14+
//~^ ERROR expected identifier, found reserved identifier `_`
15+
//~| ERROR unresolved import `_`
16+
use _::*;
17+
//~^ ERROR expected identifier, found reserved identifier `_`
18+
//~| ERROR unresolved import `_`
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error: expected identifier, found reserved identifier `_`
2+
--> $DIR/issue-110164.rs:5:5
3+
|
4+
LL | use _::a;
5+
| ^ expected identifier, found reserved identifier
6+
7+
error: expected identifier, found reserved identifier `_`
8+
--> $DIR/issue-110164.rs:8:5
9+
|
10+
LL | use _::*;
11+
| ^ expected identifier, found reserved identifier
12+
13+
error: expected identifier, found reserved identifier `_`
14+
--> $DIR/issue-110164.rs:13:9
15+
|
16+
LL | use _::a;
17+
| ^ expected identifier, found reserved identifier
18+
19+
error: expected identifier, found reserved identifier `_`
20+
--> $DIR/issue-110164.rs:16:9
21+
|
22+
LL | use _::*;
23+
| ^ expected identifier, found reserved identifier
24+
25+
error[E0432]: unresolved import `self::*`
26+
--> $DIR/issue-110164.rs:1:5
27+
|
28+
LL | use self::*;
29+
| ^^^^^^^ cannot glob-import a module into itself
30+
31+
error[E0432]: unresolved import `crate::*`
32+
--> $DIR/issue-110164.rs:3:5
33+
|
34+
LL | use crate::*;
35+
| ^^^^^^^^ cannot glob-import a module into itself
36+
37+
error[E0432]: unresolved import `_`
38+
--> $DIR/issue-110164.rs:8:5
39+
|
40+
LL | use _::*;
41+
| ^ maybe a missing crate `_`?
42+
|
43+
= help: consider adding `extern crate _` to use the `_` crate
44+
45+
error[E0432]: unresolved import `_`
46+
--> $DIR/issue-110164.rs:5:5
47+
|
48+
LL | use _::a;
49+
| ^ maybe a missing crate `_`?
50+
|
51+
= help: consider adding `extern crate _` to use the `_` crate
52+
53+
error[E0432]: unresolved import `_`
54+
--> $DIR/issue-110164.rs:13:9
55+
|
56+
LL | use _::a;
57+
| ^ maybe a missing crate `_`?
58+
|
59+
= help: consider adding `extern crate _` to use the `_` crate
60+
61+
error[E0432]: unresolved import `_`
62+
--> $DIR/issue-110164.rs:16:9
63+
|
64+
LL | use _::*;
65+
| ^ maybe a missing crate `_`?
66+
|
67+
= help: consider adding `extern crate _` to use the `_` crate
68+
69+
error: aborting due to 10 previous errors
70+
71+
For more information about this error, try `rustc --explain E0432`.

0 commit comments

Comments
 (0)