Rollup merge of #47512 - GuillaumeGomez:e0659, r=petrochenkov
Add E0659 for ambiguous names Still on the tracks of the "no error without error code" road.
This commit is contained in:
commit
a1c3449a9c
@ -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! {
|
||||
|
@ -3802,7 +3802,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 =>
|
||||
|
26
src/test/compile-fail/E0659.rs
Normal file
26
src/test/compile-fail/E0659.rs
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
error: `bar` is ambiguous
|
||||
error[E0659]: `bar` is ambiguous
|
||||
--> $DIR/macro-paths.rs:25:5
|
||||
|
|
||||
25 | bar::m! { //~ ERROR ambiguous
|
||||
@ -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
|
||||
|
@ -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
|
||||
@ -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
|
||||
|
@ -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
|
||||
@ -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
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user