]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_error_codes/src/error_codes/E0792.md
New upstream version 1.68.2+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0792.md
CommitLineData
9c376795
FG
1A type alias impl trait can only have its hidden type assigned
2when used fully generically (and within their defining scope).
3This means
4
5```compile_fail,E0792
6#![feature(type_alias_impl_trait)]
7
8type Foo<T> = impl std::fmt::Debug;
9
10fn foo() -> Foo<u32> {
11 5u32
12}
13```
14
15is not accepted. If it were accepted, one could create unsound situations like
16
17```compile_fail,E0792
18#![feature(type_alias_impl_trait)]
19
20type Foo<T> = impl Default;
21
22fn foo() -> Foo<u32> {
23 5u32
24}
25
26fn main() {
27 let x = Foo::<&'static mut String>::default();
28}
29```
30
31
32Instead you need to make the function generic:
33
34```
35#![feature(type_alias_impl_trait)]
36
37type Foo<T> = impl std::fmt::Debug;
38
39fn foo<U>() -> Foo<U> {
40 5u32
41}
42```
43
44This means that no matter the generic parameter to `foo`,
45the hidden type will always be `u32`.
46If you want to link the generic parameter to the hidden type,
47you can do that, too:
48
49
50```
51#![feature(type_alias_impl_trait)]
52
53use std::fmt::Debug;
54
55type Foo<T: Debug> = impl Debug;
56
57fn foo<U: Debug>() -> Foo<U> {
58 Vec::<U>::new()
59}
60```