Skip to content

Commit ddc2333

Browse files
Rollup merge of rust-lang#42787 - zackmdavis:explain_E0562, r=GuillaumeGomez
add extended information for E0562; impl Trait can only be a return type r? @GuillaumeGomez
2 parents d22eb08 + 43b9cb3 commit ddc2333

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/librustc_typeck/diagnostics.rs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3989,6 +3989,46 @@ let s = Simba { mother: 1, father: 0 }; // ok!
39893989
```
39903990
"##,
39913991

3992+
E0562: r##"
3993+
Abstract return types (written `impl Trait` for some trait `Trait`) are only
3994+
allowed as function return types.
3995+
3996+
Erroneous code example:
3997+
3998+
```compile_fail,E0562
3999+
#![feature(conservative_impl_trait)]
4000+
4001+
fn main() {
4002+
let count_to_ten: impl Iterator<Item=usize> = 0..10;
4003+
// error: `impl Trait` not allowed outside of function and inherent method
4004+
// return types
4005+
for i in count_to_ten {
4006+
println!("{}", i);
4007+
}
4008+
}
4009+
```
4010+
4011+
Make sure `impl Trait` only appears in return-type position.
4012+
4013+
```
4014+
#![feature(conservative_impl_trait)]
4015+
4016+
fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
4017+
0..n
4018+
}
4019+
4020+
fn main() {
4021+
for i in count_to_n(10) { // ok!
4022+
println!("{}", i);
4023+
}
4024+
}
4025+
```
4026+
4027+
See [RFC 1522] for more details.
4028+
4029+
[RFC 1522]: https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md
4030+
"##,
4031+
39924032
E0570: r##"
39934033
The requested ABI is unsupported by the current target.
39944034
@@ -4591,8 +4631,6 @@ register_diagnostics! {
45914631
E0436, // functional record update requires a struct
45924632
E0521, // redundant default implementations of trait
45934633
E0533, // `{}` does not name a unit variant, unit struct or a constant
4594-
E0562, // `impl Trait` not allowed outside of function
4595-
// and inherent method return types
45964634
E0563, // cannot determine a type for this `impl Trait`: {}
45974635
E0564, // only named lifetimes are allowed in `impl Trait`,
45984636
// but `{}` was found in the type `{}`

0 commit comments

Comments
 (0)