From 521ca31071c01b21de40ce3477a81972103b3db9 Mon Sep 17 00:00:00 2001 From: Brian Koropoff Date: Fri, 3 Oct 2014 00:57:21 -0700 Subject: [PATCH] Add some more test coverage of by-ref unboxed closures --- .../compile-fail/unboxed-closure-region.rs | 20 +++++++++++ .../unboxed-closures-borrow-conflict.rs | 20 +++++++++++ src/test/run-pass/unboxed-closures-by-ref.rs | 35 +++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/test/compile-fail/unboxed-closure-region.rs create mode 100644 src/test/compile-fail/unboxed-closures-borrow-conflict.rs create mode 100644 src/test/run-pass/unboxed-closures-by-ref.rs 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 000000000000..2a71aeaca5f3 --- /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 000000000000..baf7f3f5e58a --- /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 000000000000..2ee28d19b75a --- /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); +}