]> git.lizzy.rs Git - rust.git/blob - tests/ui/async-await/multiple-lifetimes/ret-ref.rs
Rollup merge of #107114 - Erk-:add-absolute-note-to-path-join, r=m-ou-se
[rust.git] / tests / ui / async-await / multiple-lifetimes / ret-ref.rs
1 // edition:2018
2
3 // Test that we get the expected borrow check errors when an async
4 // function (which takes multiple lifetimes) only returns data from
5 // one of them.
6
7 async fn multiple_named_lifetimes<'a, 'b>(a: &'a u8, _: &'b u8) -> &'a u8 {
8     a
9 }
10
11 // Both are borrowed whilst the future is live.
12 async fn future_live() {
13     let mut a = 22;
14     let mut b = 44;
15     let future = multiple_named_lifetimes(&a, &b);
16     a += 1; //~ ERROR cannot assign
17     b += 1; //~ ERROR cannot assign
18     let p = future.await;
19     drop(p);
20 }
21
22 // Just the return value is live after future is awaited.
23 async fn just_return_live() {
24     let mut a = 22;
25     let mut b = 44;
26     let future = multiple_named_lifetimes(&a, &b);
27     let p = future.await;
28     a += 1; //~ ERROR cannot assign
29     b += 1;
30     drop(p);
31 }
32
33 // Once `p` is dead, both `a` and `b` are unborrowed.
34 async fn after_both_dead() {
35     let mut a = 22;
36     let mut b = 44;
37     let future = multiple_named_lifetimes(&a, &b);
38     let p = future.await;
39     drop(p);
40     a += 1;
41     b += 1;
42 }
43
44 fn main() { }