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