]> git.lizzy.rs Git - rust.git/blob - tests/ui/functions-closures/implied-bounds-closure-arg-outlives.rs
Rollup merge of #106644 - alexcrichton:update-wasi-toolchain, r=cuviper
[rust.git] / tests / ui / functions-closures / implied-bounds-closure-arg-outlives.rs
1 // run-pass
2 // Test that we are able to handle the relationships between free
3 // regions bound in a closure callback.
4
5 #[derive(Copy, Clone)]
6 struct MyCx<'short, 'long: 'short> {
7     short: &'short u32,
8     long: &'long u32,
9 }
10
11 impl<'short, 'long> MyCx<'short, 'long> {
12     fn short(self) -> &'short u32 { self.short }
13     fn long(self) -> &'long u32 { self.long }
14     fn set_short(&mut self, v: &'short u32) { self.short = v; }
15 }
16
17 fn with<F, R>(op: F) -> R
18 where
19     F: for<'short, 'long> FnOnce(MyCx<'short, 'long>) -> R,
20 {
21     op(MyCx {
22         short: &22,
23         long: &22,
24     })
25 }
26
27 fn main() {
28     with(|mut cx| {
29         // For this to type-check, we need to be able to deduce that
30         // the lifetime of `l` can be `'short`, even though it has
31         // input from `'long`.
32         let l = if true { cx.long() } else { cx.short() };
33         cx.set_short(l);
34     });
35 }