Skip to content

Commit 6b292cd

Browse files
committed
rustc: Add long diagnostics for E0282
1 parent 52e520e commit 6b292cd

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

src/librustc/diagnostics.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,73 @@ loop. Without a loop to break out of or continue in, no sensible action can be
368368
taken.
369369
"##,
370370

371+
E0282: r##"
372+
This error indicates that type inference did not result in one unique possible
373+
type, and extra information is required. In most cases this can be provided
374+
by adding a type annotation. Sometimes you need to specify a generic type
375+
parameter manually.
376+
377+
A common example is the `collect` method on `Iterator`. It has a generic type
378+
parameter with a `FromIterator` bound, which is implemented by `Vec` and
379+
`VecDeque` among others. Consider the following snippet:
380+
381+
```
382+
let x = (1_i32 .. 10).collect();
383+
```
384+
385+
In this case, the compiler cannot infer what the type of `x` should be:
386+
`Vec<i32>` and `VecDeque<i32>` are both suitable candidates. To specify which
387+
type to use, you can use a type annotation on `x`:
388+
389+
```
390+
let x: Vec<i32> = (1_i32 .. 10).collect();
391+
```
392+
393+
It is not necessary to annotate the full type, once the ambiguity is resolved,
394+
the compiler can infer the rest:
395+
396+
```
397+
let x: Vec<_> = (1_i32 .. 10).collect();
398+
```
399+
400+
Another way to provide the compiler with enough information, is to specify the
401+
generic type parameter:
402+
403+
```
404+
let x = (1_i32 .. 10).collect::<Vec<i32>>();
405+
```
406+
407+
Again, you need not specify the full type if the compiler can infer it:
408+
409+
```
410+
let x = (1_i32 .. 10).collect::<Vec<_>>();
411+
```
412+
413+
Apart from a method or function with a generic type parameter, this error can
414+
occur when a type parameter of a struct or trait cannot be inferred. In that
415+
case it is not always possible to use a type annotation, because all candidates
416+
have the same return type. For instance:
417+
418+
```
419+
struct Foo<T> {
420+
// Some fields omitted.
421+
}
422+
423+
impl<T> Foo<T> {
424+
fn bar() -> i32 {
425+
0
426+
}
427+
428+
fn baz() {
429+
let number = Foo::bar();
430+
}
431+
}
432+
```
433+
434+
This will fail because the compiler does not know which instance of `Foo` to
435+
call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
436+
"##,
437+
371438
E0296: r##"
372439
This error indicates that the given recursion limit could not be parsed. Ensure
373440
that the value provided is a positive integer between quotes, like so:
@@ -515,7 +582,6 @@ register_diagnostics! {
515582
E0279, // requirement is not satisfied
516583
E0280, // requirement is not satisfied
517584
E0281, // type implements trait but other trait is required
518-
E0282, // unable to infer enough type information about
519585
E0283, // cannot resolve type
520586
E0284, // cannot resolve type
521587
E0285, // overflow evaluation builtin bounds

0 commit comments

Comments
 (0)