-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add support for const unsafe? extern fn
#64906
Merged
Merged
Changes from all commits
Commits
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 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
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
23 changes: 23 additions & 0 deletions
23
src/test/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.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,23 @@ | ||
#![feature(const_extern_fn)] | ||
|
||
extern "C" { | ||
fn regular_in_block(); | ||
} | ||
|
||
const extern fn bar() { | ||
unsafe { | ||
regular_in_block(); | ||
//~^ ERROR: cannot call functions with `"C"` abi in `min_const_fn` | ||
} | ||
} | ||
|
||
extern fn regular() {} | ||
|
||
const extern fn foo() { | ||
unsafe { | ||
regular(); | ||
//~^ ERROR: cannot call functions with `"C"` abi in `min_const_fn` | ||
} | ||
} | ||
Aaron1011 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/consts/const-extern-fn/const-extern-fn-call-extern-fn.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,21 @@ | ||
error[E0723]: cannot call functions with `"C"` abi in `min_const_fn` | ||
--> $DIR/const-extern-fn-call-extern-fn.rs:9:9 | ||
| | ||
LL | regular_in_block(); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error[E0723]: cannot call functions with `"C"` abi in `min_const_fn` | ||
--> $DIR/const-extern-fn-call-extern-fn.rs:18:9 | ||
| | ||
LL | regular(); | ||
| ^^^^^^^^^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0723`. |
13 changes: 13 additions & 0 deletions
13
src/test/ui/consts/const-extern-fn/const-extern-fn-min-const-fn.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,13 @@ | ||
#![feature(const_extern_fn)] | ||
|
||
const extern fn unsize(x: &[u8; 3]) -> &[u8] { x } | ||
//~^ ERROR unsizing casts are not allowed in const fn | ||
const unsafe extern "C" fn closure() -> fn() { || {} } | ||
//~^ ERROR function pointers in const fn are unstable | ||
const unsafe extern fn use_float() { 1.0 + 1.0; } | ||
//~^ ERROR only int, `bool` and `char` operations are stable in const fn | ||
const extern "C" fn ptr_cast(val: *const u8) { val as usize; } | ||
//~^ ERROR casting pointers to ints is unstable in const fn | ||
|
||
|
||
fn main() {} |
39 changes: 39 additions & 0 deletions
39
src/test/ui/consts/const-extern-fn/const-extern-fn-min-const-fn.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,39 @@ | ||
error[E0723]: unsizing casts are not allowed in const fn | ||
--> $DIR/const-extern-fn-min-const-fn.rs:3:48 | ||
| | ||
LL | const extern fn unsize(x: &[u8; 3]) -> &[u8] { x } | ||
| ^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error[E0723]: function pointers in const fn are unstable | ||
--> $DIR/const-extern-fn-min-const-fn.rs:5:41 | ||
| | ||
LL | const unsafe extern "C" fn closure() -> fn() { || {} } | ||
| ^^^^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error[E0723]: only int, `bool` and `char` operations are stable in const fn | ||
--> $DIR/const-extern-fn-min-const-fn.rs:7:38 | ||
| | ||
LL | const unsafe extern fn use_float() { 1.0 + 1.0; } | ||
| ^^^^^^^^^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error[E0723]: casting pointers to ints is unstable in const fn | ||
--> $DIR/const-extern-fn-min-const-fn.rs:9:48 | ||
| | ||
LL | const extern "C" fn ptr_cast(val: *const u8) { val as usize; } | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563 | ||
= help: add `#![feature(const_fn)]` to the crate attributes to enable | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0723`. |
10 changes: 10 additions & 0 deletions
10
src/test/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.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,10 @@ | ||
#![feature(const_extern_fn)] | ||
|
||
const unsafe extern fn foo() -> usize { 5 } | ||
|
||
fn main() { | ||
let a: [u8; foo()]; | ||
//~^ ERROR call to unsafe function is unsafe and requires unsafe function or block | ||
foo(); | ||
//~^ ERROR call to unsafe function is unsafe and requires unsafe function or block | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/consts/const-extern-fn/const-extern-fn-requires-unsafe.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,19 @@ | ||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | ||
--> $DIR/const-extern-fn-requires-unsafe.rs:8:5 | ||
| | ||
LL | foo(); | ||
| ^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error[E0133]: call to unsafe function is unsafe and requires unsafe function or block | ||
--> $DIR/const-extern-fn-requires-unsafe.rs:6:17 | ||
| | ||
LL | let a: [u8; foo()]; | ||
| ^^^^^ call to unsafe function | ||
| | ||
= note: consult the function's documentation for information on how to avoid undefined behavior | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0133`. |
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,35 @@ | ||
// run-pass | ||
#![feature(const_extern_fn)] | ||
|
||
const extern fn foo1(val: u8) -> u8 { | ||
val + 1 | ||
} | ||
|
||
const extern "C" fn foo2(val: u8) -> u8 { | ||
val + 1 | ||
} | ||
|
||
const unsafe extern fn bar1(val: bool) -> bool { | ||
!val | ||
} | ||
|
||
const unsafe extern "C" fn bar2(val: bool) -> bool { | ||
!val | ||
} | ||
|
||
|
||
fn main() { | ||
let a: [u8; foo1(25) as usize] = [0; 26]; | ||
let b: [u8; foo2(25) as usize] = [0; 26]; | ||
assert_eq!(a, b); | ||
|
||
let bar1_res = unsafe { bar1(false) }; | ||
let bar2_res = unsafe { bar2(false) }; | ||
assert!(bar1_res); | ||
assert_eq!(bar1_res, bar2_res); | ||
|
||
let _foo1_cast: extern fn(u8) -> u8 = foo1; | ||
let _foo2_cast: extern fn(u8) -> u8 = foo2; | ||
let _bar1_cast: unsafe extern fn(bool) -> bool = bar1; | ||
let _bar2_cast: unsafe extern fn(bool) -> bool = bar2; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.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,12 @@ | ||
// Check that `const extern fn` and `const unsafe extern fn` are feature-gated. | ||
|
||
#[cfg(FALSE)] const extern fn foo1() {} //~ ERROR `const extern fn` definitions are unstable | ||
#[cfg(FALSE)] const extern "C" fn foo2() {} //~ ERROR `const extern fn` definitions are unstable | ||
#[cfg(FALSE)] const extern "Rust" fn foo3() {} //~ ERROR `const extern fn` definitions are unstable | ||
#[cfg(FALSE)] const unsafe extern fn bar1() {} //~ ERROR `const extern fn` definitions are unstable | ||
#[cfg(FALSE)] const unsafe extern "C" fn bar2() {} | ||
//~^ ERROR `const extern fn` definitions are unstable | ||
#[cfg(FALSE)] const unsafe extern "Rust" fn bar3() {} | ||
//~^ ERROR `const extern fn` definitions are unstable | ||
|
||
fn main() {} |
57 changes: 57 additions & 0 deletions
57
src/test/ui/consts/const-extern-fn/feature-gate-const_extern_fn.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,57 @@ | ||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:3:15 | ||
| | ||
LL | #[cfg(FALSE)] const extern fn foo1() {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:4:15 | ||
| | ||
LL | #[cfg(FALSE)] const extern "C" fn foo2() {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:5:15 | ||
| | ||
LL | #[cfg(FALSE)] const extern "Rust" fn foo3() {} | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:6:15 | ||
| | ||
LL | #[cfg(FALSE)] const unsafe extern fn bar1() {} | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:7:15 | ||
| | ||
LL | #[cfg(FALSE)] const unsafe extern "C" fn bar2() {} | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error[E0658]: `const extern fn` definitions are unstable | ||
--> $DIR/feature-gate-const_extern_fn.rs:9:15 | ||
| | ||
LL | #[cfg(FALSE)] const unsafe extern "Rust" fn bar3() {} | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/64926 | ||
= help: add `#![feature(const_extern_fn)]` to the crate attributes to enable | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
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,8 @@ | ||
extern { | ||
const fn foo(); | ||
//~^ ERROR extern items cannot be `const` | ||
const unsafe fn bar(); | ||
//~^ ERROR extern items cannot be `const` | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Btw, I was going over
parser/ty.rs
and I noticed thatparse_ty_bare_fn
has the exact same logic. Would be good to de-duplicate that.