]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/unboxed-closures-recursive-fn-using-fn-mut.rs
Auto merge of #22517 - brson:relnotes, r=Gankro
[rust.git] / src / test / compile-fail / unboxed-closures-recursive-fn-using-fn-mut.rs
1 // Copyright 2015 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 #![feature(core,unboxed_closures)]
12
13 use std::marker::CovariantType;
14
15 // A erroneous variant of `run-pass/unboxed_closures-infer-recursive-fn.rs`
16 // where we attempt to perform mutation in the recursive function. This fails to compile
17 // because it winds up requiring `FnMut` which enforces linearity.
18
19 struct YCombinator<F,A,R> {
20     func: F,
21     marker: CovariantType<(A,R)>,
22 }
23
24 impl<F,A,R> YCombinator<F,A,R> {
25     fn new(f: F) -> YCombinator<F,A,R> {
26         YCombinator { func: f, marker: CovariantType }
27     }
28 }
29
30 impl<A,R,F : FnMut(&mut FnMut(A) -> R, A) -> R> FnMut<(A,)> for YCombinator<F,A,R> {
31     type Output = R;
32
33     extern "rust-call" fn call_mut(&mut self, (arg,): (A,)) -> R {
34         (self.func)(self, arg)
35             //~^ ERROR cannot borrow `*self` as mutable more than once at a time
36     }
37 }
38
39 fn main() {
40     let mut counter = 0;
41     let factorial = |recur: &mut FnMut(u32) -> u32, arg: u32| -> u32 {
42         counter += 1;
43         if arg == 0 {1} else {arg * recur(arg-1)}
44     };
45     let mut factorial: YCombinator<_,u32,u32> = YCombinator::new(factorial);
46     let mut r = factorial(10);
47     assert_eq!(3628800, r);
48 }