diff --git a/src/test/auxiliary/const_fn_lib.rs b/src/test/auxiliary/const_fn_lib.rs new file mode 100644 index 00000000000..b0d5a6b1272 --- /dev/null +++ b/src/test/auxiliary/const_fn_lib.rs @@ -0,0 +1,16 @@ +// Copyright 2015 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. + +// Crate that exports a const fn. Used for testing cross-crate. + +#![crate_type="rlib"] +#![feature(const_fn)] + +pub const fn foo() -> usize { 22 } //~ ERROR const fn is unstable diff --git a/src/test/compile-fail/const-fn-stability-calls-2.rs b/src/test/compile-fail/const-fn-stability-calls-2.rs new file mode 100644 index 00000000000..dd9a415311e --- /dev/null +++ b/src/test/compile-fail/const-fn-stability-calls-2.rs @@ -0,0 +1,21 @@ +// Copyright 2015 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. + +// Test use of const fn from another crate without a feature gate. + +// aux-build:const_fn_lib.rs + +extern crate const_fn_lib; + +use const_fn_lib::foo; + +fn main() { + let x: [usize; foo()] = []; //~ ERROR unsupported constant expr +} diff --git a/src/test/compile-fail/const-fn-stability-calls-3.rs b/src/test/compile-fail/const-fn-stability-calls-3.rs new file mode 100644 index 00000000000..0f413b0bbc1 --- /dev/null +++ b/src/test/compile-fail/const-fn-stability-calls-3.rs @@ -0,0 +1,25 @@ +// Copyright 2015 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. + +// Test use of const fn from another crate without a feature gate. + +#![feature(rustc_attrs)] +#![allow(unused_variables)] + +// aux-build:const_fn_lib.rs + +extern crate const_fn_lib; + +use const_fn_lib::foo; + +#[rustc_error] +fn main() { //~ ERROR compilation successful + let x = foo(); // use outside a constant is ok +} diff --git a/src/test/compile-fail/const-fn-stability-calls.rs b/src/test/compile-fail/const-fn-stability-calls.rs new file mode 100644 index 00000000000..609077663ef --- /dev/null +++ b/src/test/compile-fail/const-fn-stability-calls.rs @@ -0,0 +1,34 @@ +// Copyright 2015 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. + +// Test use of const fn from another crate without a feature gate. + +// aux-build:const_fn_lib.rs + +extern crate const_fn_lib; + +use const_fn_lib::foo; + +static FOO: usize = foo(); //~ ERROR const fns are an unstable feature +const BAR: usize = foo(); //~ ERROR const fns are an unstable feature + +macro_rules! constant { + ($n:ident: $t:ty = $v:expr) => { + const $n: $t = $v; + } +} + +constant! { + BAZ: usize = foo() //~ ERROR const fns are an unstable feature +} + +fn main() { +// let x: [usize; foo()] = []; +} diff --git a/src/test/compile-fail/const-fn-stability.rs b/src/test/compile-fail/const-fn-stability.rs index fda2930c4a9..9913c2fa12c 100644 --- a/src/test/compile-fail/const-fn-stability.rs +++ b/src/test/compile-fail/const-fn-stability.rs @@ -28,6 +28,16 @@ const fn foo() -> u32 { 0 } //~ ERROR const fn is unstable static FOO: usize = foo(); const BAR: usize = foo(); +macro_rules! constant { + ($n:ident: $t:ty = $v:expr) => { + const $n: $t = $v; + } +} + +constant! { + BAZ: usize = foo() +} + fn main() { let x: [usize; foo()] = []; } diff --git a/src/test/run-pass/const-fn-cross-crate.rs b/src/test/run-pass/const-fn-cross-crate.rs new file mode 100644 index 00000000000..5d0c17af717 --- /dev/null +++ b/src/test/run-pass/const-fn-cross-crate.rs @@ -0,0 +1,25 @@ +// Copyright 2015 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. + +// aux-build:const_fn_lib.rs + +// A very basic test of const fn functionality. + +#![feature(const_fn)] + +extern crate const_fn_lib; + +use const_fn_lib::foo; + +const FOO: usize = foo(); + +fn main() { + assert_eq!(FOO, 22); +}