Skip to content

Commit d8371c6

Browse files
committed
Auto merge of #57199 - petrochenkov:ambig, r=estebank
resolve: Simplify treatment of ambiguity errors If we have a glob conflict like this ```rust mod m1 { struct S; } mod m2 { struct S; } use m1::*; use m2::*; ``` we treat it as a special "ambiguity item" that's not an error by itself, but produces an error when actually used. ```rust use m1::*; // primary use m2::*; // secondary => ambiguity S(m1::S, m2::S); ``` Ambiguity items were *sometimes* treated as their primary items for error recovery, but pretty irregularly. After this PR they are always treated as their primary items, except that - If an ambiguity item is marked as used, then it still produces an error. - Ambiguity items are still filtered away when exported to other crates (which is also a use in some sense).
2 parents fe6a54d + 06f22ba commit d8371c6

File tree

8 files changed

+63
-54
lines changed

8 files changed

+63
-54
lines changed

src/librustc_resolve/build_reduced_graph.rs

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl<'a> ToNameBinding<'a> for (Module<'a>, ty::Visibility, Span, Mark) {
4242
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
4343
arenas.alloc_name_binding(NameBinding {
4444
kind: NameBindingKind::Module(self.0),
45+
ambiguity: None,
4546
vis: self.1,
4647
span: self.2,
4748
expansion: self.3,
@@ -53,6 +54,7 @@ impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark) {
5354
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
5455
arenas.alloc_name_binding(NameBinding {
5556
kind: NameBindingKind::Def(self.0, false),
57+
ambiguity: None,
5658
vis: self.1,
5759
span: self.2,
5860
expansion: self.3,
@@ -66,6 +68,7 @@ impl<'a> ToNameBinding<'a> for (Def, ty::Visibility, Span, Mark, IsMacroExport)
6668
fn to_name_binding(self, arenas: &'a ResolverArenas<'a>) -> &'a NameBinding<'a> {
6769
arenas.alloc_name_binding(NameBinding {
6870
kind: NameBindingKind::Def(self.0, true),
71+
ambiguity: None,
6972
vis: self.1,
7073
span: self.2,
7174
expansion: self.3,

src/librustc_resolve/lib.rs

+27-36
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ impl<'a> fmt::Debug for ModuleData<'a> {
11911191
#[derive(Clone, Debug)]
11921192
pub struct NameBinding<'a> {
11931193
kind: NameBindingKind<'a>,
1194+
ambiguity: Option<(&'a NameBinding<'a>, AmbiguityKind)>,
11941195
expansion: Mark,
11951196
span: Span,
11961197
vis: ty::Visibility,
@@ -1215,11 +1216,6 @@ enum NameBindingKind<'a> {
12151216
directive: &'a ImportDirective<'a>,
12161217
used: Cell<bool>,
12171218
},
1218-
Ambiguity {
1219-
kind: AmbiguityKind,
1220-
b1: &'a NameBinding<'a>,
1221-
b2: &'a NameBinding<'a>,
1222-
}
12231219
}
12241220

12251221
struct PrivacyError<'a>(Span, Ident, &'a NameBinding<'a>);
@@ -1309,15 +1305,13 @@ impl<'a> NameBinding<'a> {
13091305
NameBindingKind::Def(def, _) => def,
13101306
NameBindingKind::Module(module) => module.def().unwrap(),
13111307
NameBindingKind::Import { binding, .. } => binding.def(),
1312-
NameBindingKind::Ambiguity { .. } => Def::Err,
13131308
}
13141309
}
13151310

1316-
fn def_ignoring_ambiguity(&self) -> Def {
1317-
match self.kind {
1318-
NameBindingKind::Import { binding, .. } => binding.def_ignoring_ambiguity(),
1319-
NameBindingKind::Ambiguity { b1, .. } => b1.def_ignoring_ambiguity(),
1320-
_ => self.def(),
1311+
fn is_ambiguity(&self) -> bool {
1312+
self.ambiguity.is_some() || match self.kind {
1313+
NameBindingKind::Import { binding, .. } => binding.is_ambiguity(),
1314+
_ => false,
13211315
}
13221316
}
13231317

@@ -1362,7 +1356,6 @@ impl<'a> NameBinding<'a> {
13621356
fn is_glob_import(&self) -> bool {
13631357
match self.kind {
13641358
NameBindingKind::Import { directive, .. } => directive.is_glob(),
1365-
NameBindingKind::Ambiguity { b1, .. } => b1.is_glob_import(),
13661359
_ => false,
13671360
}
13681361
}
@@ -1382,7 +1375,7 @@ impl<'a> NameBinding<'a> {
13821375
}
13831376

13841377
fn macro_kind(&self) -> Option<MacroKind> {
1385-
match self.def_ignoring_ambiguity() {
1378+
match self.def() {
13861379
Def::Macro(_, kind) => Some(kind),
13871380
Def::NonMacroAttr(..) => Some(MacroKind::Attr),
13881381
_ => None,
@@ -1893,6 +1886,7 @@ impl<'a> Resolver<'a> {
18931886
arenas,
18941887
dummy_binding: arenas.alloc_name_binding(NameBinding {
18951888
kind: NameBindingKind::Def(Def::Err, false),
1889+
ambiguity: None,
18961890
expansion: Mark::root(),
18971891
span: DUMMY_SP,
18981892
vis: ty::Visibility::Public,
@@ -1963,33 +1957,30 @@ impl<'a> Resolver<'a> {
19631957

19641958
fn record_use(&mut self, ident: Ident, ns: Namespace,
19651959
used_binding: &'a NameBinding<'a>, is_lexical_scope: bool) {
1966-
match used_binding.kind {
1967-
NameBindingKind::Import { directive, binding, ref used } if !used.get() => {
1968-
// Avoid marking `extern crate` items that refer to a name from extern prelude,
1969-
// but not introduce it, as used if they are accessed from lexical scope.
1970-
if is_lexical_scope {
1971-
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
1972-
if let Some(crate_item) = entry.extern_crate_item {
1973-
if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item {
1974-
return;
1975-
}
1960+
if let Some((b2, kind)) = used_binding.ambiguity {
1961+
self.ambiguity_errors.push(AmbiguityError {
1962+
kind, ident, b1: used_binding, b2,
1963+
misc1: AmbiguityErrorMisc::None,
1964+
misc2: AmbiguityErrorMisc::None,
1965+
});
1966+
}
1967+
if let NameBindingKind::Import { directive, binding, ref used } = used_binding.kind {
1968+
// Avoid marking `extern crate` items that refer to a name from extern prelude,
1969+
// but not introduce it, as used if they are accessed from lexical scope.
1970+
if is_lexical_scope {
1971+
if let Some(entry) = self.extern_prelude.get(&ident.modern()) {
1972+
if let Some(crate_item) = entry.extern_crate_item {
1973+
if ptr::eq(used_binding, crate_item) && !entry.introduced_by_item {
1974+
return;
19761975
}
19771976
}
19781977
}
1979-
used.set(true);
1980-
directive.used.set(true);
1981-
self.used_imports.insert((directive.id, ns));
1982-
self.add_to_glob_map(directive.id, ident);
1983-
self.record_use(ident, ns, binding, false);
1984-
}
1985-
NameBindingKind::Ambiguity { kind, b1, b2 } => {
1986-
self.ambiguity_errors.push(AmbiguityError {
1987-
kind, ident, b1, b2,
1988-
misc1: AmbiguityErrorMisc::None,
1989-
misc2: AmbiguityErrorMisc::None,
1990-
});
19911978
}
1992-
_ => {}
1979+
used.set(true);
1980+
directive.used.set(true);
1981+
self.used_imports.insert((directive.id, ns));
1982+
self.add_to_glob_map(directive.id, ident);
1983+
self.record_use(ident, ns, binding, false);
19931984
}
19941985
}
19951986

src/librustc_resolve/macros.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ impl<'a> base::Resolver for Resolver<'a> {
175175
self.macro_map.insert(def_id, ext);
176176
let binding = self.arenas.alloc_name_binding(NameBinding {
177177
kind: NameBindingKind::Def(Def::Macro(def_id, kind), false),
178+
ambiguity: None,
178179
span: DUMMY_SP,
179180
vis: ty::Visibility::Public,
180181
expansion: Mark::root(),
@@ -389,7 +390,7 @@ impl<'a> Resolver<'a> {
389390
.push((path[0].ident, kind, parent_scope.clone(), binding.ok()));
390391
}
391392

392-
binding.map(|binding| binding.def_ignoring_ambiguity())
393+
binding.map(|binding| binding.def())
393394
}
394395
}
395396

@@ -950,9 +951,9 @@ impl<'a> Resolver<'a> {
950951
Ok(binding) => {
951952
let initial_def = initial_binding.map(|initial_binding| {
952953
self.record_use(ident, MacroNS, initial_binding, false);
953-
initial_binding.def_ignoring_ambiguity()
954+
initial_binding.def()
954955
});
955-
let def = binding.def_ignoring_ambiguity();
956+
let def = binding.def();
956957
let seg = Segment::from_ident(ident);
957958
check_consistency(self, &[seg], ident.span, kind, initial_def, def);
958959
}

src/librustc_resolve/resolve_imports.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ impl<'a> Resolver<'a> {
445445
directive,
446446
used: Cell::new(false),
447447
},
448+
ambiguity: None,
448449
span: directive.span,
449450
vis,
450451
expansion: directive.parent_scope.expansion,
@@ -498,8 +499,8 @@ impl<'a> Resolver<'a> {
498499
nonglob_binding, glob_binding));
499500
} else {
500501
resolution.binding = Some(nonglob_binding);
501-
resolution.shadowed_glob = Some(glob_binding);
502502
}
503+
resolution.shadowed_glob = Some(glob_binding);
503504
}
504505
(false, false) => {
505506
if let (&NameBindingKind::Def(_, true), &NameBindingKind::Def(_, true)) =
@@ -527,13 +528,15 @@ impl<'a> Resolver<'a> {
527528
})
528529
}
529530

530-
fn ambiguity(&self, kind: AmbiguityKind, b1: &'a NameBinding<'a>, b2: &'a NameBinding<'a>)
531-
-> &'a NameBinding<'a> {
531+
fn ambiguity(&self, kind: AmbiguityKind,
532+
primary_binding: &'a NameBinding<'a>, secondary_binding: &'a NameBinding<'a>)
533+
-> &'a NameBinding<'a> {
532534
self.arenas.alloc_name_binding(NameBinding {
533-
kind: NameBindingKind::Ambiguity { kind, b1, b2 },
534-
vis: if b1.vis.is_at_least(b2.vis, self) { b1.vis } else { b2.vis },
535-
span: b1.span,
536-
expansion: Mark::root(),
535+
kind: primary_binding.kind.clone(),
536+
ambiguity: Some((secondary_binding, kind)),
537+
vis: primary_binding.vis,
538+
span: primary_binding.span,
539+
expansion: primary_binding.expansion,
537540
})
538541
}
539542

@@ -962,9 +965,9 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
962965
directive.module_path.is_empty());
963966
}
964967
}
965-
initial_binding.def_ignoring_ambiguity()
968+
initial_binding.def()
966969
});
967-
let def = binding.def_ignoring_ambiguity();
970+
let def = binding.def();
968971
if let Ok(initial_def) = initial_def {
969972
if def != initial_def && this.ambiguity_errors.is_empty() {
970973
span_bug!(directive.span, "inconsistent resolution for an import");
@@ -1201,10 +1204,10 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
12011204
None => continue,
12021205
};
12031206

1204-
// Filter away "empty import canaries".
1205-
let is_non_canary_import =
1206-
binding.is_import() && binding.vis != ty::Visibility::Invisible;
1207-
if is_non_canary_import || binding.is_macro_def() {
1207+
// Filter away "empty import canaries" and ambiguous imports.
1208+
let is_good_import = binding.is_import() && !binding.is_ambiguity() &&
1209+
binding.vis != ty::Visibility::Invisible;
1210+
if is_good_import || binding.is_macro_def() {
12081211
let def = binding.def();
12091212
if def != Def::Err {
12101213
if let Some(def_id) = def.opt_def_id() {

src/test/ui/imports/auxiliary/glob-conflict.rs

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ mod m2 {
77

88
pub use m1::*;
99
pub use m2::*;
10+
11+
pub mod glob {
12+
pub use *;
13+
}

src/test/ui/imports/duplicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn main() {
3737
}
3838

3939
mod ambiguous_module_errors {
40-
pub mod m1 { pub use super::m1 as foo; }
40+
pub mod m1 { pub use super::m1 as foo; pub fn bar() {} }
4141
pub mod m2 { pub use super::m2 as foo; }
4242

4343
use self::m1::*;

src/test/ui/imports/glob-conflict-cross-crate.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ extern crate glob_conflict;
44

55
fn main() {
66
glob_conflict::f(); //~ ERROR cannot find function `f` in module `glob_conflict`
7+
glob_conflict::glob::f(); //~ ERROR cannot find function `f` in module `glob_conflict::glob`
78
}

src/test/ui/imports/glob-conflict-cross-crate.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0425]: cannot find function `f` in module `glob_conflict`
44
LL | glob_conflict::f(); //~ ERROR cannot find function `f` in module `glob_conflict`
55
| ^ not found in `glob_conflict`
66

7-
error: aborting due to previous error
7+
error[E0425]: cannot find function `f` in module `glob_conflict::glob`
8+
--> $DIR/glob-conflict-cross-crate.rs:7:26
9+
|
10+
LL | glob_conflict::glob::f(); //~ ERROR cannot find function `f` in module `glob_conflict::glob`
11+
| ^ not found in `glob_conflict::glob`
12+
13+
error: aborting due to 2 previous errors
814

915
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)