]> git.lizzy.rs Git - rust.git/blob - tests/ui/nll/ty-outlives/issue-53789-2.rs
Auto merge of #101138 - Rejyr:diagnostic-migration-rustc-lint-pt2, r=davidtwco
[rust.git] / tests / ui / nll / ty-outlives / issue-53789-2.rs
1 // Regression test for #53789.
2 //
3 // check-pass
4
5 use std::cmp::Ord;
6 use std::collections::BTreeMap;
7 use std::ops::Range;
8
9 macro_rules! valuetree {
10     () => {
11         type ValueTree = <Self::Strategy as $crate::Strategy>::Value;
12     };
13 }
14
15 macro_rules! product_unpack {
16     ($factor: pat) => {
17         ($factor,)
18     };
19     ($($factor: pat),*) => {
20         ( $( $factor ),* )
21     };
22     ($($factor: pat),*,) => {
23         ( $( $factor ),* )
24     };
25 }
26
27 macro_rules! product_type {
28     ($factor: ty) => {
29         ($factor,)
30     };
31     ($($factor: ty),*) => {
32         ( $( $factor, )* )
33     };
34     ($($factor: ty),*,) => {
35         ( $( $factor, )* )
36     };
37 }
38
39 macro_rules! default {
40     ($type: ty, $val: expr) => {
41         impl Default for $type {
42             fn default() -> Self {
43                 $val.into()
44             }
45         }
46     };
47 }
48
49 // Pervasive internal sugar
50 macro_rules! mapfn {
51     ($(#[$meta:meta])* [$($vis:tt)*]
52      fn $name:ident[$($gen:tt)*]($parm:ident: $input:ty) -> $output:ty {
53          $($body:tt)*
54      }) => {
55         $(#[$meta])*
56             #[derive(Clone, Copy)]
57         $($vis)* struct $name;
58         impl $($gen)* statics::MapFn<$input> for $name {
59             type Output = $output;
60         }
61     }
62 }
63
64 macro_rules! opaque_strategy_wrapper {
65     ($(#[$smeta:meta])* pub struct $stratname:ident
66      [$($sgen:tt)*][$($swhere:tt)*]
67      ($innerstrat:ty) -> $stratvtty:ty;
68
69      $(#[$vmeta:meta])* pub struct $vtname:ident
70      [$($vgen:tt)*][$($vwhere:tt)*]
71      ($innervt:ty) -> $actualty:ty;
72     ) => {
73         $(#[$smeta])* struct $stratname $($sgen)* (std::marker::PhantomData<(K, V)>)
74             $($swhere)*;
75
76         $(#[$vmeta])* struct $vtname $($vgen)* ($innervt) $($vwhere)*;
77
78         impl $($sgen)* Strategy for $stratname $($sgen)* $($swhere)* {
79             type Value = $stratvtty;
80         }
81
82         impl $($vgen)* ValueTree for $vtname $($vgen)* $($vwhere)* {
83             type Value = $actualty;
84         }
85     }
86 }
87
88 trait ValueTree {
89     type Value;
90 }
91
92 trait Strategy {
93     type Value: ValueTree;
94 }
95
96 #[derive(Clone)]
97 struct VecStrategy<T: Strategy> {
98     element: T,
99     size: Range<usize>,
100 }
101
102 fn vec<T: Strategy>(element: T, size: Range<usize>) -> VecStrategy<T> {
103     VecStrategy { element: element, size: size }
104 }
105
106 type ValueFor<S> = <<S as Strategy>::Value as ValueTree>::Value;
107
108 trait Arbitrary<'a>: Sized {
109     fn arbitrary_with(args: Self::Parameters) -> Self::Strategy;
110
111     type Parameters: Default;
112     type Strategy: Strategy<Value = Self::ValueTree>;
113     type ValueTree: ValueTree<Value = Self>;
114 }
115
116 type StrategyFor<A> = StrategyType<'static, A>;
117 type StrategyType<'a, A> = <A as Arbitrary<'a>>::Strategy;
118
119 //#[derive(Clone, PartialEq, Eq, Hash, Debug, From, Into)]
120 struct SizeBounds(Range<usize>);
121 default!(SizeBounds, 0..100);
122
123 impl From<Range<usize>> for SizeBounds {
124     fn from(high: Range<usize>) -> Self {
125         unimplemented!()
126     }
127 }
128
129 impl From<SizeBounds> for Range<usize> {
130     fn from(high: SizeBounds) -> Self {
131         unimplemented!()
132     }
133 }
134
135 fn any_with<'a, A: Arbitrary<'a>>(args: A::Parameters) -> StrategyType<'a, A> {
136     unimplemented!()
137 }
138
139 impl<K: ValueTree, V: ValueTree> Strategy for (K, V)
140 where
141     <K as ValueTree>::Value: Ord,
142 {
143     type Value = TupleValueTree<(K, V)>;
144 }
145
146 impl<K: ValueTree, V: ValueTree> ValueTree for TupleValueTree<(K, V)>
147 where
148     <K as ValueTree>::Value: Ord,
149 {
150     type Value = BTreeMapValueTree<K, V>;
151 }
152
153 #[derive(Clone)]
154 struct VecValueTree<T: ValueTree> {
155     elements: Vec<T>,
156 }
157
158 #[derive(Clone, Copy)]
159 struct TupleValueTree<T> {
160     tree: T,
161 }
162
163 opaque_strategy_wrapper! {
164     #[derive(Clone)]
165     pub struct BTreeMapStrategy[<K, V>]
166         [where K : Strategy, V : Strategy, ValueFor<K> : Ord](
167             statics::Filter<statics::Map<VecStrategy<(K,V)>,
168             VecToBTreeMap>, MinSize>)
169         -> BTreeMapValueTree<K::Value, V::Value>;
170
171     #[derive(Clone)]
172     pub struct BTreeMapValueTree[<K, V>]
173         [where K : ValueTree, V : ValueTree, K::Value : Ord](
174             statics::Filter<statics::Map<VecValueTree<TupleValueTree<(K, V)>>,
175             VecToBTreeMap>, MinSize>)
176         -> BTreeMap<K::Value, V::Value>;
177 }
178
179 type RangedParams2<A, B> = product_type![SizeBounds, A, B];
180
181 impl<'a, A, B> Arbitrary<'a> for BTreeMap<A, B>
182 where
183     A: Arbitrary<'static> + Ord,
184     B: Arbitrary<'static>,
185     StrategyFor<A>: 'static,
186     StrategyFor<B>: 'static,
187 {
188     valuetree!();
189     type Parameters = RangedParams2<A::Parameters, B::Parameters>;
190     type Strategy = BTreeMapStrategy<A::Strategy, B::Strategy>;
191     fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
192         let product_unpack![range, a, b] = args;
193         btree_map(any_with::<A>(a), any_with::<B>(b), range.into())
194     }
195 }
196
197 #[derive(Clone, Copy)]
198 struct MinSize(usize);
199
200 mapfn! {
201     [] fn VecToBTreeMap[<K : Ord, V>]
202         (vec: Vec<(K, V)>) -> BTreeMap<K, V>
203     {
204         vec.into_iter().collect()
205     }
206 }
207
208 fn btree_map<K: Strategy + 'static, V: Strategy + 'static>(
209     key: K,
210     value: V,
211     size: Range<usize>,
212 ) -> BTreeMapStrategy<K, V>
213 where
214     ValueFor<K>: Ord,
215 {
216     unimplemented!()
217 }
218
219 mod statics {
220     pub(super) trait MapFn<T> {
221         type Output;
222     }
223
224     #[derive(Clone)]
225     pub struct Filter<S, F> {
226         source: S,
227         fun: F,
228     }
229
230     impl<S, F> Filter<S, F> {
231         pub fn new(source: S, whence: String, filter: F) -> Self {
232             unimplemented!()
233         }
234     }
235
236     #[derive(Clone)]
237     pub struct Map<S, F> {
238         source: S,
239         fun: F,
240     }
241
242     impl<S, F> Map<S, F> {
243         pub fn new(source: S, fun: F) -> Self {
244             unimplemented!()
245         }
246     }
247 }
248
249 fn main() {}