提交 74d24ad6 编写于 作者: S Steve Klabnik

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?
......@@ -419,6 +419,74 @@ enum Method { GET, POST }
be taken.
"##,
E0282: r##"
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:
```
let x = "hello".chars().rev().collect();
```
In this case, 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<_>>();
```
Apart from a method or function with a generic type parameter, this error can
occur when a type parameter of a struct or trait cannot be inferred. In that
case it is not always possible to use a type annotation, because all candidates
have the same return type. For instance:
```
struct Foo<T> {
// Some fields omitted.
}
impl<T> Foo<T> {
fn bar() -> i32 {
0
}
fn baz() {
let number = Foo::bar();
}
}
```
This will fail because the compiler does not know which instance of `Foo` to
call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
"##,
E0296: r##"
This error indicates that the given recursion limit could not be parsed. Ensure
that the value provided is a positive integer between quotes, like so:
......@@ -617,7 +685,6 @@ struct Foo<T: 'static> {
E0279, // requirement is not satisfied
E0280, // requirement is not satisfied
E0281, // type implements trait but other trait is required
E0282, // unable to infer enough type information about
E0283, // cannot resolve type
E0284, // cannot resolve type
E0285, // overflow evaluation builtin bounds
......
......@@ -290,7 +290,7 @@ pub fn maybe_report_ambiguity<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
{
span_err!(infcx.tcx.sess, obligation.cause.span, E0282,
"unable to infer enough type information about `{}`; \
type annotations required",
type annotations or generic parameter binding required",
self_ty.user_string(infcx.tcx));
} else {
span_err!(infcx.tcx.sess, obligation.cause.span, E0283,
......
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -14,5 +14,5 @@ fn new<T>() -> &'static T {
fn main() {
let &v = new();
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
}
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -14,5 +14,5 @@ fn new<'r, T>() -> &'r T {
fn main() {
let &v = new();
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
}
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -9,4 +9,4 @@
// except according to those terms.
fn main() { format!("{:?}", None); }
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -11,5 +11,5 @@
fn main() {
// Unconstrained type:
format!("{:?}", None);
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
}
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -12,5 +12,5 @@
fn main() {
mem::transmute(0);
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
}
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -10,7 +10,7 @@
fn foo(b: bool) -> Result<bool,String> {
Err("bar".to_string());
//~^ ERROR type annotations required
//~^ ERROR type annotations or generic parameter binding required
}
fn main() {
......
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -16,7 +16,8 @@
pub fn foo<State>(_: TypeWithState<State>) {}
pub fn bar() {
foo(TypeWithState(marker::PhantomData)); //~ ERROR type annotations required
foo(TypeWithState(marker::PhantomData));
//~^ ERROR type annotations or generic parameter binding required
}
fn main() {
......
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -10,5 +10,5 @@
fn main() {
let v = &[];
let it = v.iter(); //~ ERROR type annotations required
let it = v.iter(); //~ ERROR type annotations or generic parameter binding required
}
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -31,7 +31,8 @@ fn foo(&self) -> isize {2}
fn m1() {
// we couldn't infer the type of the vector just based on calling foo()...
let mut x = Vec::new(); //~ ERROR type annotations required
let mut x = Vec::new();
//~^ ERROR type annotations or generic parameter binding required
x.foo();
}
......
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -33,7 +33,8 @@ fn test<T,U>(_: T, _: U)
}
fn a() {
test(22, std::default::Default::default()); //~ ERROR type annotations required
test(22, std::default::Default::default());
//~^ ERROR type annotations or generic parameter binding required
}
fn main() {}
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// Copyright 2013-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -11,5 +11,5 @@
// Issue #5062
fn main() {
None; //~ ERROR type annotations required
None; //~ ERROR type annotations or generic parameter binding required
}
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -13,5 +13,5 @@ struct S<'a, T:'a> {
}
fn main() {
S { o: &None }; //~ ERROR type annotations required
S { o: &None }; //~ ERROR type annotations or generic parameter binding required
}
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
......@@ -10,5 +10,6 @@
fn main() {
let _foo = Vec::new(); //~ ERROR type annotations required
let _foo = Vec::new();
//~^ ERROR type annotations or generic parameter binding required
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册