]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #61220 - imbrem:error_explanations, r=estebank
authorMazdak Farrokhzad <twingoow@gmail.com>
Tue, 28 May 2019 09:48:55 +0000 (11:48 +0200)
committerGitHub <noreply@github.com>
Tue, 28 May 2019 09:48:55 +0000 (11:48 +0200)
Added error message for E0284

Work on #61137

src/librustc/error_codes.rs
src/test/ui/associated-types/associated-types-overridden-binding.stderr
src/test/ui/associated-types/associated-types-unconstrained.stderr
src/test/ui/issues/issue-12028.stderr
src/test/ui/question-mark-type-infer.stderr

index a1bfd417566ad7473f93f8c2696af3674503aa6f..6243e911bd5fdf92bc5b834a3ad51e7ccb0f7fd7 100644 (file)
@@ -1207,6 +1207,51 @@ fn main() {
 ```
 "##,
 
+E0284: r##"
+This error occurs when the compiler is unable to unambiguously infer the
+return type of a function or method which is generic on return type, such
+as the `collect` method for `Iterator`s.
+
+For example:
+
+```compile_fail,E0284
+fn foo() -> Result<bool, ()> {
+    let results = [Ok(true), Ok(false), Err(())].iter().cloned();
+    let v: Vec<bool> = results.collect()?;
+    // Do things with v...
+    Ok(true)
+}
+```
+
+Here we have an iterator `results` over `Result<bool, ()>`.
+Hence, `results.collect()` can return any type implementing
+`FromIterator<Result<bool, ()>>`. On the other hand, the
+`?` operator can accept any type implementing `Try`.
+
+The author of this code probably wants `collect()` to return a
+`Result<Vec<bool>, ()>`, but the compiler can't be sure
+that there isn't another type `T` implementing both `Try` and
+`FromIterator<Result<bool, ()>>` in scope such that
+`T::Ok == Vec<bool>`. Hence, this code is ambiguous and an error
+is returned.
+
+To resolve this error, use a concrete type for the intermediate expression:
+
+```
+fn foo() -> Result<bool, ()> {
+    let results = [Ok(true), Ok(false), Err(())].iter().cloned();
+    let v = {
+        let temp: Result<Vec<bool>, ()> = results.collect();
+        temp?
+    };
+    // Do things with v...
+    Ok(true)
+}
+```
+
+Note that the type of `v` can now be inferred from the type of `temp`.
+"##,
+
 E0308: r##"
 This error occurs when the compiler was unable to infer the concrete type of a
 variable. It can occur for several cases, the most common of which is a
@@ -2158,7 +2203,6 @@ trait Foo { }
     E0278, // requirement is not satisfied
     E0279, // requirement is not satisfied
     E0280, // requirement is not satisfied
-    E0284, // cannot resolve type
 //  E0285, // overflow evaluation builtin bounds
 //  E0296, // replaced with a generic attribute input check
 //  E0300, // unexpanded macro
index fced38caaba913630bb38b3af2bad12a09309b85..a26ee23894f6dcd04db477b1901adadf00331715 100644 (file)
@@ -12,3 +12,4 @@ LL | trait Foo: Iterator<Item = i32> {}
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0284`.
index 26e5a6a503c6f99ebc61516e52ae9c11966380bb..da14a69ae306a67baece987e98b21226ce43256f 100644 (file)
@@ -6,3 +6,4 @@ LL |     let x: isize = Foo::bar();
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0284`.
index b9e2e80492b370142b2837b1e41d88bcbc0f5916..64694c7a8d0b66dbb9c917c8e667500af41a1d70 100644 (file)
@@ -6,3 +6,4 @@ LL |         self.input_stream(&mut stream);
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0284`.
index 2a1bdf57a88cbe3c68b307abb05ce7430a399662..f62a540572c934661246e79c7050cc3885c76afe 100644 (file)
@@ -6,3 +6,4 @@ LL |     l.iter().map(f).collect()?
 
 error: aborting due to previous error
 
+For more information about this error, try `rustc --explain E0284`.