]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-type-bounds/rpit.rs
Merge commit '3e7c6dec244539970b593824334876f8b6ed0b18' into clippyup
[rust.git] / src / test / ui / associated-type-bounds / rpit.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 fn def_et1() -> impl Tr1<As1: Copy> { S1 }
20 pub fn use_et1() { assert_copy(def_et1().mk()); }
21
22 fn def_et2() -> impl Tr1<As1: 'static> { S1 }
23 pub fn use_et2() { assert_static(def_et2().mk()); }
24
25 fn def_et3() -> impl Tr1<As1: Clone + Iterator<Item: Add<u8, Output: Into<u8>>>> {
26     struct A;
27     impl Tr1 for A {
28         type As1 = core::ops::Range<u8>;
29         fn mk(self) -> Self::As1 { 0..10 }
30     };
31     A
32 }
33
34 pub fn use_et3() {
35     let _0 = def_et3().mk().clone();
36     let mut s = 0u8;
37     for _1 in _0 {
38         let _2 = _1 + 1u8;
39         s += _2.into();
40     }
41     assert_eq!(s, (0..10).map(|x| x + 1).sum());
42 }
43
44 fn def_et4() -> impl Tr1<As1: for<'a> Tr2<'a>> {
45     #[derive(Copy, Clone)]
46     struct A;
47     impl Tr1 for A {
48         type As1 = A;
49         fn mk(self) -> A { A }
50     }
51     impl<'a> Tr2<'a> for A {
52         fn tr2(self) -> &'a Self { &A }
53     }
54     A
55 }
56
57 pub fn use_et4() { assert_forall_tr2(def_et4().mk()); }
58
59 fn main() {
60     let _ = use_et1();
61     let _ = use_et2();
62     let _ = use_et3();
63     let _ = use_et4();
64 }