Skip to content

Commit 1f43731

Browse files
committed
Add more catch-related CFG and lifetime tests and fix CFG bug
1 parent fc04eaa commit 1f43731

File tree

5 files changed

+90
-19
lines changed

5 files changed

+90
-19
lines changed

src/librustc/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
8787
stmts_exit = self.stmt(stmt, stmts_exit);
8888
}
8989
let blk_expr_exit = self.opt_expr(&blk.expr, stmts_exit);
90-
self.add_contained_edge(blk_expr_exit, blk_expr_exit);
90+
self.add_contained_edge(blk_expr_exit, expr_exit);
9191

9292
self.breakable_block_scopes.pop();
9393

src/test/compile-fail/catch-bad-lifetime.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,34 @@
1010

1111
#![feature(catch_expr)]
1212

13+
// This test checks that borrows made and returned inside catch blocks are properly constrained
1314
pub fn main() {
14-
let _: Result<(), &str> = do catch {
15-
let my_string = String::from("");
16-
let my_str: &str = &my_string;
17-
Err(my_str)?;
18-
Err("")?;
19-
Ok(())
20-
}; //~ ERROR `my_string` does not live long enough
15+
{
16+
// Test that borrows returned from a catch block must be valid for the lifetime of the
17+
// result variable
18+
let _result: Result<(), &str> = do catch {
19+
let my_string = String::from("");
20+
let my_str: & str = & my_string;
21+
Err(my_str) ?;
22+
Err("") ?;
23+
Ok(())
24+
}; //~ ERROR `my_string` does not live long enough
25+
}
2126

22-
let mut i = 5;
23-
let k = &mut i;
24-
let mut j: Result<(), &mut i32> = do catch {
25-
Err(k)?;
26-
i = 10; //~ ERROR cannot assign to `i` because it is borrowed
27-
Ok(())
28-
};
29-
::std::mem::drop(k); //~ ERROR use of moved value: `k`
30-
i = 40; //~ ERROR cannot assign to `i` because it is borrowed
27+
{
28+
// Test that borrows returned from catch blocks freeze their referent
29+
let mut i = 5;
30+
let k = &mut i;
31+
let mut j: Result<(), &mut i32> = do catch {
32+
Err(k) ?;
33+
i = 10; //~ ERROR cannot assign to `i` because it is borrowed
34+
Ok(())
35+
};
36+
::std::mem::drop(k); //~ ERROR use of moved value: `k`
37+
i = 40; //~ ERROR cannot assign to `i` because it is borrowed
3138

32-
let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic!("") };
33-
*i_ptr = 50;
39+
let i_ptr = if let Err(i_ptr) = j { i_ptr } else { panic ! ("") };
40+
*i_ptr = 50;
41+
}
3442
}
3543

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.
4+
//
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.
10+
11+
#![feature(catch_expr)]
12+
13+
// This test checks that borrows made and returned inside catch blocks are properly constrained
14+
pub fn main() {
15+
{
16+
// Test that a borrow which *might* be returned still freezes its referent
17+
let mut i = 222;
18+
let x: Result<&i32, ()> = do catch {
19+
Err(())?;
20+
Ok(&i)
21+
};
22+
x.ok().cloned();
23+
i = 0; //~ ERROR cannot assign to `i` because it is borrowed
24+
let _ = i;
25+
}
26+
27+
{
28+
let x = String::new();
29+
let _y: Result<(), ()> = do catch {
30+
Err(())?;
31+
::std::mem::drop(x);
32+
Ok(())
33+
};
34+
println!("{}", x); //~ ERROR use of moved value: `x`
35+
}
36+
37+
{
38+
// Test that a borrow which *might* be assigned to an outer variable still freezes
39+
// its referent
40+
let mut i = 222;
41+
let j;
42+
let x: Result<(), ()> = do catch {
43+
Err(())?;
44+
j = &i;
45+
Ok(())
46+
};
47+
i = 0; //~ ERROR cannot assign to `i` because it is borrowed
48+
let _ = i;
49+
}
50+
}
51+

src/test/compile-fail/catch-opt-init.rs

+4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010

1111
#![feature(catch_expr)]
1212

13+
fn use_val<T: Sized>(_x: T) {}
14+
1315
pub fn main() {
1416
let cfg_res;
1517
let _: Result<(), ()> = do catch {
1618
Err(())?;
1719
cfg_res = 5;
20+
Ok::<(), ()>(())?;
21+
use_val(cfg_res);
1822
Ok(())
1923
};
2024
assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable

src/test/run-pass/catch-expr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ pub fn main() {
5858
};
5959
assert_eq!(cfg_init, 5);
6060

61+
let cfg_init_2;
62+
let _res: Result<(), ()> = do catch {
63+
cfg_init_2 = 6;
64+
Err(())?;
65+
Ok(())
66+
};
67+
assert_eq!(cfg_init_2, 6);
68+
6169
let my_string = "test".to_string();
6270
let res: Result<&str, ()> = do catch {
6371
Ok(&my_string)

0 commit comments

Comments
 (0)