Allow using fn pointers in const fn behind const_fn_ptr gate

This commit is contained in:
gnzlbg 2019-09-20 18:39:13 +02:00
parent ea3ba36f3f
commit 9cf9030e1c
10 changed files with 148 additions and 4 deletions

View file

@ -1407,11 +1407,23 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
}
}
ty::FnPtr(_) => {
let unleash_miri = self
.tcx
.sess
.opts
.debugging_opts
.unleash_the_miri_inside_of_you;
let const_fn_ptr = self.tcx.features().const_fn_ptr;
if self.mode.requires_const_checking() {
let mut err = self.tcx.sess.struct_span_err(
if !(unleash_miri || const_fn_ptr) {
emit_feature_err(
&self.tcx.sess.parse_sess,
sym::const_fn_ptr,
self.span,
&format!("function pointers are not allowed in const fn"));
err.emit();
GateIssue::Language,
"function pointers in const fn are unstable",
);
}
}
}
_ => {

View file

@ -405,6 +405,9 @@ declare_features! (
/// Allows macro invocations in `extern {}` blocks.
(active, macros_in_extern, "1.27.0", Some(49476), None),
/// Allows calling function pointers inside `const` functions.
(active, const_fn_ptr, "1.27.0", Some(51909), None),
/// Allows accessing fields of unions inside `const` functions.
(active, const_fn_union, "1.27.0", Some(51909), None),

View file

@ -198,6 +198,7 @@ symbols! {
const_compare_raw_pointers,
const_constructor,
const_fn,
const_fn_ptr,
const_fn_union,
const_generics,
const_indexing,

View file

@ -0,0 +1,21 @@
// run-pass
#![feature(const_fn)]
#![feature(const_fn_ptr)]
const fn double(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;
const fn bar(x: usize) -> usize {
X(x)
}
const fn foo(x: fn(usize) -> usize, y: usize) -> usize {
x(y)
}
fn main() {
const Y: usize = bar(2);
assert_eq!(Y, 4);
const Z: usize = foo(double, 2);
assert_eq!(Z, 4);
}

View file

@ -0,0 +1,15 @@
// run-pass
// FIXME: this should not pass
#![feature(const_fn)]
#![feature(const_fn_ptr)]
fn double(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;
const fn bar(x: usize) -> usize {
X(x)
}
fn main() {}

View file

@ -0,0 +1,20 @@
warning: function is never used: `double`
--> $DIR/const_fn_ptr_fail.rs:8:1
|
LL | fn double(x: usize) -> usize { x * 2 }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: constant item is never used: `X`
--> $DIR/const_fn_ptr_fail.rs:9:1
|
LL | const X: fn(usize) -> usize = double;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: function is never used: `bar`
--> $DIR/const_fn_ptr_fail.rs:11:1
|
LL | const fn bar(x: usize) -> usize {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -0,0 +1,21 @@
#![feature(const_fn)]
#![feature(const_fn_ptr)]
fn double(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;
const fn bar(x: fn(usize) -> usize, y: usize) -> usize {
x(y)
}
const Y: usize = bar(X, 2);
//~^ ERROR any use of this value will cause an error
const Z: usize = bar(double, 2);
//~^ ERROR any use of this value will cause an error
fn main() {
assert_eq!(Y, 4);
assert_eq!(Z, 4);
}

View file

@ -0,0 +1,28 @@
error: any use of this value will cause an error
--> $DIR/const_fn_ptr_fail2.rs:8:5
|
LL | x(y)
| ^^^^
| |
| calling non-const function `double`
| inside call to `bar` at $DIR/const_fn_ptr_fail2.rs:11:18
...
LL | const Y: usize = bar(X, 2);
| ---------------------------
|
= note: `#[deny(const_err)]` on by default
error: any use of this value will cause an error
--> $DIR/const_fn_ptr_fail2.rs:8:5
|
LL | x(y)
| ^^^^
| |
| calling non-const function `double`
| inside call to `bar` at $DIR/const_fn_ptr_fail2.rs:14:18
...
LL | const Z: usize = bar(double, 2);
| --------------------------------
error: aborting due to 2 previous errors

View file

@ -0,0 +1,11 @@
#![feature(const_fn)]
fn main() {}
const fn foo() {}
const X: fn() = foo;
const fn bar() {
X()
//~^ ERROR function pointers in const fn are unstable
}

View file

@ -0,0 +1,12 @@
error[E0658]: function pointers in const fn are unstable
--> $DIR/feature-gate-const_fn_ptr.rs:9:5
|
LL | X()
| ^^^
|
= note: for more information, see https://github.com/rust-lang/rust/issues/51909
= help: add `#![feature(const_fn_ptr)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.