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