Add more catch-related CFG and lifetime tests and fix CFG bug

This commit is contained in:
Taylor Cramer 2017-03-14 16:48:01 -07:00
parent fc04eaacc5
commit 1f43731772
5 changed files with 90 additions and 19 deletions

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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;
}
}

View File

@ -10,11 +10,15 @@
#![feature(catch_expr)]
fn use_val<T: Sized>(_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

View File

@ -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)