]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-type-bounds/dyn-impl-trait-type.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / associated-type-bounds / dyn-impl-trait-type.rs
1 // run-pass
2
3 #![feature(associated_type_bounds)]
4
5 use std::ops::Add;
6
7 trait Tr1 { type As1; fn mk(&self) -> Self::As1; }
8 trait Tr2<'a> { fn tr2(self) -> &'a Self; }
9
10 fn assert_copy<T: Copy>(x: T) { let _x = x; let _x = x; }
11 fn assert_static<T: 'static>(_: T) {}
12 fn assert_forall_tr2<T: for<'a> Tr2<'a>>(_: T) {}
13
14 struct S1;
15 #[derive(Copy, Clone)]
16 struct S2;
17 impl Tr1 for S1 { type As1 = S2; fn mk(&self) -> Self::As1 { S2 } }
18
19 type Et1 = Box<dyn Tr1<As1: Copy>>;
20 fn def_et1() -> Et1 { Box::new(S1) }
21 pub fn use_et1() { assert_copy(def_et1().mk()); }
22
23 type Et2 = Box<dyn Tr1<As1: 'static>>;
24 fn def_et2() -> Et2 { Box::new(S1) }
25 pub fn use_et2() { assert_static(def_et2().mk()); }
26
27 type Et3 = Box<dyn Tr1<As1: Clone + Iterator<Item: Add<u8, Output: Into<u8>>>>>;
28 fn def_et3() -> Et3 {
29     struct A;
30     impl Tr1 for A {
31         type As1 = core::ops::Range<u8>;
32         fn mk(&self) -> Self::As1 { 0..10 }
33     };
34     Box::new(A)
35 }
36 pub fn use_et3() {
37     let _0 = def_et3().mk().clone();
38     let mut s = 0u8;
39     for _1 in _0 {
40         let _2 = _1 + 1u8;
41         s += _2.into();
42     }
43     assert_eq!(s, (0..10).map(|x| x + 1).sum());
44 }
45
46 type Et4 = Box<dyn Tr1<As1: for<'a> Tr2<'a>>>;
47 fn def_et4() -> Et4 {
48     #[derive(Copy, Clone)]
49     struct A;
50     impl Tr1 for A {
51         type As1 = A;
52         fn mk(&self) -> A { A }
53     }
54     impl<'a> Tr2<'a> for A {
55         fn tr2(self) -> &'a Self { &A }
56     }
57     Box::new(A)
58 }
59 pub fn use_et4() { assert_forall_tr2(def_et4().mk()); }
60
61 fn main() {
62     let _ = use_et1();
63     let _ = use_et2();
64     let _ = use_et3();
65     let _ = use_et4();
66 }