Skip to content

Commit 43b9cb3

Browse files
committed
add extended information for E0562; impl Trait can only be a return type
1 parent 29bce6e commit 43b9cb3

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
@@ -3938,6 +3938,46 @@ let s = Simba { mother: 1, father: 0 }; // ok!
39383938
```
39393939
"##,
39403940

3941+
E0562: r##"
3942+
Abstract return types (written `impl Trait` for some trait `Trait`) are only
3943+
allowed as function return types.
3944+
3945+
Erroneous code example:
3946+
3947+
```compile_fail,E0562
3948+
#![feature(conservative_impl_trait)]
3949+
3950+
fn main() {
3951+
let count_to_ten: impl Iterator<Item=usize> = 0..10;
3952+
// error: `impl Trait` not allowed outside of function and inherent method
3953+
// return types
3954+
for i in count_to_ten {
3955+
println!("{}", i);
3956+
}
3957+
}
3958+
```
3959+
3960+
Make sure `impl Trait` only appears in return-type position.
3961+
3962+
```
3963+
#![feature(conservative_impl_trait)]
3964+
3965+
fn count_to_n(n: usize) -> impl Iterator<Item=usize> {
3966+
0..n
3967+
}
3968+
3969+
fn main() {
3970+
for i in count_to_n(10) { // ok!
3971+
println!("{}", i);
3972+
}
3973+
}
3974+
```
3975+
3976+
See [RFC 1522] for more details.
3977+
3978+
[RFC 1522]: https://github.com/rust-lang/rfcs/blob/master/text/1522-conservative-impl-trait.md
3979+
"##,
3980+
39413981
E0570: r##"
39423982
The requested ABI is unsupported by the current target.
39433983
@@ -4287,8 +4327,6 @@ register_diagnostics! {
42874327
E0436, // functional record update requires a struct
42884328
E0521, // redundant default implementations of trait
42894329
E0533, // `{}` does not name a unit variant, unit struct or a constant
4290-
E0562, // `impl Trait` not allowed outside of function
4291-
// and inherent method return types
42924330
E0563, // cannot determine a type for this `impl Trait`: {}
42934331
E0564, // only named lifetimes are allowed in `impl Trait`,
42944332
// but `{}` was found in the type `{}`

0 commit comments

Comments
 (0)