about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorTaylor Cramer <cramertaylorj@gmail.com>2017-02-28 11:05:03 -0800
committerTaylor Cramer <cramertaylorj@gmail.com>2017-03-17 21:01:04 -0700
commitfc04eaacc5bd5760e98cd3aa390dcc3ae795d12f (patch)
treebc13f89329235a88e2041780079cec6841999e83 /src/test
parent703b246287fa9e7ef39bd66f978f6dc6fa119c15 (diff)
downloadrust-fc04eaacc5bd5760e98cd3aa390dcc3ae795d12f.tar.gz
rust-fc04eaacc5bd5760e98cd3aa390dcc3ae795d12f.zip
Implement ? in catch expressions and add tests
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/catch-bad-lifetime.rs35
-rw-r--r--src/test/compile-fail/catch-bad-type.rs21
-rw-r--r--src/test/compile-fail/catch-opt-init.rs22
-rw-r--r--src/test/run-pass/catch-expr.rs34
4 files changed, 112 insertions, 0 deletions
diff --git a/src/test/compile-fail/catch-bad-lifetime.rs b/src/test/compile-fail/catch-bad-lifetime.rs
new file mode 100644
index 00000000000..d8de419df6d
--- /dev/null
+++ b/src/test/compile-fail/catch-bad-lifetime.rs
@@ -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;
+}
+
diff --git a/src/test/compile-fail/catch-bad-type.rs b/src/test/compile-fail/catch-bad-type.rs
new file mode 100644
index 00000000000..cff9f508275
--- /dev/null
+++ b/src/test/compile-fail/catch-bad-type.rs
@@ -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
+    };
+}
diff --git a/src/test/compile-fail/catch-opt-init.rs b/src/test/compile-fail/catch-opt-init.rs
new file mode 100644
index 00000000000..c5d116c82dd
--- /dev/null
+++ b/src/test/compile-fail/catch-opt-init.rs
@@ -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
+}
+
diff --git a/src/test/run-pass/catch-expr.rs b/src/test/run-pass/catch-expr.rs
index a9b28a534a3..f2852bac27a 100644
--- a/src/test/run-pass/catch-expr.rs
+++ b/src/test/run-pass/catch-expr.rs
@@ -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"));
 }