-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// run-pass | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
#![feature(const_fn)] | ||
|
||
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_const(2); | ||
assert_eq!(Y, 4); | ||
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// run-pass | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
#![feature(const_fn)] | ||
#![allow(unused)] | ||
|
||
fn double(x: usize) -> usize { x * 2 } | ||
const X: fn(usize) -> usize = double; | ||
|
||
const fn bar(x: usize) -> usize { | ||
X(x) // FIXME: this should error someday | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// compile-flags: -Zunleash-the-miri-inside-of-you | ||
#![feature(const_fn)] | ||
#![allow(const_err)] | ||
|
||
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); // 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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
warning: skipping const checks | ||
--> $DIR/const_fn_ptr_fail2.rs:16: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_fail2.rs:16: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_fail2.rs:16: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_fail2.rs:21: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_fail2.rs:21: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_fail2.rs:21: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) | ||
|
||
error[E0080]: evaluation of constant expression failed | ||
--> $DIR/const_fn_ptr_fail2.rs:16:5 | ||
| | ||
LL | assert_eq!(Y, 4); | ||
| ^^^^^^^^^^^-^^^^^ | ||
| | | ||
| referenced constant has errors | ||
| | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error[E0080]: evaluation of constant expression failed | ||
--> $DIR/const_fn_ptr_fail2.rs:21:5 | ||
| | ||
LL | assert_eq!(Z, 4); | ||
| ^^^^^^^^^^^-^^^^^ | ||
| | | ||
| referenced constant has errors | ||
| | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.