Skip to content

Commit 74d24ad

Browse files
committed
Rollup merge of #24966 - ruud-v-a:explain, r=pnkfelix
The error message was misleading, so I adjusted it, and I also added the long diagnostics for this error (resolves one point in #24407). I was unsure about how to phrase the error message. Is “generic parameter binding” the correct term for this?
2 parents 5217430 + 4b8098b commit 74d24ad

15 files changed

+99
-28
lines changed

src/librustc/diagnostics.rs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,74 @@ of a loop. Without a loop to break out of or continue in, no sensible action can
419419
be taken.
420420
"##,
421421

422+
E0282: r##"
423+
This error indicates that type inference did not result in one unique possible
424+
type, and extra information is required. In most cases this can be provided
425+
by adding a type annotation. Sometimes you need to specify a generic type
426+
parameter manually.
427+
428+
A common example is the `collect` method on `Iterator`. It has a generic type
429+
parameter with a `FromIterator` bound, which for a `char` iterator is
430+
implemented by `Vec` and `String` among others. Consider the following snippet
431+
that reverses the characters of a string:
432+
433+
```
434+
let x = "hello".chars().rev().collect();
435+
```
436+
437+
In this case, the compiler cannot infer what the type of `x` should be:
438+
`Vec<char>` and `String` are both suitable candidates. To specify which type to
439+
use, you can use a type annotation on `x`:
440+
441+
```
442+
let x: Vec<char> = "hello".chars().rev().collect();
443+
```
444+
445+
It is not necessary to annotate the full type. Once the ambiguity is resolved,
446+
the compiler can infer the rest:
447+
448+
```
449+
let x: Vec<_> = "hello".chars().rev().collect();
450+
```
451+
452+
Another way to provide the compiler with enough information, is to specify the
453+
generic type parameter:
454+
455+
```
456+
let x = "hello".chars().rev().collect::<Vec<char>>();
457+
```
458+
459+
Again, you need not specify the full type if the compiler can infer it:
460+
461+
```
462+
let x = "hello".chars().rev().collect::<Vec<_>>();
463+
```
464+
465+
Apart from a method or function with a generic type parameter, this error can
466+
occur when a type parameter of a struct or trait cannot be inferred. In that
467+
case it is not always possible to use a type annotation, because all candidates
468+
have the same return type. For instance:
469+
470+
```
471+
struct Foo<T> {
472+
// Some fields omitted.
473+
}
474+
475+
impl<T> Foo<T> {
476+
fn bar() -> i32 {
477+
0
478+
}
479+
480+
fn baz() {
481+
let number = Foo::bar();
482+
}
483+
}
484+
```
485+
486+
This will fail because the compiler does not know which instance of `Foo` to
487+
call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
488+
"##,
489+
422490
E0296: r##"
423491
This error indicates that the given recursion limit could not be parsed. Ensure
424492
that the value provided is a positive integer between quotes, like so:
@@ -617,7 +685,6 @@ register_diagnostics! {
617685
E0279, // requirement is not satisfied
618686
E0280, // requirement is not satisfied
619687
E0281, // type implements trait but other trait is required
620-
E0282, // unable to infer enough type information about
621688
E0283, // cannot resolve type
622689
E0284, // cannot resolve type
623690
E0285, // overflow evaluation builtin bounds

src/librustc/middle/traits/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub fn maybe_report_ambiguity<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
290290
{
291291
span_err!(infcx.tcx.sess, obligation.cause.span, E0282,
292292
"unable to infer enough type information about `{}`; \
293-
type annotations required",
293+
type annotations or generic parameter binding required",
294294
self_ty.user_string(infcx.tcx));
295295
} else {
296296
span_err!(infcx.tcx.sess, obligation.cause.span, E0283,

src/test/compile-fail/issue-12187-1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -14,5 +14,5 @@ fn new<T>() -> &'static T {
1414

1515
fn main() {
1616
let &v = new();
17-
//~^ ERROR type annotations required
17+
//~^ ERROR type annotations or generic parameter binding required
1818
}

src/test/compile-fail/issue-12187-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -14,5 +14,5 @@ fn new<'r, T>() -> &'r T {
1414

1515
fn main() {
1616
let &v = new();
17-
//~^ ERROR type annotations required
17+
//~^ ERROR type annotations or generic parameter binding required
1818
}

src/test/compile-fail/issue-5062.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -9,4 +9,4 @@
99
// except according to those terms.
1010

1111
fn main() { format!("{:?}", None); }
12-
//~^ ERROR type annotations required
12+
//~^ ERROR type annotations or generic parameter binding required

src/test/compile-fail/issue-6458-2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -11,5 +11,5 @@
1111
fn main() {
1212
// Unconstrained type:
1313
format!("{:?}", None);
14-
//~^ ERROR type annotations required
14+
//~^ ERROR type annotations or generic parameter binding required
1515
}

src/test/compile-fail/issue-6458-3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -12,5 +12,5 @@ use std::mem;
1212

1313
fn main() {
1414
mem::transmute(0);
15-
//~^ ERROR type annotations required
15+
//~^ ERROR type annotations or generic parameter binding required
1616
}

src/test/compile-fail/issue-6458-4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -10,7 +10,7 @@
1010

1111
fn foo(b: bool) -> Result<bool,String> {
1212
Err("bar".to_string());
13-
//~^ ERROR type annotations required
13+
//~^ ERROR type annotations or generic parameter binding required
1414
}
1515

1616
fn main() {

src/test/compile-fail/issue-6458.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -16,7 +16,8 @@ pub struct MyState;
1616
pub fn foo<State>(_: TypeWithState<State>) {}
1717

1818
pub fn bar() {
19-
foo(TypeWithState(marker::PhantomData)); //~ ERROR type annotations required
19+
foo(TypeWithState(marker::PhantomData));
20+
//~^ ERROR type annotations or generic parameter binding required
2021
}
2122

2223
fn main() {

src/test/compile-fail/issue-7813.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -10,5 +10,5 @@
1010

1111
fn main() {
1212
let v = &[];
13-
let it = v.iter(); //~ ERROR type annotations required
13+
let it = v.iter(); //~ ERROR type annotations or generic parameter binding required
1414
}

src/test/compile-fail/method-ambig-one-trait-unknown-int-type.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -31,7 +31,8 @@ impl foo for Vec<isize> {
3131

3232
fn m1() {
3333
// we couldn't infer the type of the vector just based on calling foo()...
34-
let mut x = Vec::new(); //~ ERROR type annotations required
34+
let mut x = Vec::new();
35+
//~^ ERROR type annotations or generic parameter binding required
3536
x.foo();
3637
}
3738

src/test/compile-fail/traits-multidispatch-convert-ambig-dest.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -33,7 +33,8 @@ where T : Convert<U>
3333
}
3434

3535
fn a() {
36-
test(22, std::default::Default::default()); //~ ERROR type annotations required
36+
test(22, std::default::Default::default());
37+
//~^ ERROR type annotations or generic parameter binding required
3738
}
3839

3940
fn main() {}

src/test/compile-fail/unconstrained-none.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -11,5 +11,5 @@
1111
// Issue #5062
1212

1313
fn main() {
14-
None; //~ ERROR type annotations required
14+
None; //~ ERROR type annotations or generic parameter binding required
1515
}

src/test/compile-fail/unconstrained-ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -13,5 +13,5 @@ struct S<'a, T:'a> {
1313
}
1414

1515
fn main() {
16-
S { o: &None }; //~ ERROR type annotations required
16+
S { o: &None }; //~ ERROR type annotations or generic parameter binding required
1717
}

src/test/compile-fail/vector-no-ann.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -10,5 +10,6 @@
1010

1111

1212
fn main() {
13-
let _foo = Vec::new(); //~ ERROR type annotations required
13+
let _foo = Vec::new();
14+
//~^ ERROR type annotations or generic parameter binding required
1415
}

0 commit comments

Comments
 (0)