Auto merge of #26848 - oli-obk:const_fn_const_eval, r=pnkfelix
this has the funky side-effect of also allowing constant evaluation of function calls to functions that are not `const fn` as long as `check_const` didn't mark that function `NOT_CONST`
It's still not possible to call a normal function from a `const fn`, but let statements' initialization value can get const evaluated (this caused the fallout in the overflowing tests)
we can now do this:
```rust
const fn add(x: usize, y: usize) -> usize { x + y }
const ARR: [i32; add(1, 2)] = [5, 6, 7];
```
also added a test for destructuring in const fn args
```rust
const fn i((a, b): (u32, u32)) -> u32 { a + b } //~ ERROR: E0022
```
This is a **[breaking change]**, since it turns some runtime panics into compile-time errors. This statement is true for ANY improvement to the const evaluator.
This commit is contained in:
commit
540fd3aa71
14 changed files with 163 additions and 53 deletions
|
|
@ -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,
|
||||
|
|
|
|||
18
src/test/compile-fail/const-fn-destructuring-arg.rs
Normal file
18
src/test/compile-fail/const-fn-destructuring-arg.rs
Normal 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() {}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
19
src/test/run-pass/const-fn-const-eval.rs
Normal file
19
src/test/run-pass/const-fn-const-eval.rs
Normal 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() {}
|
||||
|
|
@ -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);
|
||||
} }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue