diff options
| author | scalexm <martin.alex32@hotmail.fr> | 2017-09-13 22:40:48 +0200 |
|---|---|---|
| committer | scalexm <martin.alex32@hotmail.fr> | 2017-09-20 20:43:41 +0200 |
| commit | f7964aebe516bc7e4f06f7128ebb39ad5576a1a7 (patch) | |
| tree | fb903110af5ee4e9a3437f70b2cd9b7a7466ef6d /src/test | |
| parent | 01c65cb15ac57bfdc91613a4f6032ecc76c402a3 (diff) | |
Implement `Copy`/`Clone` for closures
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/feature-gate-clone-closures.rs | 21 | ||||
| -rw-r--r-- | src/test/compile-fail/feature-gate-copy-closures.rs | 19 | ||||
| -rw-r--r-- | src/test/compile-fail/not-clone-closure.rs | 24 | ||||
| -rw-r--r-- | src/test/compile-fail/not-copy-closure.rs | 24 | ||||
| -rw-r--r-- | src/test/run-pass/clone-closure.rs | 29 | ||||
| -rw-r--r-- | src/test/run-pass/copy-closure.rs | 28 |
6 files changed, 145 insertions, 0 deletions
diff --git a/src/test/compile-fail/feature-gate-clone-closures.rs b/src/test/compile-fail/feature-gate-clone-closures.rs new file mode 100644 index 00000000000..a15153ea7bf --- /dev/null +++ b/src/test/compile-fail/feature-gate-clone-closures.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. + +#[derive(Clone)] +struct S(i32); + +fn main() { + let a = S(5); + let hello = move || { + println!("Hello {}", a.0); + }; + + let hello = hello.clone(); //~ ERROR no method named `clone` found for type +} diff --git a/src/test/compile-fail/feature-gate-copy-closures.rs b/src/test/compile-fail/feature-gate-copy-closures.rs new file mode 100644 index 00000000000..b11b09eb9fd --- /dev/null +++ b/src/test/compile-fail/feature-gate-copy-closures.rs @@ -0,0 +1,19 @@ +// 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. + +fn main() { + let a = 5; + let hello = || { + println!("Hello {}", a); + }; + + let b = hello; + let c = hello; //~ ERROR use of moved value: `hello` [E0382] +} diff --git a/src/test/compile-fail/not-clone-closure.rs b/src/test/compile-fail/not-clone-closure.rs new file mode 100644 index 00000000000..2a30dc4fdd4 --- /dev/null +++ b/src/test/compile-fail/not-clone-closure.rs @@ -0,0 +1,24 @@ +// 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. + +// Check that closures do not implement `Clone` if their environment is not `Clone`. + +#![feature(clone_closures)] + +struct S(i32); + +fn main() { + let a = S(5); + let hello = move || { + println!("Hello {}", a.0); + }; + + let hello = hello.clone(); //~ ERROR the trait bound `S: std::clone::Clone` is not satisfied +} diff --git a/src/test/compile-fail/not-copy-closure.rs b/src/test/compile-fail/not-copy-closure.rs new file mode 100644 index 00000000000..271e6d5fc90 --- /dev/null +++ b/src/test/compile-fail/not-copy-closure.rs @@ -0,0 +1,24 @@ +// 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. + +// Check that closures do not implement `Copy` if their environment is not `Copy`. + +#![feature(copy_closures)] +#![feature(clone_closures)] + +fn main() { + let mut a = 5; + let hello = || { + a += 1; + }; + + let b = hello; + let c = hello; //~ ERROR use of moved value: `hello` [E0382] +} diff --git a/src/test/run-pass/clone-closure.rs b/src/test/run-pass/clone-closure.rs new file mode 100644 index 00000000000..7f554c77fc4 --- /dev/null +++ b/src/test/run-pass/clone-closure.rs @@ -0,0 +1,29 @@ +// 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. + +// Check that closures implement `Clone`. + +#![feature(clone_closures)] + +#[derive(Clone)] +struct S(i32); + +fn main() { + let mut a = S(5); + let mut hello = move || { + a.0 += 1; + println!("Hello {}", a.0); + a.0 + }; + + let mut hello2 = hello.clone(); + assert_eq!(6, hello2()); + assert_eq!(6, hello()); +} diff --git a/src/test/run-pass/copy-closure.rs b/src/test/run-pass/copy-closure.rs new file mode 100644 index 00000000000..309c83ebd99 --- /dev/null +++ b/src/test/run-pass/copy-closure.rs @@ -0,0 +1,28 @@ +// 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. + +// Check that closures implement `Copy`. + +#![feature(copy_closures)] +#![feature(clone_closures)] + +fn call<T, F: FnOnce() -> T>(f: F) -> T { f() } + +fn main() { + let a = 5; + let hello = || { + println!("Hello {}", a); + a + }; + + assert_eq!(5, call(hello.clone())); + assert_eq!(5, call(hello)); + assert_eq!(5, call(hello)); +} |
