-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2017 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. | ||
|
||
// Ensure that capturing closures are never coerced to fns | ||
// Especially interesting as non-capturing closures can be. | ||
|
||
fn main() { | ||
let mut a = 0u8; | ||
let foo :fn(u8) -> u8 = |v: u8| { a += v; a }; | ||
//~^ ERROR mismatched types | ||
let b = 0u8; | ||
let bar :fn() -> u8 = || { b }; | ||
//~^ ERROR mismatched types | ||
let baz :fn() -> u8 = || { b } as fn() -> u8; | ||
//~^ ERROR mismatched types | ||
//~^^ ERROR non-scalar cast | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2017 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. | ||
|
||
// ignore-stage0: new feature, remove this when SNAP | ||
// revisions: a b | ||
|
||
#[cfg(a)] | ||
mod a { | ||
const FOO :fn(u8) -> u8 = |v: u8| { v }; | ||
//[a]~^ ERROR non-capturing closure to fn coercion is experimental | ||
//[a]~^^ ERROR mismatched types | ||
|
||
const BAR: [fn(&mut u32); 1] = [ | ||
|v: &mut u32| *v += 1, | ||
//[a]~^ ERROR non-capturing closure to fn coercion is experimental | ||
//[a]~^^ ERROR mismatched types | ||
]; | ||
} | ||
|
||
#[cfg(b)] | ||
mod b { | ||
fn func_specific() -> (fn() -> u32) { | ||
|| return 42 | ||
//[b]~^ ERROR non-capturing closure to fn coercion is experimental | ||
//[b]~^^ ERROR mismatched types | ||
} | ||
fn foo() { | ||
// Items | ||
assert_eq!(func_specific()(), 42); | ||
let foo :fn(u8) -> u8 = |v: u8| { v }; | ||
//[b]~^ ERROR non-capturing closure to fn coercion is experimental | ||
//[b]~^^ ERROR mismatched types | ||
} | ||
|
||
} | ||
|
||
|
||
|