Skip to content

Allow using fn pointers in const fn with unleash miri #64635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove feature
  • Loading branch information
gnzlbg committed Sep 21, 2019
commit d4344969731e55bd0acaee4ad6962a85c8e0f27a
17 changes: 6 additions & 11 deletions src/librustc_mir/transform/qualify_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1413,17 +1413,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Checker<'a, 'tcx> {
.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() {
if !(unleash_miri || const_fn_ptr) {
emit_feature_err(
&self.tcx.sess.parse_sess,
sym::const_fn_ptr,
self.span,
GateIssue::Language,
"function pointers in const fn are unstable",
);
}
if self.mode.requires_const_checking() && !unleash_miri {
let mut err = self.tcx.sess.struct_span_err(
self.span,
"function pointers in `const fn` are unstable",
);
err.emit();
}
}
_ => {
Expand Down
3 changes: 0 additions & 3 deletions src/libsyntax/feature_gate/active.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,6 @@ 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),

Expand Down
1 change: 0 additions & 1 deletion src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ symbols! {
const_compare_raw_pointers,
const_constructor,
const_fn,
const_fn_ptr,
const_fn_union,
const_generics,
const_indexing,
Expand Down
24 changes: 20 additions & 4 deletions src/test/ui/consts/const-eval/const_fn_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
// run-pass
// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_fn)]
#![feature(const_fn_ptr)]

const fn double(x: usize) -> usize { x * 2 }
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;

const fn bar(x: usize) -> usize {
X(x)
}

const fn bar_const(x: usize) -> usize {
X_const(x)
}

const fn foo(x: fn(usize) -> usize, y: usize) -> usize {
x(y)
}

fn main() {
const Y: usize = bar(2);
const Y: usize = bar_const(2);
assert_eq!(Y, 4);
const Z: usize = foo(double, 2);
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);
let z = foo(double_const, 2);
assert_eq!(z, 4);
let z = foo(double, 2);
assert_eq!(z, 4);
}
152 changes: 152 additions & 0 deletions src/test/ui/consts/const-eval/const_fn_ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
warning: skipping const checks
--> $DIR/const_fn_ptr.rs:25:5
|
LL | assert_eq!(Y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:25:5
|
LL | assert_eq!(Y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:25:5
|
LL | assert_eq!(Y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:27:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:27:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:27:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:29:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:29:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:29:5
|
LL | assert_eq!(y, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:32:5
|
LL | assert_eq!(Z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:32:5
|
LL | assert_eq!(Z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:32:5
|
LL | assert_eq!(Z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:34:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:34:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:34:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:36:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:36:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: skipping const checks
--> $DIR/const_fn_ptr.rs:36:5
|
LL | assert_eq!(z, 4);
| ^^^^^^^^^^^^^^^^^
|
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: constant `X_const` should have an upper case name
--> $DIR/const_fn_ptr.rs:9:7
|
LL | const X_const: fn(usize) -> usize = double_const;
| ^^^^^^^ help: convert the identifier to upper case: `X_CONST`
|
= note: `#[warn(non_upper_case_globals)]` on by default

8 changes: 3 additions & 5 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// run-pass

// FIXME: this should not pass

// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_fn)]
#![feature(const_fn_ptr)]
#![allow(unused)]

fn double(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;

const fn bar(x: usize) -> usize {
X(x)
X(x) // FIXME: this should error someday
}

fn main() {}
20 changes: 0 additions & 20 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail.stderr

This file was deleted.

19 changes: 12 additions & 7 deletions src/test/ui/consts/const-eval/const_fn_ptr_fail2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// compile-flags: -Zunleash-the-miri-inside-of-you
#![feature(const_fn)]
#![feature(const_fn_ptr)]
#![allow(const_err)]

fn double(x: usize) -> usize { x * 2 }
const X: fn(usize) -> usize = double;
Expand All @@ -8,14 +9,18 @@ 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

const Y: usize = bar(X, 2); // FIXME: should fail to typeck someday
const Z: usize = bar(double, 2); // FIXME: should fail to typeck someday

fn main() {
assert_eq!(Y, 4);
//~^ ERROR evaluation of constant expression failed
//~^^ WARN skipping const checks
//~^^^ WARN skipping const checks
//~^^^^ WARN skipping const checks
assert_eq!(Z, 4);
//~^ ERROR evaluation of constant expression failed
//~^^ WARN skipping const checks
//~^^^ WARN skipping const checks
//~^^^^ WARN skipping const checks
}
Loading