]> git.lizzy.rs Git - rust.git/blob - src/test/ui/try-block/try-block-bad-lifetime.rs
Auto merge of #56079 - mark-i-m:patch-1, r=nikomatsakis
[rust.git] / src / test / ui / try-block / try-block-bad-lifetime.rs
1 // compile-flags: --edition 2018
2
3 #![feature(try_blocks)]
4
5 #![inline(never)]
6 fn do_something_with<T>(_x: T) {}
7
8 // This test checks that borrows made and returned inside try blocks are properly constrained
9 pub fn main() {
10     {
11         // Test that borrows returned from a try block must be valid for the lifetime of the
12         // result variable
13         let result: Result<(), &str> = try {
14             let my_string = String::from("");
15             let my_str: & str = & my_string;
16             //~^ ERROR `my_string` does not live long enough
17             Err(my_str) ?;
18             Err("") ?;
19         };
20         do_something_with(result);
21     }
22
23     {
24         // Test that borrows returned from try blocks freeze their referent
25         let mut i = 5;
26         let k = &mut i;
27         let mut j: Result<(), &mut i32> = try {
28             Err(k) ?;
29             i = 10; //~ ERROR cannot assign to `i` because it is borrowed
30         };
31         ::std::mem::drop(k); //~ ERROR use of moved value: `k`
32         i = 40; //~ ERROR cannot assign to `i` because it is borrowed
33
34         let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") };
35         *i_ptr = 50;
36     }
37 }
38