about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-05-24 11:12:34 +0000
committerbors <bors@rust-lang.org>2015-05-24 11:12:34 +0000
commitba0e1cd8147d452c356aacb29fb87568ca26f111 (patch)
tree0cf03636be9210de94e30bb07467097aeb5273c7 /src/test
parentcc56c20ba4ccc12f58e1c844b77168bc4805cbb6 (diff)
parent82ded3cd03e001e82b9dce9aedac621a5292231b (diff)
downloadrust-ba0e1cd8147d452c356aacb29fb87568ca26f111.tar.gz
rust-ba0e1cd8147d452c356aacb29fb87568ca26f111.zip
Auto merge of #25609 - nikomatsakis:const-fn, r=pnkfelix
This is a port of @eddyb's `const-fn` branch. I rebased it, tweaked a few things, and added tests as well as a feature gate. The set of tests is still pretty rudimentary, I'd appreciate suggestions on new tests to write. Also, a double-check that the feature-gate covers all necessary cases.

One question: currently, the feature-gate allows the *use* of const functions from stable code, just not the definition. This seems to fit our usual strategy, and implies that we might (perhaps) allow some constant functions in libstd someday, even before stabilizing const-fn, if we were willing to commit to the existence of const fns but found some details of their impl unsatisfactory.

r? @pnkfelix 
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/check-static-values-constraints.rs2
-rw-r--r--src/test/compile-fail/const-fn-mismatch.rs26
-rw-r--r--src/test/compile-fail/const-fn-not-in-trait.rs21
-rw-r--r--src/test/compile-fail/const-fn-not-safe-for-const.rs47
-rw-r--r--src/test/compile-fail/const-fn-stability.rs28
-rw-r--r--src/test/compile-fail/issue-16538.rs2
-rw-r--r--src/test/compile-fail/issue-7364.rs2
-rw-r--r--src/test/compile-fail/static-vec-repeat-not-constant.rs2
-rw-r--r--src/test/run-pass/const-fn-nested.rs23
-rw-r--r--src/test/run-pass/const-fn.rs32
10 files changed, 181 insertions, 4 deletions
diff --git a/src/test/compile-fail/check-static-values-constraints.rs b/src/test/compile-fail/check-static-values-constraints.rs
index 0180bccbca4..c3a1de11752 100644
--- a/src/test/compile-fail/check-static-values-constraints.rs
+++ b/src/test/compile-fail/check-static-values-constraints.rs
@@ -117,7 +117,7 @@ static mut STATIC14: SafeStruct = SafeStruct {
 //~^ ERROR mutable statics are not allowed to have destructors
     field1: SafeEnum::Variant1,
     field2: SafeEnum::Variant4("str".to_string())
-//~^ ERROR static contains unimplemented expression type
+//~^ ERROR method calls in statics are limited to constant inherent methods
 };
 
 static STATIC15: &'static [Box<MyOwned>] = &[
diff --git a/src/test/compile-fail/const-fn-mismatch.rs b/src/test/compile-fail/const-fn-mismatch.rs
new file mode 100644
index 00000000000..d813cf32954
--- /dev/null
+++ b/src/test/compile-fail/const-fn-mismatch.rs
@@ -0,0 +1,26 @@
+// 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 <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.
+
+// Test that we can't declare a const fn in an impl -- right now it's
+// just not allowed at all, though eventually it'd make sense to allow
+// it if the trait fn is const (but right now no trait fns can be
+// const).
+
+#![feature(const_fn)]
+
+trait Foo {
+    fn f() -> u32;
+}
+
+impl Foo for u32 {
+    const fn f() -> u32 { 22 } //~ ERROR E0379
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/const-fn-not-in-trait.rs b/src/test/compile-fail/const-fn-not-in-trait.rs
new file mode 100644
index 00000000000..191f3e02527
--- /dev/null
+++ b/src/test/compile-fail/const-fn-not-in-trait.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 <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.
+
+// Test that const fn is illegal in a trait declaration, whether or
+// not a default is provided.
+
+#![feature(const_fn)]
+
+trait Foo {
+    const fn f() -> u32; //~ ERROR trait fns cannot be declared const
+    const fn g() -> u32 { 0 } //~ ERROR trait fns cannot be declared const
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/const-fn-not-safe-for-const.rs b/src/test/compile-fail/const-fn-not-safe-for-const.rs
new file mode 100644
index 00000000000..baa3eba0680
--- /dev/null
+++ b/src/test/compile-fail/const-fn-not-safe-for-const.rs
@@ -0,0 +1,47 @@
+// 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 <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.
+
+// Test that we can't call random fns in a const fn or do other bad things.
+
+#![feature(const_fn)]
+
+use std::mem::transmute;
+
+fn random() -> u32 { 0 }
+
+const fn sub(x: &u32) -> usize {
+    unsafe { transmute(x) } //~ ERROR E0015
+}
+
+const fn sub1() -> u32 {
+    random() //~ ERROR E0015
+}
+
+static Y: u32 = 0;
+
+const fn get_Y() -> u32 {
+    Y
+        //~^ ERROR E0013
+        //~| ERROR cannot refer to other statics by value
+}
+
+const fn get_Y_addr() -> &'static u32 {
+    &Y
+        //~^ ERROR E0013
+}
+
+const fn get() -> u32 {
+    let x = 22; //~ ERROR E0016
+    let y = 44; //~ ERROR E0016
+    x + y
+}
+
+fn main() {
+}
diff --git a/src/test/compile-fail/const-fn-stability.rs b/src/test/compile-fail/const-fn-stability.rs
new file mode 100644
index 00000000000..8aa5189bcd6
--- /dev/null
+++ b/src/test/compile-fail/const-fn-stability.rs
@@ -0,0 +1,28 @@
+// 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 <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.
+
+// Test use of const fn without feature gate.
+
+const fn foo() -> usize { 0 } //~ ERROR const fn is unstable
+
+trait Foo {
+    const fn foo() -> u32; //~ ERROR const fn is unstable
+    const fn bar() -> u32 { 0 } //~ ERROR const fn is unstable
+}
+
+impl Foo {
+    const fn baz() -> u32 { 0 } //~ ERROR const fn is unstable
+}
+
+impl Foo for u32 {
+    const fn foo() -> u32 { 0 } //~ ERROR const fn is unstable
+}
+
+fn main() { }
diff --git a/src/test/compile-fail/issue-16538.rs b/src/test/compile-fail/issue-16538.rs
index a4e0f69b63b..2b53e92d9bc 100644
--- a/src/test/compile-fail/issue-16538.rs
+++ b/src/test/compile-fail/issue-16538.rs
@@ -20,6 +20,6 @@ mod Y {
 
 static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
 //~^ ERROR the trait `core::marker::Sync` is not implemented for the type
-//~| ERROR function calls in statics are limited to struct and enum constructors
+//~| ERROR E0015
 
 fn main() {}
diff --git a/src/test/compile-fail/issue-7364.rs b/src/test/compile-fail/issue-7364.rs
index 6a36b2f84bf..5d85fe93a48 100644
--- a/src/test/compile-fail/issue-7364.rs
+++ b/src/test/compile-fail/issue-7364.rs
@@ -17,6 +17,6 @@ static boxed: Box<RefCell<isize>> = box RefCell::new(0);
 //~^ ERROR allocations are not allowed in statics
 //~| ERROR the trait `core::marker::Sync` is not implemented for the type
 //~| ERROR the trait `core::marker::Sync` is not implemented for the type
-//~| ERROR function calls in statics are limited to struct and enum constructors
+//~| ERROR E0015
 
 fn main() { }
diff --git a/src/test/compile-fail/static-vec-repeat-not-constant.rs b/src/test/compile-fail/static-vec-repeat-not-constant.rs
index 7a957564587..a533a5bd54d 100644
--- a/src/test/compile-fail/static-vec-repeat-not-constant.rs
+++ b/src/test/compile-fail/static-vec-repeat-not-constant.rs
@@ -11,6 +11,6 @@
 fn foo() -> isize { 23 }
 
 static a: [isize; 2] = [foo(); 2];
-//~^ ERROR: function calls in statics are limited to struct and enum constructors
+//~^ ERROR: E0015
 
 fn main() {}
diff --git a/src/test/run-pass/const-fn-nested.rs b/src/test/run-pass/const-fn-nested.rs
new file mode 100644
index 00000000000..86f5dedc4d1
--- /dev/null
+++ b/src/test/run-pass/const-fn-nested.rs
@@ -0,0 +1,23 @@
+// 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 <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.
+
+// Test a call whose argument is the result of another call.
+
+#![feature(const_fn)]
+
+const fn sub(x: u32, y: u32) -> u32 {
+    x - y
+}
+
+const X: u32 = sub(sub(88, 44), 22);
+
+fn main() {
+    assert_eq!(X, 22);
+}
diff --git a/src/test/run-pass/const-fn.rs b/src/test/run-pass/const-fn.rs
new file mode 100644
index 00000000000..9bd8eb55cc3
--- /dev/null
+++ b/src/test/run-pass/const-fn.rs
@@ -0,0 +1,32 @@
+// 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 <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.
+
+// A very basic test of const fn functionality.
+
+#![feature(const_fn)]
+
+const fn add(x: u32, y: u32) -> u32 {
+    x + y
+}
+
+const fn sub(x: u32, y: u32) -> u32 {
+    x - y
+}
+
+const SUM: u32 = add(44, 22);
+const DIFF: u32 = sub(44, 22);
+
+fn main() {
+    assert_eq!(SUM, 66);
+    assert!(SUM != 88);
+
+    assert_eq!(DIFF, 22);
+
+}