Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E0659 for ambiguous names #47512

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/librustc_resolve/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,59 @@ println!("const value: {}", SomeModule::PRIVATE); // ok!
```
"##,

E0659: r##"
An item usage is ambiguous.

Erroneous code example:

```compile_fail,E0659
pub mod moon {
pub fn foo() {}
}

pub mod earth {
pub fn foo() {}
}

mod collider {
pub use moon::*;
pub use earth::*;
}

fn main() {
collider::foo(); // ERROR: `foo` is ambiguous
}
```

This error generally appears when two items with the same name are imported into
a module. Here, the `foo` functions are imported and reexported from the
`collider` module and therefore, when we're using `collider::foo()`, both
functions collide.

To solve this error, the best solution is generally to keep the path before the
item when using it. Example:

```
pub mod moon {
pub fn foo() {}
}

pub mod earth {
pub fn foo() {}
}

mod collider {
pub use moon;
pub use earth;
}

fn main() {
collider::moon::foo(); // ok!
collider::earth::foo(); // ok!
}
```
"##,

}

register_diagnostics! {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3783,7 +3783,7 @@ impl<'a> Resolver<'a> {
self.session.buffer_lint(lint::builtin::LEGACY_IMPORTS, id, span, &msg);
} else {
let mut err =
self.session.struct_span_err(span, &format!("`{}` is ambiguous", name));
struct_span_err!(self.session, span, E0659, "`{}` is ambiguous", name);
err.span_note(b1.span, &msg1);
match b2.def() {
Def::Macro(..) if b2.span == DUMMY_SP =>
Expand Down
26 changes: 26 additions & 0 deletions src/test/compile-fail/E0659.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

mod moon {
pub fn foo() {}
}

mod earth {
pub fn foo() {}
}

mod collider {
pub use moon::*;
pub use earth::*;
}

fn main() {
collider::foo(); //~ ERROR E0659
}
8 changes: 4 additions & 4 deletions src/test/ui/imports/duplicate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ help: You can use `as` to change the binding name of the import
25 | use a::foo as Otherfoo; //~ ERROR the name `foo` is defined multiple times
| ^^^^^^^^^^^^^^^^^^

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

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

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

error: `foo` is ambiguous
error[E0659]: `foo` is ambiguous
--> $DIR/duplicate.rs:59:9
|
59 | foo::bar(); //~ ERROR `foo` is ambiguous
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/imports/macro-paths.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: `bar` is ambiguous
error[E0659]: `bar` is ambiguous
--> $DIR/macro-paths.rs:25:5
|
25 | bar::m! { //~ ERROR ambiguous
Expand All @@ -16,7 +16,7 @@ note: `bar` could also refer to the name imported here
| ^^^^^^
= note: macro-expanded items do not shadow when used in a macro invocation path

error: `baz` is ambiguous
error[E0659]: `baz` is ambiguous
--> $DIR/macro-paths.rs:35:5
|
35 | baz::m! { //~ ERROR ambiguous
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/imports/macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ note: `m` could also refer to the macro imported here
49 | use two_macros::m;
| ^^^^^^^^^^^^^

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

error: `m` is ambiguous
error[E0659]: `m` is ambiguous
--> $DIR/macros.rs:41:9
|
41 | m! { //~ ERROR ambiguous
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/imports/shadow_builtin_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error: `panic` is already in scope
|
= note: macro-expanded `macro_rules!`s may not shadow existing macros (see RFC 1560)

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

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

error: `n` is ambiguous
error[E0659]: `n` is ambiguous
--> $DIR/shadow_builtin_macros.rs:61:5
|
61 | n!(); //~ ERROR ambiguous
Expand Down