From e5fb58e6c09202df314d3ef6ccb797f2cc8401e2 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Wed, 22 Aug 2012 19:02:08 -0400 Subject: [PATCH] Add compile-fail tests for unsound moving out of enums (#2329) --- ...ove-neither-can-live-while-the-other-survives-1.rs | 9 +++++++++ ...ove-neither-can-live-while-the-other-survives-2.rs | 9 +++++++++ ...ove-neither-can-live-while-the-other-survives-3.rs | 11 +++++++++++ src/test/compile-fail/bind-by-move-no-guards.rs | 10 ++++++++++ src/test/compile-fail/bind-by-move-no-lvalues-1.rs | 9 +++++++++ src/test/compile-fail/bind-by-move-no-lvalues-2.rs | 10 ++++++++++ src/test/compile-fail/bind-by-move-no-sub-bindings.rs | 9 +++++++++ 7 files changed, 67 insertions(+) create mode 100644 src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-1.rs create mode 100644 src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-2.rs create mode 100644 src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-3.rs create mode 100644 src/test/compile-fail/bind-by-move-no-guards.rs create mode 100644 src/test/compile-fail/bind-by-move-no-lvalues-1.rs create mode 100644 src/test/compile-fail/bind-by-move-no-lvalues-2.rs create mode 100644 src/test/compile-fail/bind-by-move-no-sub-bindings.rs diff --git a/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-1.rs b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-1.rs new file mode 100644 index 000000000000..0ee3cac5180f --- /dev/null +++ b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-1.rs @@ -0,0 +1,9 @@ +struct X { x: (); drop { error!("destructor runs"); } } + +fn main() { + let x = some(X { x: () }); + match move x { + some(ref _y @ move _z) => { }, //~ ERROR cannot bind by-move and by-ref in the same pattern + none => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-2.rs b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-2.rs new file mode 100644 index 000000000000..9752ae1b3576 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-2.rs @@ -0,0 +1,9 @@ +struct X { x: (); drop { error!("destructor runs"); } } + +fn main() { + let x = some((X { x: () }, X { x: () })); + match move x { + some((ref _y, move _z)) => { }, //~ ERROR cannot bind by-move and by-ref in the same pattern + none => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-3.rs b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-3.rs new file mode 100644 index 000000000000..399e8702b907 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-neither-can-live-while-the-other-survives-3.rs @@ -0,0 +1,11 @@ +struct X { x: (); drop { error!("destructor runs"); } } + +enum double_option { some2(T,U), none2 } + +fn main() { + let x = some2(X { x: () }, X { x: () }); + match move x { + some2(ref _y, move _z) => { }, //~ ERROR cannot bind by-move and by-ref in the same pattern + none2 => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-no-guards.rs b/src/test/compile-fail/bind-by-move-no-guards.rs new file mode 100644 index 000000000000..e3fb330990e9 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-no-guards.rs @@ -0,0 +1,10 @@ +fn main() { + let (c,p) = pipes::stream(); + let x = some(p); + c.send(false); + match move x { + some(move z) if z.recv() => { fail }, //~ ERROR cannot bind by-move into a pattern guard + some(move z) => { assert !z.recv(); }, + none => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-no-lvalues-1.rs b/src/test/compile-fail/bind-by-move-no-lvalues-1.rs new file mode 100644 index 000000000000..bd7fd843ed56 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-no-lvalues-1.rs @@ -0,0 +1,9 @@ +struct X { x: (); drop { error!("destructor runs"); } } + +fn main() { + let x = some(X { x: () }); + match x { + some(move _z) => { }, //~ ERROR cannot bind by-move when matching an lvalue + none => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-no-lvalues-2.rs b/src/test/compile-fail/bind-by-move-no-lvalues-2.rs new file mode 100644 index 000000000000..ecd684719fe4 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-no-lvalues-2.rs @@ -0,0 +1,10 @@ +struct X { x: (); drop { error!("destructor runs"); } } +struct Y { y: option; } + +fn main() { + let x = Y { y: some(X { x: () }) }; + match x.y { + some(move _z) => { }, //~ ERROR cannot bind by-move when matching an lvalue + none => fail + } +} diff --git a/src/test/compile-fail/bind-by-move-no-sub-bindings.rs b/src/test/compile-fail/bind-by-move-no-sub-bindings.rs new file mode 100644 index 000000000000..88c995874aa8 --- /dev/null +++ b/src/test/compile-fail/bind-by-move-no-sub-bindings.rs @@ -0,0 +1,9 @@ +struct X { x: (); drop { error!("destructor runs"); } } + +fn main() { + let x = some(X { x: () }); + match move x { + some(move _y @ ref _z) => { }, //~ ERROR cannot bind by-move with sub-bindings + none => fail + } +}