]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #24966 - ruud-v-a:explain, r=pnkfelix
authorSteve Klabnik <steve@steveklabnik.com>
Thu, 7 May 2015 10:21:01 +0000 (12:21 +0200)
committerSteve Klabnik <steve@steveklabnik.com>
Thu, 7 May 2015 10:21:01 +0000 (12:21 +0200)
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?

1  2 
src/librustc/diagnostics.rs
src/librustc/middle/traits/error_reporting.rs

index ff2e1135dea07245db47ab0d6d61eeac9a45dc3a,39ebcda4778c7a0616133bf52d0e8c9415b67e81..88112b4b90cbe690a03e6480987cdad769c11981
@@@ -414,11 -363,79 +414,79 @@@ of a closure you should instead use a r
  "##,
  
  E0268: r##"
 -This error indicates the use of loop keyword (break or continue) outside of a
 -loop. Without a loop to break out of or continue in, no sensible action can be
 -taken.
 +This error indicates the use of a loop keyword (`break` or `continue`) outside
 +of a loop. Without a loop to break out of or continue in, no sensible action can
 +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: