diff options
| author | Jakub Wieczorek <jakub@jakub.cc> | 2014-06-14 15:55:55 +0200 |
|---|---|---|
| committer | Jakub Wieczorek <jakub@jakub.cc> | 2014-07-22 23:45:49 +0200 |
| commit | 59edfdd2ab3bad73086940afe1a57fa4706a4d2f (patch) | |
| tree | 70e5775d5e4a189dfdff9efa4c835c705d97f32a /src/test | |
| parent | 31c908b7be51899b16935dbd453718bdcbee431a (diff) | |
| download | rust-59edfdd2ab3bad73086940afe1a57fa4706a4d2f.tar.gz rust-59edfdd2ab3bad73086940afe1a57fa4706a4d2f.zip | |
Add Drop support for enums
Fixes #13041.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/kindck-destructor-owned.rs | 2 | ||||
| -rw-r--r-- | src/test/run-pass/drop-trait-enum.rs | 94 | ||||
| -rw-r--r-- | src/test/run-pass/drop-uninhabited-enum.rs | 19 | ||||
| -rw-r--r-- | src/test/run-pass/issue-10802.rs | 16 | ||||
| -rw-r--r-- | src/test/run-pass/issue-6892.rs | 21 |
5 files changed, 146 insertions, 6 deletions
diff --git a/src/test/compile-fail/kindck-destructor-owned.rs b/src/test/compile-fail/kindck-destructor-owned.rs index fa5f1531637..44fe2607fcc 100644 --- a/src/test/compile-fail/kindck-destructor-owned.rs +++ b/src/test/compile-fail/kindck-destructor-owned.rs @@ -17,7 +17,7 @@ struct Foo { } impl Drop for Foo { - //~^ ERROR cannot implement a destructor on a structure that does not satisfy Send +//~^ ERROR cannot implement a destructor on a structure or enumeration that does not satisfy Send fn drop(&mut self) { } } diff --git a/src/test/run-pass/drop-trait-enum.rs b/src/test/run-pass/drop-trait-enum.rs new file mode 100644 index 00000000000..977eaa13fc1 --- /dev/null +++ b/src/test/run-pass/drop-trait-enum.rs @@ -0,0 +1,94 @@ +// Copyright 2014 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(struct_variant)] + +use std::task; + +#[deriving(PartialEq, Show)] +enum Message { + Dropped, + DestructorRan +} + +struct SendOnDrop { + sender: Sender<Message> +} + +impl Drop for SendOnDrop { + fn drop(&mut self) { + self.sender.send(Dropped); + } +} + +enum Foo { + SimpleVariant(Sender<Message>), + NestedVariant(Box<uint>, SendOnDrop, Sender<Message>), + FailingVariant { on_drop: SendOnDrop } +} + +impl Drop for Foo { + fn drop(&mut self) { + match self { + &SimpleVariant(ref mut sender) => { + sender.send(DestructorRan); + } + &NestedVariant(_, _, ref mut sender) => { + sender.send(DestructorRan); + } + &FailingVariant { .. } => { + fail!("Failed"); + } + } + } +} + +pub fn main() { + let (sender, receiver) = channel(); + { + let v = SimpleVariant(sender); + } + assert_eq!(receiver.recv(), DestructorRan); + assert_eq!(receiver.recv_opt().ok(), None); + + let (sender, receiver) = channel(); + { + let v = NestedVariant(box 42u, SendOnDrop { sender: sender.clone() }, sender); + } + assert_eq!(receiver.recv(), DestructorRan); + assert_eq!(receiver.recv(), Dropped); + assert_eq!(receiver.recv_opt().ok(), None); + + let (sender, receiver) = channel(); + task::spawn(proc() { + let v = FailingVariant { on_drop: SendOnDrop { sender: sender } }; + }); + assert_eq!(receiver.recv(), Dropped); + assert_eq!(receiver.recv_opt().ok(), None); + + let (sender, receiver) = channel(); + { + task::spawn(proc() { + let mut v = NestedVariant(box 42u, SendOnDrop { + sender: sender.clone() + }, sender.clone()); + v = NestedVariant(box 42u, SendOnDrop { sender: sender.clone() }, sender.clone()); + v = SimpleVariant(sender.clone()); + v = FailingVariant { on_drop: SendOnDrop { sender: sender } }; + }); + } + assert_eq!(receiver.recv(), DestructorRan); + assert_eq!(receiver.recv(), Dropped); + assert_eq!(receiver.recv(), DestructorRan); + assert_eq!(receiver.recv(), Dropped); + assert_eq!(receiver.recv(), DestructorRan); + assert_eq!(receiver.recv(), Dropped); + assert_eq!(receiver.recv_opt().ok(), None); +} diff --git a/src/test/run-pass/drop-uninhabited-enum.rs b/src/test/run-pass/drop-uninhabited-enum.rs new file mode 100644 index 00000000000..f8c54fbab8a --- /dev/null +++ b/src/test/run-pass/drop-uninhabited-enum.rs @@ -0,0 +1,19 @@ +// Copyright 2014 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. + +enum Foo { } + +impl Drop for Foo { + fn drop(&mut self) { } +} + +fn foo(x: Foo) { } + +fn main() { } diff --git a/src/test/run-pass/issue-10802.rs b/src/test/run-pass/issue-10802.rs index 6c4f0cc7f5f..4fda506ae64 100644 --- a/src/test/run-pass/issue-10802.rs +++ b/src/test/run-pass/issue-10802.rs @@ -8,8 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. - struct DroppableStruct; +enum DroppableEnum { + DroppableVariant1, DroppableVariant2 +} static mut DROPPED: bool = false; @@ -18,9 +20,15 @@ impl Drop for DroppableStruct { unsafe { DROPPED = true; } } } +impl Drop for DroppableEnum { + fn drop(&mut self) { + unsafe { DROPPED = true; } + } +} trait MyTrait { } impl MyTrait for Box<DroppableStruct> {} +impl MyTrait for Box<DroppableEnum> {} struct Whatever { w: Box<MyTrait> } impl Whatever { @@ -35,4 +43,10 @@ fn main() { let _a = Whatever::new(box f as Box<MyTrait>); } assert!(unsafe { DROPPED }); + unsafe { DROPPED = false; } + { + let f = box DroppableVariant1; + let _a = Whatever::new(box f as Box<MyTrait>); + } + assert!(unsafe { DROPPED }); } diff --git a/src/test/run-pass/issue-6892.rs b/src/test/run-pass/issue-6892.rs index 891892ff9cb..462a78a5368 100644 --- a/src/test/run-pass/issue-6892.rs +++ b/src/test/run-pass/issue-6892.rs @@ -14,6 +14,7 @@ struct Foo; struct Bar { x: int } struct Baz(int); +enum FooBar { _Foo(Foo), _Bar(uint) } static mut NUM_DROPS: uint = 0; @@ -32,6 +33,11 @@ impl Drop for Baz { unsafe { NUM_DROPS += 1; } } } +impl Drop for FooBar { + fn drop(&mut self) { + unsafe { NUM_DROPS += 1; } + } +} fn main() { assert_eq!(unsafe { NUM_DROPS }, 0); @@ -41,12 +47,19 @@ fn main() { assert_eq!(unsafe { NUM_DROPS }, 2); { let _x = Baz(21); } assert_eq!(unsafe { NUM_DROPS }, 3); + { let _x = _Foo(Foo); } + assert_eq!(unsafe { NUM_DROPS }, 5); + { let _x = _Bar(42u); } + assert_eq!(unsafe { NUM_DROPS }, 6); - assert_eq!(unsafe { NUM_DROPS }, 3); { let _ = Foo; } - assert_eq!(unsafe { NUM_DROPS }, 4); + assert_eq!(unsafe { NUM_DROPS }, 7); { let _ = Bar { x: 21 }; } - assert_eq!(unsafe { NUM_DROPS }, 5); + assert_eq!(unsafe { NUM_DROPS }, 8); { let _ = Baz(21); } - assert_eq!(unsafe { NUM_DROPS }, 6); + assert_eq!(unsafe { NUM_DROPS }, 9); + { let _ = _Foo(Foo); } + assert_eq!(unsafe { NUM_DROPS }, 11); + { let _ = _Bar(42u); } + assert_eq!(unsafe { NUM_DROPS }, 12); } |
