]> git.lizzy.rs Git - rust.git/blob - src/test/ui/try-block/try-block-maybe-bad-lifetime.rs
Rollup merge of #95376 - WaffleLapkin:drain_keep_rest, r=dtolnay
[rust.git] / src / test / ui / try-block / try-block-maybe-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 a borrow which *might* be returned still freezes its referent
12         let mut i = 222;
13         let x: Result<&i32, ()> = try {
14             Err(())?;
15             &i
16         };
17         i = 0; //~ ERROR cannot assign to `i` because it is borrowed
18         let _ = i;
19         do_something_with(x);
20     }
21
22     {
23         let x = String::new();
24         let _y: Result<(), ()> = try {
25             Err(())?;
26             ::std::mem::drop(x);
27         };
28         println!("{}", x); //~ ERROR borrow of moved value: `x`
29     }
30
31     {
32         // Test that a borrow which *might* be assigned to an outer variable still freezes
33         // its referent
34         let mut i = 222;
35         let mut j = &-1;
36         let _x: Result<(), ()> = try {
37             Err(())?;
38             j = &i;
39         };
40         i = 0; //~ ERROR cannot assign to `i` because it is borrowed
41         let _ = i;
42         do_something_with(j);
43     }
44 }