Skip to content

Commit f66e711

Browse files
committedJan 18, 2018
Add E0659 for ambiguous names
1 parent da569fa commit f66e711

File tree

7 files changed

+91
-12
lines changed

7 files changed

+91
-12
lines changed
 

‎src/librustc_resolve/diagnostics.rs

+53
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,59 @@ println!("const value: {}", SomeModule::PRIVATE); // ok!
16211621
```
16221622
"##,
16231623

1624+
E0659: r##"
1625+
An item usage is ambiguous.
1626+
1627+
Erroneous code example:
1628+
1629+
```compile_fail,E0659
1630+
pub mod moon {
1631+
pub fn foo() {}
1632+
}
1633+
1634+
pub mod earth {
1635+
pub fn foo() {}
1636+
}
1637+
1638+
mod collider {
1639+
pub use moon::*;
1640+
pub use earth::*;
1641+
}
1642+
1643+
fn main() {
1644+
collider::foo(); // ERROR: `foo` is ambiguous
1645+
}
1646+
```
1647+
1648+
This error generally appears when two items with the same name are imported into
1649+
a module. Here, the `foo` functions are imported and reexported from the
1650+
`collider` module and therefore, when we're using `collider::foo()`, both
1651+
functions collide.
1652+
1653+
To solve this error, the best solution is generally to keep the path before the
1654+
item when using it. Example:
1655+
1656+
```
1657+
pub mod moon {
1658+
pub fn foo() {}
1659+
}
1660+
1661+
pub mod earth {
1662+
pub fn foo() {}
1663+
}
1664+
1665+
mod collider {
1666+
pub use moon;
1667+
pub use earth;
1668+
}
1669+
1670+
fn main() {
1671+
collider::moon::foo(); // ok!
1672+
collider::earth::foo(); // ok!
1673+
}
1674+
```
1675+
"##,
1676+
16241677
}
16251678

16261679
register_diagnostics! {

‎src/librustc_resolve/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3783,7 +3783,7 @@ impl<'a> Resolver<'a> {
37833783
self.session.buffer_lint(lint::builtin::LEGACY_IMPORTS, id, span, &msg);
37843784
} else {
37853785
let mut err =
3786-
self.session.struct_span_err(span, &format!("`{}` is ambiguous", name));
3786+
struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
37873787
err.span_note(b1.span, &msg1);
37883788
match b2.def() {
37893789
Def::Macro(..) if b2.span == DUMMY_SP =>

‎src/test/compile-fail/E0659.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod moon {
12+
pub fn foo() {}
13+
}
14+
15+
mod earth {
16+
pub fn foo() {}
17+
}
18+
19+
mod collider {
20+
pub use moon::*;
21+
pub use earth::*;
22+
}
23+
24+
fn main() {
25+
collider::foo(); //~ ERROR E0659
26+
}

‎src/test/ui/imports/duplicate.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ help: You can use `as` to change the binding name of the import
1212
25 | use a::foo as Otherfoo; //~ ERROR the name `foo` is defined multiple times
1313
| ^^^^^^^^^^^^^^^^^^
1414

15-
error: `foo` is ambiguous
15+
error[E0659]: `foo` is ambiguous
1616
--> $DIR/duplicate.rs:56:9
1717
|
1818
56 | use self::foo::bar; //~ ERROR `foo` is ambiguous
@@ -30,7 +30,7 @@ note: `foo` could also refer to the name imported here
3030
| ^^^^^^^^^^^
3131
= note: consider adding an explicit import of `foo` to disambiguate
3232

33-
error: `foo` is ambiguous
33+
error[E0659]: `foo` is ambiguous
3434
--> $DIR/duplicate.rs:45:5
3535
|
3636
45 | f::foo(); //~ ERROR `foo` is ambiguous
@@ -48,7 +48,7 @@ note: `foo` could also refer to the name imported here
4848
| ^^^^
4949
= note: consider adding an explicit import of `foo` to disambiguate
5050

51-
error: `foo` is ambiguous
51+
error[E0659]: `foo` is ambiguous
5252
--> $DIR/duplicate.rs:46:5
5353
|
5454
46 | g::foo(); //~ ERROR `foo` is ambiguous
@@ -66,7 +66,7 @@ note: `foo` could also refer to the name imported here
6666
| ^^^^
6767
= note: consider adding an explicit import of `foo` to disambiguate
6868

69-
error: `foo` is ambiguous
69+
error[E0659]: `foo` is ambiguous
7070
--> $DIR/duplicate.rs:59:9
7171
|
7272
59 | foo::bar(); //~ ERROR `foo` is ambiguous

‎src/test/ui/imports/macro-paths.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `bar` is ambiguous
1+
error[E0659]: `bar` is ambiguous
22
--> $DIR/macro-paths.rs:25:5
33
|
44
25 | bar::m! { //~ ERROR ambiguous
@@ -16,7 +16,7 @@ note: `bar` could also refer to the name imported here
1616
| ^^^^^^
1717
= note: macro-expanded items do not shadow when used in a macro invocation path
1818

19-
error: `baz` is ambiguous
19+
error[E0659]: `baz` is ambiguous
2020
--> $DIR/macro-paths.rs:35:5
2121
|
2222
35 | baz::m! { //~ ERROR ambiguous

‎src/test/ui/imports/macros.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ note: `m` could also refer to the macro imported here
1515
49 | use two_macros::m;
1616
| ^^^^^^^^^^^^^
1717

18-
error: `m` is ambiguous
18+
error[E0659]: `m` is ambiguous
1919
--> $DIR/macros.rs:28:5
2020
|
2121
28 | m! { //~ ERROR ambiguous
@@ -33,7 +33,7 @@ note: `m` could also refer to the name imported here
3333
| ^^^^^^^^^^^^^
3434
= note: macro-expanded macro imports do not shadow
3535

36-
error: `m` is ambiguous
36+
error[E0659]: `m` is ambiguous
3737
--> $DIR/macros.rs:41:9
3838
|
3939
41 | m! { //~ ERROR ambiguous

‎src/test/ui/imports/shadow_builtin_macros.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ error: `panic` is already in scope
99
|
1010
= note: macro-expanded `macro_rules!`s may not shadow existing macros (see RFC 1560)
1111

12-
error: `panic` is ambiguous
12+
error[E0659]: `panic` is ambiguous
1313
--> $DIR/shadow_builtin_macros.rs:27:14
1414
|
1515
27 | fn f() { panic!(); } //~ ERROR ambiguous
@@ -23,7 +23,7 @@ note: `panic` could refer to the name imported here
2323
= note: `panic` is also a builtin macro
2424
= note: consider adding an explicit import of `panic` to disambiguate
2525

26-
error: `panic` is ambiguous
26+
error[E0659]: `panic` is ambiguous
2727
--> $DIR/shadow_builtin_macros.rs:32:14
2828
|
2929
32 | fn f() { panic!(); } //~ ERROR ambiguous
@@ -37,7 +37,7 @@ note: `panic` could refer to the name imported here
3737
= note: `panic` is also a builtin macro
3838
= note: macro-expanded macro imports do not shadow
3939

40-
error: `n` is ambiguous
40+
error[E0659]: `n` is ambiguous
4141
--> $DIR/shadow_builtin_macros.rs:61:5
4242
|
4343
61 | n!(); //~ ERROR ambiguous

0 commit comments

Comments
 (0)
Please sign in to comment.