diff --git a/src/test/compile-fail/closure-no-fn.rs b/src/test/compile-fail/closure-no-fn.rs new file mode 100644 index 0000000000000..6ad65523833a9 --- /dev/null +++ b/src/test/compile-fail/closure-no-fn.rs @@ -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 or the MIT license +// , 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 +} diff --git a/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs b/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs new file mode 100644 index 0000000000000..20ed24f025b89 --- /dev/null +++ b/src/test/compile-fail/feature-gate-closure_to_fn_coercion.rs @@ -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 or the MIT license +// , 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 + } + +} + + +