diff --git a/src/test/compile-fail/unboxed-closure-region.rs b/src/test/compile-fail/unboxed-closure-region.rs new file mode 100644 index 00000000000..2a71aeaca5f --- /dev/null +++ b/src/test/compile-fail/unboxed-closure-region.rs @@ -0,0 +1,20 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unboxed_closures)] + +// Test that an unboxed closure that captures a free variable by +// reference cannot escape the region of that variable. +fn main() { + let _f = { + let x = 0u; + |:| x //~ ERROR cannot infer an appropriate lifetime due to conflicting requirements + }; +} diff --git a/src/test/compile-fail/unboxed-closures-borrow-conflict.rs b/src/test/compile-fail/unboxed-closures-borrow-conflict.rs new file mode 100644 index 00000000000..baf7f3f5e58 --- /dev/null +++ b/src/test/compile-fail/unboxed-closures-borrow-conflict.rs @@ -0,0 +1,20 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(unboxed_closures)] + +// Test that an unboxed closure that mutates a free variable will +// cause borrow conflicts. + +fn main() { + let mut x = 0u; + let f = |:| x += 1; + let _y = x; //~ ERROR cannot use `x` because it was mutably borrowed +} diff --git a/src/test/run-pass/unboxed-closures-by-ref.rs b/src/test/run-pass/unboxed-closures-by-ref.rs new file mode 100644 index 00000000000..2ee28d19b75 --- /dev/null +++ b/src/test/run-pass/unboxed-closures-by-ref.rs @@ -0,0 +1,35 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(overloaded_calls, unboxed_closures)] + +// Test by-ref capture of environment in unboxed closure types + +fn call_fn(f: F) { + f() +} + +fn call_fn_mut(mut f: F) { + f() +} + +fn call_fn_once(f: F) { + f() +} + +fn main() { + let mut x = 0u; + let y = 2u; + + call_fn(|&:| x += y); + call_fn_mut(|&mut:| x += y); + call_fn_once(|:| x += y); + assert_eq!(x, y * 3); +}