Auto merge of #114811 - estebank:impl-ambiguity, r=wesleywiser
Show more information when multiple `impl`s apply - When there are `impl`s without type params, show only those (to avoid showing overly generic `impl`s). ``` error[E0283]: type annotations needed --> $DIR/multiple-impl-apply.rs:34:9 | LL | let y = x.into(); | ^ ---- type must be known at this point | note: multiple `impl`s satisfying `_: From<Baz>` found --> $DIR/multiple-impl-apply.rs:14:1 | LL | impl From<Baz> for Bar { | ^^^^^^^^^^^^^^^^^^^^^^ ... LL | impl From<Baz> for Foo { | ^^^^^^^^^^^^^^^^^^^^^^ = note: required for `Baz` to implement `Into<_>` help: consider giving `y` an explicit type | LL | let y: /* Type */ = x.into(); | ++++++++++++ ``` - Lower the importance of `T: Sized`, `T: WellFormed` and coercion errors, to prioritize more relevant errors. The pre-existing deduplication logic deals with hiding redundant errors better that way, and we show errors with more metadata that is useful to the user. - Show `<SelfTy as Trait>::assoc_fn` suggestion in more cases. ``` error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type --> $DIR/cross-return-site-inference.rs:38:16 | LL | return Err(From::from("foo")); | ^^^^^^^^^^ cannot call associated function of trait | help: use a fully-qualified path to a specific available implementation | LL | return Err(</* self type */ as From>::from("foo")); | +++++++++++++++++++ + ``` Fix #88284.
This commit is contained in:
commit
94bc9c737e
85 changed files with 4021 additions and 3853 deletions
|
@ -3,7 +3,7 @@ The compiler could not infer a type and asked for a type annotation.
|
|||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0282
|
||||
let x = "hello".chars().rev().collect();
|
||||
let x = Vec::new();
|
||||
```
|
||||
|
||||
This error indicates that type inference did not result in one unique possible
|
||||
|
@ -11,21 +11,24 @@ type, and extra information is required. In most cases this can be provided
|
|||
by adding a type annotation. Sometimes you need to specify a generic type
|
||||
parameter manually.
|
||||
|
||||
A common example is the `collect` method on `Iterator`. It has a generic type
|
||||
parameter with a `FromIterator` bound, which for a `char` iterator is
|
||||
implemented by `Vec` and `String` among others. Consider the following snippet
|
||||
that reverses the characters of a string:
|
||||
In the example above, type `Vec` has a type parameter `T`. When calling
|
||||
`Vec::new`, barring any other later usage of the variable `x` that allows the
|
||||
compiler to infer what type `T` is, the compiler needs to be told what it is.
|
||||
|
||||
In the first code example, the compiler cannot infer what the type of `x` should
|
||||
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
|
||||
to use, you can use a type annotation on `x`:
|
||||
The type can be specified on the variable:
|
||||
|
||||
```
|
||||
let x: Vec<char> = "hello".chars().rev().collect();
|
||||
let x: Vec<i32> = Vec::new();
|
||||
```
|
||||
|
||||
It is not necessary to annotate the full type. Once the ambiguity is resolved,
|
||||
the compiler can infer the rest:
|
||||
The type can also be specified in the path of the expression:
|
||||
|
||||
```
|
||||
let x = Vec::<i32>::new();
|
||||
```
|
||||
|
||||
In cases with more complex types, it is not necessary to annotate the full
|
||||
type. Once the ambiguity is resolved, the compiler can infer the rest:
|
||||
|
||||
```
|
||||
let x: Vec<_> = "hello".chars().rev().collect();
|
||||
|
|
|
@ -1,7 +1,51 @@
|
|||
An implementation cannot be chosen unambiguously because of lack of information.
|
||||
The compiler could not infer a type and asked for a type annotation.
|
||||
|
||||
Erroneous code example:
|
||||
|
||||
```compile_fail,E0283
|
||||
let x = "hello".chars().rev().collect();
|
||||
```
|
||||
|
||||
This error indicates that type inference did not result in one unique possible
|
||||
type, and extra information is required. In most cases this can be provided
|
||||
by adding a type annotation. Sometimes you need to specify a generic type
|
||||
parameter manually.
|
||||
|
||||
A common example is the `collect` method on `Iterator`. It has a generic type
|
||||
parameter with a `FromIterator` bound, which for a `char` iterator is
|
||||
implemented by `Vec` and `String` among others. Consider the following snippet
|
||||
that reverses the characters of a string:
|
||||
|
||||
In the first code example, the compiler cannot infer what the type of `x` should
|
||||
be: `Vec<char>` and `String` are both suitable candidates. To specify which type
|
||||
to use, you can use a type annotation on `x`:
|
||||
|
||||
```
|
||||
let x: Vec<char> = "hello".chars().rev().collect();
|
||||
```
|
||||
|
||||
It is not necessary to annotate the full type. Once the ambiguity is resolved,
|
||||
the compiler can infer the rest:
|
||||
|
||||
```
|
||||
let x: Vec<_> = "hello".chars().rev().collect();
|
||||
```
|
||||
|
||||
Another way to provide the compiler with enough information, is to specify the
|
||||
generic type parameter:
|
||||
|
||||
```
|
||||
let x = "hello".chars().rev().collect::<Vec<char>>();
|
||||
```
|
||||
|
||||
Again, you need not specify the full type if the compiler can infer it:
|
||||
|
||||
```
|
||||
let x = "hello".chars().rev().collect::<Vec<_>>();
|
||||
```
|
||||
|
||||
We can see a self-contained example below:
|
||||
|
||||
```compile_fail,E0283
|
||||
struct Foo;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue