allow constant evaluation of function calls

This commit is contained in:
Oliver Schneider 2015-10-14 12:30:10 +02:00
parent 9ed32bba77
commit b4e30bd2a3
14 changed files with 155 additions and 52 deletions

View file

@ -14,7 +14,7 @@
struct S(i32);
const CONSTANT: S = S(0);
//~^ ERROR: constant evaluation error: unsupported constant expr
//~^ ERROR: constant evaluation error: non-constant path in constant expression [E0080]
enum E {
V = CONSTANT,

View file

@ -0,0 +1,18 @@
// 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 certain things are disallowed in const fn signatures
#![feature(const_fn)]
// no destructuring
const fn i((a, b): (u32, u32)) -> u32 { a + b } //~ ERROR: E0022
fn main() {}

View file

@ -17,5 +17,5 @@ extern crate const_fn_lib;
use const_fn_lib::foo;
fn main() {
let x: [usize; foo()] = []; //~ ERROR unsupported constant expr
let x: [usize; foo()] = []; //~ ERROR non-constant path in constant expr
}

View file

@ -0,0 +1,19 @@
// Copyright 2012-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(const_fn)]
const fn add(x: usize, y: usize) -> usize {
x + y
}
const ARR: [i32; add(1, 2)] = [5, 6, 7];
pub fn main() {}

View file

@ -13,9 +13,6 @@
// Check that we do *not* overflow on a number of edge cases.
// (compare with test/run-fail/overflowing-{lsh,rsh}*.rs)
// (Work around constant-evaluation)
fn id<T>(x: T) -> T { x }
fn main() {
test_left_shift();
test_right_shift();
@ -26,34 +23,34 @@ fn test_left_shift() {
macro_rules! tests {
($iN:ty, $uN:ty, $max_rhs:expr, $expect_i:expr, $expect_u:expr) => { {
let x = (1 as $iN) << id(0);
let x = (1 as $iN) << 0;
assert_eq!(x, 1);
let x = (1 as $uN) << id(0);
let x = (1 as $uN) << 0;
assert_eq!(x, 1);
let x = (1 as $iN) << id($max_rhs);
let x = (1 as $iN) << $max_rhs;
assert_eq!(x, $expect_i);
let x = (1 as $uN) << id($max_rhs);
let x = (1 as $uN) << $max_rhs;
assert_eq!(x, $expect_u);
// high-order bits on LHS are silently discarded without panic.
let x = (3 as $iN) << id($max_rhs);
let x = (3 as $iN) << $max_rhs;
assert_eq!(x, $expect_i);
let x = (3 as $uN) << id($max_rhs);
let x = (3 as $uN) << $max_rhs;
assert_eq!(x, $expect_u);
} }
}
let x = 1_i8 << id(0);
let x = 1_i8 << 0;
assert_eq!(x, 1);
let x = 1_u8 << id(0);
let x = 1_u8 << 0;
assert_eq!(x, 1);
let x = 1_i8 << id(7);
let x = 1_i8 << 7;
assert_eq!(x, std::i8::MIN);
let x = 1_u8 << id(7);
let x = 1_u8 << 7;
assert_eq!(x, 0x80);
// high-order bits on LHS are silently discarded without panic.
let x = 3_i8 << id(7);
let x = 3_i8 << 7;
assert_eq!(x, std::i8::MIN);
let x = 3_u8 << id(7);
let x = 3_u8 << 7;
assert_eq!(x, 0x80);
// above is (approximately) expanded from:
@ -71,23 +68,23 @@ fn test_right_shift() {
($iN:ty, $uN:ty, $max_rhs:expr,
$signbit_i:expr, $highbit_i:expr, $highbit_u:expr) =>
{ {
let x = (1 as $iN) >> id(0);
let x = (1 as $iN) >> 0;
assert_eq!(x, 1);
let x = (1 as $uN) >> id(0);
let x = (1 as $uN) >> 0;
assert_eq!(x, 1);
let x = ($highbit_i) >> id($max_rhs-1);
let x = ($highbit_i) >> $max_rhs-1;
assert_eq!(x, 1);
let x = ($highbit_u) >> id($max_rhs);
let x = ($highbit_u) >> $max_rhs;
assert_eq!(x, 1);
// sign-bit is carried by arithmetic right shift
let x = ($signbit_i) >> id($max_rhs);
let x = ($signbit_i) >> $max_rhs;
assert_eq!(x, -1);
// low-order bits on LHS are silently discarded without panic.
let x = ($highbit_i + 1) >> id($max_rhs-1);
let x = ($highbit_i + 1) >> $max_rhs-1;
assert_eq!(x, 1);
let x = ($highbit_u + 1) >> id($max_rhs);
let x = ($highbit_u + 1) >> $max_rhs;
assert_eq!(x, 1);
let x = ($signbit_i + 1) >> id($max_rhs);
let x = ($signbit_i + 1) >> $max_rhs;
assert_eq!(x, -1);
} }
}