Skip to content

Commit 3b51579

Browse files
authored
Rollup merge of rust-lang#61220 - imbrem:error_explanations, r=estebank
Added error message for E0284 Work on rust-lang#61137
2 parents 08f7724 + b348012 commit 3b51579

5 files changed

+49
-1
lines changed

src/librustc/error_codes.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,51 @@ fn main() {
12071207
```
12081208
"##,
12091209

1210+
E0284: r##"
1211+
This error occurs when the compiler is unable to unambiguously infer the
1212+
return type of a function or method which is generic on return type, such
1213+
as the `collect` method for `Iterator`s.
1214+
1215+
For example:
1216+
1217+
```compile_fail,E0284
1218+
fn foo() -> Result<bool, ()> {
1219+
let results = [Ok(true), Ok(false), Err(())].iter().cloned();
1220+
let v: Vec<bool> = results.collect()?;
1221+
// Do things with v...
1222+
Ok(true)
1223+
}
1224+
```
1225+
1226+
Here we have an iterator `results` over `Result<bool, ()>`.
1227+
Hence, `results.collect()` can return any type implementing
1228+
`FromIterator<Result<bool, ()>>`. On the other hand, the
1229+
`?` operator can accept any type implementing `Try`.
1230+
1231+
The author of this code probably wants `collect()` to return a
1232+
`Result<Vec<bool>, ()>`, but the compiler can't be sure
1233+
that there isn't another type `T` implementing both `Try` and
1234+
`FromIterator<Result<bool, ()>>` in scope such that
1235+
`T::Ok == Vec<bool>`. Hence, this code is ambiguous and an error
1236+
is returned.
1237+
1238+
To resolve this error, use a concrete type for the intermediate expression:
1239+
1240+
```
1241+
fn foo() -> Result<bool, ()> {
1242+
let results = [Ok(true), Ok(false), Err(())].iter().cloned();
1243+
let v = {
1244+
let temp: Result<Vec<bool>, ()> = results.collect();
1245+
temp?
1246+
};
1247+
// Do things with v...
1248+
Ok(true)
1249+
}
1250+
```
1251+
1252+
Note that the type of `v` can now be inferred from the type of `temp`.
1253+
"##,
1254+
12101255
E0308: r##"
12111256
This error occurs when the compiler was unable to infer the concrete type of a
12121257
variable. It can occur for several cases, the most common of which is a
@@ -2158,7 +2203,6 @@ register_diagnostics! {
21582203
E0278, // requirement is not satisfied
21592204
E0279, // requirement is not satisfied
21602205
E0280, // requirement is not satisfied
2161-
E0284, // cannot resolve type
21622206
// E0285, // overflow evaluation builtin bounds
21632207
// E0296, // replaced with a generic attribute input check
21642208
// E0300, // unexpanded macro

src/test/ui/associated-types/associated-types-overridden-binding.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ LL | trait Foo: Iterator<Item = i32> {}
1212

1313
error: aborting due to previous error
1414

15+
For more information about this error, try `rustc --explain E0284`.

src/test/ui/associated-types/associated-types-unconstrained.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | let x: isize = Foo::bar();
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0284`.

src/test/ui/issues/issue-12028.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | self.input_stream(&mut stream);
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0284`.

src/test/ui/question-mark-type-infer.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | l.iter().map(f).collect()?
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0284`.

0 commit comments

Comments
 (0)