Implement ? in catch expressions and add tests

This commit is contained in:
Taylor Cramer 2017-02-28 11:05:03 -08:00
parent 703b246287
commit fc04eaacc5
16 changed files with 571 additions and 273 deletions

View file

@ -0,0 +1,35 @@
// 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)]
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
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;
}

View file

@ -0,0 +1,21 @@
// 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)]
pub fn main() {
let res: Result<i32, i32> = do catch {
Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>` is not satisfied
Ok(5)
};
let res: Result<i32, i32> = do catch {
Ok("") //~ mismatched types
};
}

View file

@ -0,0 +1,22 @@
// 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)]
pub fn main() {
let cfg_res;
let _: Result<(), ()> = do catch {
Err(())?;
cfg_res = 5;
Ok(())
};
assert_eq!(cfg_res, 5); //~ ERROR use of possibly uninitialized variable
}

View file

@ -29,4 +29,38 @@ pub fn main() {
match catch {
_ => {}
};
let catch_err = do catch {
Err(22)?;
Ok(1)
};
assert_eq!(catch_err, Err(22));
let catch_okay: Result<i32, i32> = do catch {
if false { Err(25)?; }
Ok::<(), i32>(())?;
Ok(28)
};
assert_eq!(catch_okay, Ok(28));
let catch_from_loop: Result<i32, i32> = do catch {
for i in 0..10 {
if i < 5 { Ok::<i32, i32>(i)?; } else { Err(i)?; }
}
Ok(22)
};
assert_eq!(catch_from_loop, Err(5));
let cfg_init;
let _res: Result<(), ()> = do catch {
cfg_init = 5;
Ok(())
};
assert_eq!(cfg_init, 5);
let my_string = "test".to_string();
let res: Result<&str, ()> = do catch {
Ok(&my_string)
};
assert_eq!(res, Ok("test"));
}