diff --git a/src/librustc/cfg/construct.rs b/src/librustc/cfg/construct.rs index 777408457d1f..189a7344c313 100644 --- a/src/librustc/cfg/construct.rs +++ b/src/librustc/cfg/construct.rs @@ -87,7 +87,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> { stmts_exit = self.stmt(stmt, stmts_exit); } let blk_expr_exit = self.opt_expr(&blk.expr, stmts_exit); - self.add_contained_edge(blk_expr_exit, blk_expr_exit); + self.add_contained_edge(blk_expr_exit, expr_exit); self.breakable_block_scopes.pop(); diff --git a/src/test/compile-fail/catch-bad-lifetime.rs b/src/test/compile-fail/catch-bad-lifetime.rs index d8de419df6d0..57242dad6e31 100644 --- a/src/test/compile-fail/catch-bad-lifetime.rs +++ b/src/test/compile-fail/catch-bad-lifetime.rs @@ -10,26 +10,34 @@ #![feature(catch_expr)] +// This test checks that borrows made and returned inside catch blocks are properly constrained pub fn main() { - let _: Result<(), &str> = do catch { - let my_string = String::from(""); - let my_str: &str = &my_string; - Err(my_str)?; - Err("")?; - Ok(()) - }; //~ ERROR `my_string` does not live long enough + { + // Test that borrows returned from a catch block must be valid for the lifetime of the + // result variable + let _result: Result<(), &str> = do catch { + let my_string = String::from(""); + let my_str: & str = & my_string; + Err(my_str) ?; + Err("") ?; + Ok(()) + }; //~ ERROR `my_string` does not live long enough + } - let mut i = 5; - let k = &mut i; - let mut j: Result<(), &mut i32> = do catch { - Err(k)?; - i = 10; //~ ERROR cannot assign to `i` because it is borrowed - Ok(()) - }; - ::std::mem::drop(k); //~ ERROR use of moved value: `k` - i = 40; //~ ERROR cannot assign to `i` because it is borrowed + { + // Test that borrows returned from catch blocks freeze their referent + let mut i = 5; + let k = &mut i; + let mut j: Result<(), &mut i32> = do catch { + Err(k) ?; + i = 10; //~ ERROR cannot assign to `i` because it is borrowed + Ok(()) + }; + ::std::mem::drop(k); //~ ERROR use of moved value: `k` + i = 40; //~ ERROR cannot assign to `i` because it is borrowed - let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic!("") }; - *i_ptr = 50; + let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") }; + *i_ptr = 50; + } } diff --git a/src/test/compile-fail/catch-maybe-bad-lifetime.rs b/src/test/compile-fail/catch-maybe-bad-lifetime.rs new file mode 100644 index 000000000000..b783a3dd7860 --- /dev/null +++ b/src/test/compile-fail/catch-maybe-bad-lifetime.rs @@ -0,0 +1,51 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(catch_expr)] + +// This test checks that borrows made and returned inside catch blocks are properly constrained +pub fn main() { + { + // Test that a borrow which *might* be returned still freezes its referent + let mut i = 222; + let x: Result<&i32, ()> = do catch { + Err(())?; + Ok(&i) + }; + x.ok().cloned(); + i = 0; //~ ERROR cannot assign to `i` because it is borrowed + let _ = i; + } + + { + let x = String::new(); + let _y: Result<(), ()> = do catch { + Err(())?; + ::std::mem::drop(x); + Ok(()) + }; + println!("{}", x); //~ ERROR use of moved value: `x` + } + + { + // Test that a borrow which *might* be assigned to an outer variable still freezes + // its referent + let mut i = 222; + let j; + let x: Result<(), ()> = do catch { + Err(())?; + j = &i; + Ok(()) + }; + i = 0; //~ ERROR cannot assign to `i` because it is borrowed + let _ = i; + } +} + diff --git a/src/test/compile-fail/catch-opt-init.rs b/src/test/compile-fail/catch-opt-init.rs index c5d116c82ddf..48284b4cb90b 100644 --- a/src/test/compile-fail/catch-opt-init.rs +++ b/src/test/compile-fail/catch-opt-init.rs @@ -10,11 +10,15 @@ #![feature(catch_expr)] +fn use_val(_x: T) {} + pub fn main() { let cfg_res; let _: Result<(), ()> = do catch { Err(())?; cfg_res = 5; + Ok::<(), ()>(())?; + use_val(cfg_res); Ok(()) }; assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable diff --git a/src/test/run-pass/catch-expr.rs b/src/test/run-pass/catch-expr.rs index f2852bac27ae..5a757161a78a 100644 --- a/src/test/run-pass/catch-expr.rs +++ b/src/test/run-pass/catch-expr.rs @@ -58,6 +58,14 @@ pub fn main() { }; assert_eq!(cfg_init, 5); + let cfg_init_2; + let _res: Result<(), ()> = do catch { + cfg_init_2 = 6; + Err(())?; + Ok(()) + }; + assert_eq!(cfg_init_2, 6); + let my_string = "test".to_string(); let res: Result<&str, ()> = do catch { Ok(&my_string)