From 6c83fe4c24b7c0e6c6ff6916c8b4a9d9973e46a9 Mon Sep 17 00:00:00 2001 From: Jakub Wieczorek Date: Sat, 15 Dec 2012 23:13:55 +0000 Subject: [PATCH] Add more tests --- .../alt-vec-illegal-tail-element-loan.rs | 13 +++++++++++ src/test/compile-fail/alt-vec-tail-move.rs | 8 +++++++ src/test/run-pass/vec-matching-autoslice.rs | 22 +++++++++++++++++++ .../vec-matching-legal-tail-element-borrow.rs | 10 +++++++++ 4 files changed, 53 insertions(+) create mode 100644 src/test/compile-fail/alt-vec-illegal-tail-element-loan.rs create mode 100644 src/test/compile-fail/alt-vec-tail-move.rs create mode 100644 src/test/run-pass/vec-matching-autoslice.rs create mode 100644 src/test/run-pass/vec-matching-legal-tail-element-borrow.rs diff --git a/src/test/compile-fail/alt-vec-illegal-tail-element-loan.rs b/src/test/compile-fail/alt-vec-illegal-tail-element-loan.rs new file mode 100644 index 0000000000000..70c7c33f691f1 --- /dev/null +++ b/src/test/compile-fail/alt-vec-illegal-tail-element-loan.rs @@ -0,0 +1,13 @@ +fn a() -> &int { + let vec = [1, 2, 3, 4]; + let tail = match vec { + [a, ..tail] => &tail[0], //~ ERROR illegal borrow + _ => fail ~"foo" + }; + move tail +} + +fn main() { + let fifth = a(); + io::println(fmt!("%d", *fifth)); +} diff --git a/src/test/compile-fail/alt-vec-tail-move.rs b/src/test/compile-fail/alt-vec-tail-move.rs new file mode 100644 index 0000000000000..fc7d663713195 --- /dev/null +++ b/src/test/compile-fail/alt-vec-tail-move.rs @@ -0,0 +1,8 @@ +fn main() { + let a = [mut 1, 2, 3, 4]; + let _ = match a { + [1, 2, ..move tail] => tail, + _ => core::util::unreachable() + }; + a[0] = 0; //~ ERROR: use of moved variable +} diff --git a/src/test/run-pass/vec-matching-autoslice.rs b/src/test/run-pass/vec-matching-autoslice.rs new file mode 100644 index 0000000000000..badb0245b8174 --- /dev/null +++ b/src/test/run-pass/vec-matching-autoslice.rs @@ -0,0 +1,22 @@ +fn main() { + let x = @[1, 2, 3]; + match x { + [2, .._] => core::util::unreachable(), + [1, ..tail] => { + assert tail == [2, 3]; + } + [_] => core::util::unreachable(), + [] => core::util::unreachable() + } + + let y = (~[(1, true), (2, false)], 0.5); + match y { + ([_, _, _], 0.5) => core::util::unreachable(), + ([(1, a), (b, false), ..tail], _) => { + assert a == true; + assert b == 2; + assert tail.is_empty(); + } + ([..tail], _) => core::util::unreachable() + } +} diff --git a/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs b/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs new file mode 100644 index 0000000000000..ea21408788e88 --- /dev/null +++ b/src/test/run-pass/vec-matching-legal-tail-element-borrow.rs @@ -0,0 +1,10 @@ +fn main() { + let x = &[1, 2, 3, 4, 5]; + if !x.is_empty() { + let el = match x { + [1, ..ref tail] => &tail[0], + _ => core::util::unreachable() + }; + io::println(fmt!("%d", *el)); + } +}