]> git.lizzy.rs Git - rust.git/blob - tests/run-pass/dyn-lcsit.rs
add interesting unsizing test
[rust.git] / tests / run-pass / dyn-lcsit.rs
1 // Taken from the rustc test suite; this triggers an interesting case in unsizing.
2
3 #![allow(non_upper_case_globals, incomplete_features)]
4 #![feature(associated_type_bounds)]
5 #![feature(impl_trait_in_bindings)]
6
7 use std::ops::Add;
8
9 trait Tr1 { type As1; fn mk(&self) -> Self::As1; }
10 trait Tr2<'a> { fn tr2(self) -> &'a Self; }
11
12 fn assert_copy<T: Copy>(x: T) { let _x = x; let _x = x; }
13 fn assert_static<T: 'static>(_: T) {}
14 fn assert_forall_tr2<T: for<'a> Tr2<'a>>(_: T) {}
15
16 #[derive(Copy, Clone)]
17 struct S1;
18 #[derive(Copy, Clone)]
19 struct S2;
20 impl Tr1 for S1 { type As1 = S2; fn mk(&self) -> Self::As1 { S2 } }
21
22 const cdef_et1: &dyn Tr1<As1: Copy> = &S1;
23 const sdef_et1: &dyn Tr1<As1: Copy> = &S1;
24 pub fn use_et1() { assert_copy(cdef_et1.mk()); assert_copy(sdef_et1.mk()); }
25
26 const cdef_et2: &(dyn Tr1<As1: 'static> + Sync) = &S1;
27 static sdef_et2: &(dyn Tr1<As1: 'static> + Sync) = &S1;
28 pub fn use_et2() { assert_static(cdef_et2.mk()); assert_static(sdef_et2.mk()); }
29
30 const cdef_et3: &dyn Tr1<As1: Clone + Iterator<Item: Add<u8, Output: Into<u8>>>> = {
31     struct A;
32     impl Tr1 for A {
33         type As1 = core::ops::Range<u8>;
34         fn mk(&self) -> Self::As1 { 0..10 }
35     };
36     &A
37 };
38 pub fn use_et3() {
39     let _0 = cdef_et3.mk().clone();
40     let mut s = 0u8;
41     for _1 in _0 {
42         let _2 = _1 + 1u8;
43         s += _2.into();
44     }
45     assert_eq!(s, (0..10).map(|x| x + 1).sum());
46 }
47
48 const cdef_et4: &(dyn Tr1<As1: for<'a> Tr2<'a>> + Sync) = {
49     #[derive(Copy, Clone)]
50     struct A;
51     impl Tr1 for A {
52         type As1 = A;
53         fn mk(&self) -> A { A }
54     }
55     impl<'a> Tr2<'a> for A {
56         fn tr2(self) -> &'a Self { &A }
57     }
58     &A
59 };
60 static sdef_et4: &(dyn Tr1<As1: for<'a> Tr2<'a>> + Sync) = cdef_et4;
61 pub fn use_et4() { assert_forall_tr2(cdef_et4.mk()); assert_forall_tr2(sdef_et4.mk()); }
62
63 fn main() {
64     let _ = use_et1();
65     let _ = use_et2();
66     let _ = use_et3();
67     let _ = use_et4();
68 }