1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // compile-flags: --edition 2018
13 #![feature(try_blocks)]
16 fn do_something_with<T>(_x: T) {}
18 // This test checks that borrows made and returned inside try blocks are properly constrained
21 // Test that borrows returned from a try block must be valid for the lifetime of the
23 let result: Result<(), &str> = try {
24 let my_string = String::from("");
25 let my_str: & str = & my_string;
26 //~^ ERROR `my_string` does not live long enough
30 do_something_with(result);
34 // Test that borrows returned from try blocks freeze their referent
37 let mut j: Result<(), &mut i32> = try {
39 i = 10; //~ ERROR cannot assign to `i` because it is borrowed
41 ::std::mem::drop(k); //~ ERROR use of moved value: `k`
42 i = 40; //~ ERROR cannot assign to `i` because it is borrowed
44 let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") };