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