]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/unboxed-closure-illegal-move.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / compile-fail / unboxed-closure-illegal-move.rs
CommitLineData
1a4d82fc
JJ
1// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
1a4d82fc
JJ
11#![feature(unboxed_closures)]
12
13// Tests that we can't move out of an unboxed closure environment
14// if the upvar is captured by ref or the closure takes self by
15// reference.
16
85aaf69f
SL
17fn to_fn<A,F:Fn<A>>(f: F) -> F { f }
18fn to_fn_mut<A,F:FnMut<A>>(f: F) -> F { f }
19fn to_fn_once<A,F:FnOnce<A>>(f: F) -> F { f }
20
c34b1796
AL
21// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
22
1a4d82fc
JJ
23fn main() {
24 // By-ref cases
25 {
c34b1796 26 let x = Box::new(0);
85aaf69f 27 let f = to_fn(|| drop(x)); //~ ERROR cannot move
1a4d82fc
JJ
28 }
29 {
c34b1796 30 let x = Box::new(0);
85aaf69f 31 let f = to_fn_mut(|| drop(x)); //~ ERROR cannot move
1a4d82fc
JJ
32 }
33 {
c34b1796 34 let x = Box::new(0);
85aaf69f 35 let f = to_fn_once(|| drop(x)); // OK -- FnOnce
1a4d82fc
JJ
36 }
37 // By-value cases
38 {
c34b1796 39 let x = Box::new(0);
85aaf69f 40 let f = to_fn(move || drop(x)); //~ ERROR cannot move
1a4d82fc
JJ
41 }
42 {
c34b1796 43 let x = Box::new(0);
85aaf69f 44 let f = to_fn_mut(move || drop(x)); //~ ERROR cannot move
1a4d82fc
JJ
45 }
46 {
c34b1796 47 let x = Box::new(0);
85aaf69f 48 let f = to_fn_once(move || drop(x)); // this one is ok
1a4d82fc
JJ
49 }
50}