Add E0790 as more specific variant of E0283
This commit is contained in:
parent
96c2df810b
commit
1cbacc0c8a
21 changed files with 354 additions and 92 deletions
|
@ -3,48 +3,31 @@ An implementation cannot be chosen unambiguously because of lack of information.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0283
|
||||
trait Generator {
|
||||
fn create() -> u32;
|
||||
}
|
||||
struct Foo;
|
||||
|
||||
struct Impl;
|
||||
|
||||
impl Generator for Impl {
|
||||
fn create() -> u32 { 1 }
|
||||
}
|
||||
|
||||
struct AnotherImpl;
|
||||
|
||||
impl Generator for AnotherImpl {
|
||||
fn create() -> u32 { 2 }
|
||||
impl Into<u32> for Foo {
|
||||
fn into(self) -> u32 { 1 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cont: u32 = Generator::create();
|
||||
// error, impossible to choose one of Generator trait implementation
|
||||
// Should it be Impl or AnotherImpl, maybe something else?
|
||||
let foo = Foo;
|
||||
let bar: u32 = foo.into() * 1u32;
|
||||
}
|
||||
```
|
||||
|
||||
This error can be solved by adding type annotations that provide the missing
|
||||
information to the compiler. In this case, the solution is to use a concrete
|
||||
type:
|
||||
information to the compiler. In this case, the solution is to specify the
|
||||
fully-qualified method:
|
||||
|
||||
```
|
||||
trait Generator {
|
||||
fn create() -> u32;
|
||||
}
|
||||
struct Foo;
|
||||
|
||||
struct AnotherImpl;
|
||||
|
||||
impl Generator for AnotherImpl {
|
||||
fn create() -> u32 { 2 }
|
||||
impl Into<u32> for Foo {
|
||||
fn into(self) -> u32 { 1 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let gen1 = AnotherImpl::create();
|
||||
|
||||
// if there are multiple methods with same name (different traits)
|
||||
let gen2 = <AnotherImpl as Generator>::create();
|
||||
let foo = Foo;
|
||||
let bar: u32 = <Foo as Into<u32>>::into(foo) * 1u32;
|
||||
}
|
||||
```
|
||||
|
|
51
compiler/rustc_error_codes/src/error_codes/E0790.md
Normal file
51
compiler/rustc_error_codes/src/error_codes/E0790.md
Normal file
|
@ -0,0 +1,51 @@
|
|||
You need to specify a specific implementation of the trait in order to call the
|
||||
method.
|
||||
|
||||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0790
|
||||
trait Generator {
|
||||
fn create() -> u32;
|
||||
}
|
||||
|
||||
struct Impl;
|
||||
|
||||
impl Generator for Impl {
|
||||
fn create() -> u32 { 1 }
|
||||
}
|
||||
|
||||
struct AnotherImpl;
|
||||
|
||||
impl Generator for AnotherImpl {
|
||||
fn create() -> u32 { 2 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cont: u32 = Generator::create();
|
||||
// error, impossible to choose one of Generator trait implementation
|
||||
// Should it be Impl or AnotherImpl, maybe something else?
|
||||
}
|
||||
```
|
||||
|
||||
This error can be solved by adding type annotations that provide the missing
|
||||
information to the compiler. In this case, the solution is to use a concrete
|
||||
type:
|
||||
|
||||
```
|
||||
trait Generator {
|
||||
fn create() -> u32;
|
||||
}
|
||||
|
||||
struct AnotherImpl;
|
||||
|
||||
impl Generator for AnotherImpl {
|
||||
fn create() -> u32 { 2 }
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let gen1 = AnotherImpl::create();
|
||||
|
||||
// if there are multiple methods with same name (different traits)
|
||||
let gen2 = <AnotherImpl as Generator>::create();
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue