]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/moves-based-on-type-no-recursive-stack-closure.rs
Honor hidden doc attribute of derivable trait methods
[rust.git] / src / test / compile-fail / moves-based-on-type-no-recursive-stack-closure.rs
1 // Copyright 2013 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
11 // Tests correct kind-checking of the reason stack closures without the :Copy
12 // bound must be noncopyable. For details see
13 // http://smallcultfollowing.com/babysteps/blog/2013/04/30/the-case-of-the-recurring-closure/
14
15 struct R<'a> {
16     // This struct is needed to create the
17     // otherwise infinite type of a fn that
18     // accepts itself as argument:
19     c: |&R, bool|: 'a
20 }
21
22 fn innocent_looking_victim() {
23     let mut x = Some("hello".to_owned());
24     conspirator(|f, writer| {
25         if writer {
26             x = None;
27         } else {
28             match x {
29                 Some(ref msg) => {
30                     (f.c)(f, true);
31                     println!("{:?}", msg);
32                 },
33                 None => fail!("oops"),
34             }
35         }
36     })
37 }
38
39 fn conspirator(f: |&R, bool|) {
40     let r = R {c: f};
41     f(&r, false) //~ ERROR use of moved value
42 }
43
44 fn main() { innocent_looking_victim() }