diff options
Diffstat (limited to 'tests/ui/expr')
| -rw-r--r-- | tests/ui/expr/block-fn.rs | 9 | ||||
| -rw-r--r-- | tests/ui/expr/block-generic.rs | 27 | ||||
| -rw-r--r-- | tests/ui/expr/block.rs | 18 | ||||
| -rw-r--r-- | tests/ui/expr/copy.rs | 18 | ||||
| -rw-r--r-- | tests/ui/expr/if-generic.rs | 29 | ||||
| -rw-r--r-- | tests/ui/expr/if-panic-all.rs | 11 | ||||
| -rw-r--r-- | tests/ui/expr/scope.rs | 7 |
7 files changed, 119 insertions, 0 deletions
diff --git a/tests/ui/expr/block-fn.rs b/tests/ui/expr/block-fn.rs new file mode 100644 index 00000000000..1d3689eb4f3 --- /dev/null +++ b/tests/ui/expr/block-fn.rs @@ -0,0 +1,9 @@ +//@ run-pass + +fn test_fn() { + fn ten() -> isize { return 10; } + let rs = ten; + assert_eq!(rs(), 10); +} + +pub fn main() { test_fn(); } diff --git a/tests/ui/expr/block-generic.rs b/tests/ui/expr/block-generic.rs new file mode 100644 index 00000000000..b36bda917d6 --- /dev/null +++ b/tests/ui/expr/block-generic.rs @@ -0,0 +1,27 @@ +//@ run-pass +#![allow(unused_braces)] + +fn test_generic<T: Clone, F>(expected: T, eq: F) where F: FnOnce(T, T) -> bool { + let actual: T = { expected.clone() }; + assert!(eq(expected, actual)); +} + +fn test_bool() { + fn compare_bool(b1: bool, b2: bool) -> bool { return b1 == b2; } + test_generic::<bool, _>(true, compare_bool); +} + +#[derive(Clone)] +struct Pair { + a: isize, + b: isize, +} + +fn test_rec() { + fn compare_rec(t1: Pair, t2: Pair) -> bool { + t1.a == t2.a && t1.b == t2.b + } + test_generic::<Pair, _>(Pair {a: 1, b: 2}, compare_rec); +} + +pub fn main() { test_bool(); test_rec(); } diff --git a/tests/ui/expr/block.rs b/tests/ui/expr/block.rs new file mode 100644 index 00000000000..bf626c9ead3 --- /dev/null +++ b/tests/ui/expr/block.rs @@ -0,0 +1,18 @@ +//@ run-pass +#![allow(unused_braces)] +#![allow(dead_code)] + +// Tests for standalone blocks as expressions + +fn test_basic() { let rs: bool = { true }; assert!((rs)); } + +struct RS { v1: isize, v2: isize } + +fn test_rec() { let rs = { RS {v1: 10, v2: 20} }; assert_eq!(rs.v2, 20); } + +fn test_filled_with_stuff() { + let rs = { let mut a = 0; while a < 10 { a += 1; } a }; + assert_eq!(rs, 10); +} + +pub fn main() { test_basic(); test_rec(); test_filled_with_stuff(); } diff --git a/tests/ui/expr/copy.rs b/tests/ui/expr/copy.rs new file mode 100644 index 00000000000..cfe47ff6d93 --- /dev/null +++ b/tests/ui/expr/copy.rs @@ -0,0 +1,18 @@ +//@ run-pass + +fn f(arg: &mut A) { + arg.a = 100; +} + +#[derive(Copy, Clone)] +struct A { a: isize } + +pub fn main() { + let mut x = A {a: 10}; + f(&mut x); + assert_eq!(x.a, 100); + x.a = 20; + let mut y = x; + f(&mut y); + assert_eq!(x.a, 20); +} diff --git a/tests/ui/expr/if-generic.rs b/tests/ui/expr/if-generic.rs new file mode 100644 index 00000000000..ed99ee63a51 --- /dev/null +++ b/tests/ui/expr/if-generic.rs @@ -0,0 +1,29 @@ +//@ run-pass + +fn test_generic<T, F>(expected: T, not_expected: T, eq: F) where + T: Clone, + F: FnOnce(T, T) -> bool, +{ + let actual: T = if true { expected.clone() } else { not_expected }; + assert!(eq(expected, actual)); +} + +fn test_bool() { + fn compare_bool(b1: bool, b2: bool) -> bool { return b1 == b2; } + test_generic::<bool, _>(true, false, compare_bool); +} + +#[derive(Clone)] +struct Pair { + a: isize, + b: isize, +} + +fn test_rec() { + fn compare_rec(t1: Pair, t2: Pair) -> bool { + t1.a == t2.a && t1.b == t2.b + } + test_generic::<Pair, _>(Pair{a: 1, b: 2}, Pair{a: 2, b: 3}, compare_rec); +} + +pub fn main() { test_bool(); test_rec(); } diff --git a/tests/ui/expr/if-panic-all.rs b/tests/ui/expr/if-panic-all.rs new file mode 100644 index 00000000000..2ba2a36d165 --- /dev/null +++ b/tests/ui/expr/if-panic-all.rs @@ -0,0 +1,11 @@ +//@ run-pass +// When all branches of an if expression result in panic, the entire if +// expression results in panic. + +pub fn main() { + let _x = if true { + 10 + } else { + if true { panic!() } else { panic!() } + }; +} diff --git a/tests/ui/expr/scope.rs b/tests/ui/expr/scope.rs new file mode 100644 index 00000000000..57321ce2aa0 --- /dev/null +++ b/tests/ui/expr/scope.rs @@ -0,0 +1,7 @@ +//@ run-pass +// Regression test for issue #762 + +//@ pretty-expanded FIXME #23616 + +pub fn f() { } +pub fn main() { return ::f(); } |
