rust/tests/ui/consts/const-eval/const_fn_ptr.rs

37 lines
751 B
Rust
Raw Normal View History

// run-pass
2019-09-21 09:09:38 -05:00
// compile-flags: -Zunleash-the-miri-inside-of-you
2019-09-21 09:09:38 -05:00
fn double(x: usize) -> usize { x * 2 }
const fn double_const(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;
const X_CONST: fn(usize) -> usize = double_const;
2020-05-03 07:23:08 -05:00
const fn bar(x: usize) -> usize {
2020-04-29 02:53:22 -05:00
X(x)
}
2020-05-03 07:23:08 -05:00
const fn bar_const(x: usize) -> usize {
2020-04-29 02:53:22 -05:00
X_CONST(x)
2019-09-21 09:09:38 -05:00
}
2020-05-03 07:23:08 -05:00
const fn foo(x: fn(usize) -> usize, y: usize) -> usize {
2020-04-29 02:53:22 -05:00
x(y)
}
fn main() {
2019-09-21 09:09:38 -05:00
const Y: usize = bar_const(2);
assert_eq!(Y, 4);
2019-09-21 09:09:38 -05:00
let y = bar_const(2);
assert_eq!(y, 4);
let y = bar(2);
assert_eq!(y, 4);
const Z: usize = foo(double_const, 2);
assert_eq!(Z, 4);
2019-09-21 09:09:38 -05:00
let z = foo(double_const, 2);
assert_eq!(z, 4);
let z = foo(double, 2);
assert_eq!(z, 4);
}