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