-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using fn pointers in const fn behind const_fn_ptr gate
- Loading branch information
Showing
10 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,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); | ||
} |
This file contains 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,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() {} |
This file contains 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,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 { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
This file contains 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,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); | ||
} |
This file contains 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,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 | ||
|
11 changes: 11 additions & 0 deletions
11
src/test/ui/consts/const-eval/feature-gate-const_fn_ptr.rs
This file contains 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,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 | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/consts/const-eval/feature-gate-const_fn_ptr.stderr
This file contains 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,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`. |