]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-type-bounds/trait-alias-impl-trait.rs
Auto merge of #103600 - compiler-errors:early-binder-nits, r=spastorino
[rust.git] / src / test / ui / associated-type-bounds / trait-alias-impl-trait.rs
1 // run-pass
2
3 #![feature(associated_type_bounds)]
4 #![feature(type_alias_impl_trait)]
5
6 use std::ops::Add;
7
8 trait Tr1 {
9     type As1;
10     fn mk(self) -> Self::As1;
11 }
12 trait Tr2<'a> {
13     fn tr2(self) -> &'a Self;
14 }
15
16 fn assert_copy<T: Copy>(x: T) {
17     let _x = x;
18     let _x = x;
19 }
20 fn assert_static<T: 'static>(_: T) {}
21 fn assert_forall_tr2<T: for<'a> Tr2<'a>>(_: T) {}
22
23 struct S1;
24 #[derive(Copy, Clone)]
25 struct S2;
26 impl Tr1 for S1 {
27     type As1 = S2;
28     fn mk(self) -> Self::As1 {
29         S2
30     }
31 }
32
33 type Et1 = impl Tr1<As1: Copy>;
34 fn def_et1() -> Et1 {
35     S1
36 }
37 pub fn use_et1() {
38     assert_copy(def_et1().mk());
39 }
40
41 type Et2 = impl Tr1<As1: 'static>;
42 fn def_et2() -> Et2 {
43     S1
44 }
45 pub fn use_et2() {
46     assert_static(def_et2().mk());
47 }
48
49 type Et3 = impl Tr1<As1: Clone + Iterator<Item: Add<u8, Output: Into<u8>>>>;
50 fn def_et3() -> Et3 {
51     struct A;
52     impl Tr1 for A {
53         type As1 = core::ops::Range<u8>;
54         fn mk(self) -> Self::As1 {
55             0..10
56         }
57     }
58     A
59 }
60 pub fn use_et3() {
61     let _0 = def_et3().mk().clone();
62     let mut s = 0u8;
63     for _1 in _0 {
64         let _2 = _1 + 1u8;
65         s += _2.into();
66     }
67     assert_eq!(s, (0..10).map(|x| x + 1).sum());
68 }
69
70 type Et4 = impl Tr1<As1: for<'a> Tr2<'a>>;
71 fn def_et4() -> Et4 {
72     #[derive(Copy, Clone)]
73     struct A;
74     impl Tr1 for A {
75         type As1 = A;
76         fn mk(self) -> A {
77             A
78         }
79     }
80     impl<'a> Tr2<'a> for A {
81         fn tr2(self) -> &'a Self {
82             &A
83         }
84     }
85     A
86 }
87 pub fn use_et4() {
88     assert_forall_tr2(def_et4().mk());
89 }
90
91 fn main() {
92     let _ = use_et1();
93     let _ = use_et2();
94     let _ = use_et3();
95     let _ = use_et4();
96 }