Auto merge of #61361 - estebank:infer-type, r=varkor

Add more detail to type inference error

When encountering code where type inference fails, add more actionable
information:

```
fn main() {
    let foo = Vec::new();
}
```

```
error[E0282]: type annotations needed in `std::vec::Vec<T>`
  --> $DIR/vector-no-ann.rs:2:16
   |
LL |     let foo = Vec::new();
   |         ---   ^^^^^^^^ cannot infer type for `T` in `std::vec::Vec<T>`
   |         |
   |         consider giving `foo` a type
```

Fix #25633.
This commit is contained in:
bors 2019-06-03 02:45:35 +00:00
commit d59dcb261e
20 changed files with 136 additions and 50 deletions

View file

@ -4,5 +4,5 @@ fn new<T>() -> &'static T {
fn main() {
let &v = new();
//~^ ERROR type annotations needed [E0282]
//~^ ERROR type annotations needed
}

View file

@ -1,11 +1,11 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `&T`
--> $DIR/issue-12187-1.rs:6:10
|
LL | let &v = new();
| -^
| ||
| |cannot infer type
| consider giving the pattern a type
| consider giving this pattern the explicit type `&T`, with the type parameters specified
error: aborting due to previous error

View file

@ -4,5 +4,5 @@ fn new<'r, T>() -> &'r T {
fn main() {
let &v = new();
//~^ ERROR type annotations needed [E0282]
//~^ ERROR type annotations needed
}

View file

@ -1,11 +1,11 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `&T`
--> $DIR/issue-12187-2.rs:6:10
|
LL | let &v = new();
| -^
| ||
| |cannot infer type
| consider giving the pattern a type
| consider giving this pattern the explicit type `&T`, with the type parameters specified
error: aborting due to previous error

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `B<T>`
--> $DIR/issue-17551.rs:6:15
|
LL | let foo = B(marker::PhantomData);
| --- ^ cannot infer type for `T`
| |
| consider giving `foo` a type
| consider giving `foo` the explicit type `B<T>`, where the type parameter `T` is specified
error: aborting due to previous error

View file

@ -1,4 +1,4 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `&(_,)`
--> $DIR/issue-20261.rs:4:11
|
LL | for (ref i,) in [].iter() {

View file

@ -1,8 +1,8 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `Expr<'_, VAR>`
--> $DIR/issue-23046.rs:17:15
|
LL | let ex = |x| {
| ^ consider giving this closure parameter a type
| ^ consider giving this closure parameter the explicit type `Expr<'_, VAR>`, where the type parameter `VAR` is specified
error: aborting due to previous error

View file

@ -1,8 +1,8 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `(std::sync::mpsc::Sender<Foo<T>>, std::sync::mpsc::Receiver<Foo<T>>)`
--> $DIR/issue-25368.rs:11:17
|
LL | let (tx, rx) = channel();
| -------- consider giving the pattern a type
| -------- consider giving this pattern the explicit type `(std::sync::mpsc::Sender<Foo<T>>, std::sync::mpsc::Receiver<Foo<T>>)`, where the type parameter `T` is specified
...
LL | tx.send(Foo{ foo: PhantomData });
| ^^^ cannot infer type for `T`

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `&[_; 0]`
--> $DIR/issue-7813.rs:2:13
|
LL | let v = &[];
| - ^^^ cannot infer type
| |
| consider giving `v` a type
| consider giving `v` the explicit type `&[_; 0]`, with the type parameters specified
error: aborting due to previous error

View file

@ -22,7 +22,7 @@ impl Foo for Vec<isize> {
fn m1() {
// we couldn't infer the type of the vector just based on calling foo()...
let mut x = Vec::new();
//~^ ERROR type annotations needed [E0282]
//~^ ERROR type annotations needed
x.foo();
}

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `std::vec::Vec<T>`
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:24:17
|
LL | let mut x = Vec::new();
| ----- ^^^^^^^^ cannot infer type for `T`
| |
| consider giving `x` a type
| consider giving `x` the explicit type `std::vec::Vec<T>`, where the type parameter `T` is specified
error[E0308]: mismatched types
--> $DIR/method-ambig-one-trait-unknown-int-type.rs:33:20

View file

@ -1,8 +1,8 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `std::option::Option<_>`
--> $DIR/issue-42234-unknown-receiver-type.rs:7:5
|
LL | let x: Option<_> = None;
| - consider giving `x` a type
| - consider giving `x` the explicit type `std::option::Option<_>`, where the type parameter `T` is specified
LL | x.unwrap().method_that_could_exist_on_some_type();
| ^^^^^^^^^^ cannot infer type for `T`
|

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `[_; 0]`
--> $DIR/cannot_infer_local_or_array.rs:2:13
|
LL | let x = [];
| - ^^ cannot infer type
| |
| consider giving `x` a type
| consider giving `x` the explicit type `[_; 0]`, with the type parameters specified
error: aborting due to previous error

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `std::vec::Vec<T>`
--> $DIR/cannot_infer_local_or_vec.rs:2:13
|
LL | let x = vec![];
| - ^^^^^^ cannot infer type for `T`
| |
| consider giving `x` a type
| consider giving `x` the explicit type `std::vec::Vec<T>`, where the type parameter `T` is specified
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `(std::vec::Vec<T>,)`
--> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:18
|
LL | let (x, ) = (vec![], );
| ----- ^^^^^^ cannot infer type for `T`
| |
| consider giving the pattern a type
| consider giving this pattern the explicit type `(std::vec::Vec<T>,)`, where the type parameter `T` is specified
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

View file

@ -1,8 +1,8 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `std::option::Option<T>`
--> $DIR/unboxed-closures-failed-recursive-fn-2.rs:16:32
|
LL | let mut closure0 = None;
| ------------ consider giving `closure0` a type
| ------------ consider giving `closure0` the explicit type `std::option::Option<T>`, with the type parameters specified
...
LL | return c();
| ^^^ cannot infer type

View file

@ -1,4 +1,4 @@
fn main() {
let _foo = Vec::new();
//~^ ERROR type annotations needed [E0282]
//~^ ERROR type annotations needed
}

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
error[E0282]: type annotations needed for `std::vec::Vec<T>`
--> $DIR/vector-no-ann.rs:2:16
|
LL | let _foo = Vec::new();
| ---- ^^^^^^^^ cannot infer type for `T`
| |
| consider giving `_foo` a type
| consider giving `_foo` the explicit type `std::vec::Vec<T>`, where the type parameter `T` is specified
error: aborting due to previous error