Add some more test coverage of by-ref unboxed closures
This commit is contained in:
parent
39344c2d7e
commit
521ca31071
20
src/test/compile-fail/unboxed-closure-region.rs
Normal file
20
src/test/compile-fail/unboxed-closure-region.rs
Normal file
@ -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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
|
||||
};
|
||||
}
|
20
src/test/compile-fail/unboxed-closures-borrow-conflict.rs
Normal file
20
src/test/compile-fail/unboxed-closures-borrow-conflict.rs
Normal file
@ -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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
|
||||
}
|
35
src/test/run-pass/unboxed-closures-by-ref.rs
Normal file
35
src/test/run-pass/unboxed-closures-by-ref.rs
Normal file
@ -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 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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: Fn()>(f: F) {
|
||||
f()
|
||||
}
|
||||
|
||||
fn call_fn_mut<F: FnMut()>(mut f: F) {
|
||||
f()
|
||||
}
|
||||
|
||||
fn call_fn_once<F: FnOnce()>(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);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user