提交 521ca310 编写于 作者: B Brian Koropoff

Add some more test coverage of by-ref unboxed closures

上级 39344c2d
// 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
};
}
// 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
}
// 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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册