-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 #[cfg(panic = '...')]
#74754
Merged
Merged
Add #[cfg(panic = '...')]
#74754
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# `cfg_panic` | ||
|
||
The tracking issue for this feature is: [#77443] | ||
|
||
[#77443]: https://github.com/rust-lang/rust/issues/77443 | ||
|
||
------------------------ | ||
|
||
The `cfg_panic` feature makes it possible to execute different code | ||
depending on the panic strategy. | ||
|
||
Possible values at the moment are `"unwind"` or `"abort"`, although | ||
it is possible that new panic strategies may be added to Rust in the | ||
future. | ||
|
||
## Examples | ||
|
||
```rust | ||
#![feature(cfg_panic)] | ||
|
||
#[cfg(panic = "unwind")] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
#[cfg(not(panic = "unwind"))] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
fn b() { | ||
if cfg!(panic = "abort") { | ||
// ... | ||
} else { | ||
// ... | ||
} | ||
} | ||
``` |
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,16 @@ | ||
// build-pass | ||
// compile-flags: -C panic=abort | ||
// no-prefer-dynamic | ||
#![feature(cfg_panic)] | ||
|
||
#[cfg(panic = "unwind")] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(not(panic = "abort"))] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(panic = "some_imaginary_future_panic_handler")] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(panic = "abort")] | ||
pub 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,18 @@ | ||
// build-pass | ||
// compile-flags: -C panic=unwind | ||
// ignore-emscripten no panic_unwind implementation | ||
// ignore-wasm32 no panic_unwind implementation | ||
// ignore-wasm64 no panic_unwind implementation | ||
#![feature(cfg_panic)] | ||
|
||
#[cfg(panic = "abort")] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(not(panic = "unwind"))] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(panic = "some_imaginary_future_panic_handler")] | ||
pub fn bad() -> i32 { } | ||
|
||
#[cfg(panic = "unwind")] | ||
pub fn main() { } |
File renamed without changes.
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
// Test that `assert` works when `const_panic` is enabled. | ||
|
||
// revisions: stock panic | ||
// revisions: stock const_panic | ||
|
||
#![cfg_attr(panic, feature(const_panic))] | ||
#![cfg_attr(const_panic, feature(const_panic))] | ||
|
||
const _: () = assert!(true); | ||
//[stock]~^ ERROR panicking in constants is unstable | ||
|
||
const _: () = assert!(false); | ||
//[stock]~^ ERROR panicking in constants is unstable | ||
//[panic]~^^ ERROR any use of this value will cause an error | ||
//[const_panic]~^^ ERROR any use of this value will cause an error | ||
|
||
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,11 @@ | ||
#[cfg(panic = "unwind")] | ||
//~^ ERROR `cfg(panic)` is experimental and subject to change | ||
fn foo() -> bool { true } | ||
#[cfg(not(panic = "unwind"))] | ||
//~^ ERROR `cfg(panic)` is experimental and subject to change | ||
fn foo() -> bool { false } | ||
|
||
|
||
fn main() { | ||
assert!(foo()); | ||
} |
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[E0658]: `cfg(panic)` is experimental and subject to change | ||
--> $DIR/feature-gate-cfg-panic.rs:1:7 | ||
| | ||
LL | #[cfg(panic = "unwind")] | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #77443 <https://github.com/rust-lang/rust/issues/77443> for more information | ||
= help: add `#![feature(cfg_panic)]` to the crate attributes to enable | ||
|
||
error[E0658]: `cfg(panic)` is experimental and subject to change | ||
--> $DIR/feature-gate-cfg-panic.rs:4:11 | ||
| | ||
LL | #[cfg(not(panic = "unwind"))] | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #77443 <https://github.com/rust-lang/rust/issues/77443> for more information | ||
= help: add `#![feature(cfg_panic)]` to the crate attributes to enable | ||
|
||
error: aborting due to 2 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
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
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.
From adding the feature gate I did notice that this new
cfg
predicate can of course now clash with user-setcfg
.I'm not sure if that's problematic enough to prevent stabilization, or perhaps requires that we can only stabilize with an edition?