]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-type-bounds/union-bounds.rs
Auto merge of #103894 - mati865:gnullvm-libunwind-changes, r=thomcc
[rust.git] / src / test / ui / associated-type-bounds / union-bounds.rs
1 // run-pass
2
3 #![feature(associated_type_bounds)]
4
5 #![allow(unused_assignments)]
6
7 trait Tr1: Copy { type As1: Copy; }
8 trait Tr2: Copy { type As2: Copy; }
9 trait Tr3: Copy { type As3: Copy; }
10 trait Tr4<'a>: Copy { type As4: Copy; }
11 trait Tr5: Copy { type As5: Copy; }
12
13 impl Tr1 for &str { type As1 = bool; }
14 impl Tr2 for bool { type As2 = u8; }
15 impl Tr3 for u8 { type As3 = fn() -> u8; }
16 impl Tr1 for () { type As1 = (usize,); }
17 impl<'a> Tr4<'a> for (usize,) { type As4 = u8; }
18 impl Tr5 for bool { type As5 = u16; }
19
20 union Un1<T: Tr1<As1: Tr2>> {
21     outest: T,
22     outer: T::As1,
23     inner: <T::As1 as Tr2>::As2,
24 }
25
26 union Un2<T: Tr1<As1: Tr2<As2: Tr3>>> {
27     outest: T,
28     outer: T::As1,
29     inner: <T::As1 as Tr2>::As2,
30 }
31
32 union Un3<T: Tr1<As1: 'static>> {
33     outest: T,
34     outer: &'static T::As1,
35 }
36
37 union Un4<'x1, 'x2, T: Tr1<As1: for<'l> Tr4<'l>>> {
38     f1: &'x1 <T::As1 as Tr4<'x1>>::As4,
39     f2: &'x2 <T::As1 as Tr4<'x2>>::As4,
40 }
41
42 union _Un5<'x1, 'x2, T: Tr1<As1: for<'l> Tr4<'l, As4: Copy>>> {
43     f1: &'x1 <T::As1 as Tr4<'x1>>::As4,
44     f2: &'x2 <T::As1 as Tr4<'x2>>::As4,
45 }
46
47 union Un6<T>
48 where
49     T: Tr1<As1: Tr2 + 'static + Tr5>,
50 {
51     f0: T,
52     f1: <T::As1 as Tr2>::As2,
53     f2: &'static T::As1,
54     f3: <T::As1 as Tr5>::As5,
55 }
56
57 union _Un7<'a, 'b, T> // `<T::As1 as Tr2>::As2: 'a` is implied.
58 where
59     T: Tr1<As1: Tr2>,
60 {
61     f0: &'a T,
62     f1: &'b <T::As1 as Tr2>::As2,
63 }
64
65 unsafe fn _use_un7<'a, 'b, T>(x: _Un7<'a, 'b, T>)
66 where
67     T: Tr1,
68     T::As1: Tr2,
69 {
70     let _: &'a T = &x.f0;
71 }
72
73 #[derive(Copy, Clone)]
74 union UnSelf<T> where Self: Tr1<As1: Tr2>, T: Copy {
75     f0: T,
76     f1: <Self as Tr1>::As1,
77     f2: <<Self as Tr1>::As1 as Tr2>::As2,
78 }
79
80 impl Tr1 for UnSelf<&'static str> { type As1 = bool; }
81
82 fn main() {
83     let mut un1 = Un1 { outest: "foo" };
84     un1 = Un1 { outer: true };
85     assert_eq!(unsafe { un1.outer }, true);
86     un1 = Un1 { inner: 42u8 };
87     assert_eq!(unsafe { un1.inner }, 42u8);
88
89     let mut un2 = Un2 { outest: "bar" };
90     assert_eq!(unsafe { un2.outest }, "bar");
91     un2 = Un2 { outer: true };
92     assert_eq!(unsafe { un2.outer }, true);
93     un2 = Un2 { inner: 42u8 };
94     assert_eq!(unsafe { un2.inner }, 42u8);
95
96     let mut un3 = Un3 { outest: "baz" };
97     assert_eq!(unsafe { un3.outest }, "baz");
98     un3 = Un3 { outer: &true };
99     assert_eq!(unsafe { *un3.outer }, true);
100
101     let f1 = (1,);
102     let f2 = (2,);
103     let mut un4 = Un4::<()> { f1: &f1.0 };
104     assert_eq!(1, unsafe { *un4.f1 });
105     un4 = Un4 { f2: &f2.0 };
106     assert_eq!(2, unsafe { *un4.f2 });
107
108     let mut un6 = Un6 { f0: "bar" };
109     assert_eq!(unsafe { un6.f0 }, "bar");
110     un6 = Un6 { f1: 24u8 };
111     assert_eq!(unsafe { un6.f1 }, 24u8);
112     un6 = Un6 { f2: &true };
113     assert_eq!(unsafe { un6.f2 }, &true);
114     un6 = Un6 { f3: 12u16 };
115     assert_eq!(unsafe { un6.f3 }, 12u16);
116
117     let mut unself = UnSelf::<_> { f0: "selfish" };
118     assert_eq!(unsafe { unself.f0 }, "selfish");
119     unself = UnSelf { f1: true };
120     assert_eq!(unsafe { unself.f1 }, true);
121     unself = UnSelf { f2: 24u8 };
122     assert_eq!(unsafe { unself.f2 }, 24u8);
123 }