Skip to content

Commit 3e6acd8

Browse files
committed
Add #[cfg(panic = "...")]
1 parent 2e8a54a commit 3e6acd8

File tree

15 files changed

+130
-10
lines changed

15 files changed

+130
-10
lines changed

compiler/rustc_feature/src/active.rs

+3
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@ declare_features! (
601601
/// Allow anonymous constants from an inline `const` block
602602
(active, inline_const, "1.49.0", Some(76001), None),
603603

604+
/// Enables `#[cfg(panic = "...")]` config key.
605+
(active, cfg_panic, "1.49.0", Some(77443), None),
606+
604607
// -------------------------------------------------------------------------
605608
// feature-group-end: actual feature gates
606609
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const GATED_CFGS: &[GatedCfg] = &[
3333
),
3434
(sym::sanitize, sym::cfg_sanitize, cfg_fn!(cfg_sanitize)),
3535
(sym::version, sym::cfg_version, cfg_fn!(cfg_version)),
36+
(sym::panic, sym::cfg_panic, cfg_fn!(cfg_panic)),
3637
];
3738

3839
/// Find a gated cfg determined by the `pred`icate which is given the cfg's name.

compiler/rustc_session/src/config.rs

+3
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,9 @@ pub fn default_configuration(sess: &Session) -> CrateConfig {
793793
}
794794
}
795795

796+
let panic_strategy = sess.panic_strategy();
797+
ret.insert((sym::panic, Some(panic_strategy.desc_symbol())));
798+
796799
for s in sess.opts.debugging_opts.sanitizer {
797800
let symbol = Symbol::intern(&s.to_string());
798801
ret.insert((sym::sanitize, Some(symbol)));

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ symbols! {
323323
cfg_attr,
324324
cfg_attr_multi,
325325
cfg_doctest,
326+
cfg_panic,
326327
cfg_sanitize,
327328
cfg_target_feature,
328329
cfg_target_has_atomic,

compiler/rustc_target/src/spec/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use crate::spec::abi::{lookup as lookup_abi, Abi};
3838
use crate::spec::crt_objects::{CrtObjects, CrtObjectsFallback};
3939
use rustc_serialize::json::{Json, ToJson};
40+
use rustc_span::symbol::{sym, Symbol};
4041
use std::collections::BTreeMap;
4142
use std::path::{Path, PathBuf};
4243
use std::str::FromStr;
@@ -174,6 +175,13 @@ impl PanicStrategy {
174175
PanicStrategy::Abort => "abort",
175176
}
176177
}
178+
179+
pub fn desc_symbol(&self) -> Symbol {
180+
match *self {
181+
PanicStrategy::Unwind => sym::unwind,
182+
PanicStrategy::Abort => sym::abort,
183+
}
184+
}
177185
}
178186

179187
impl ToJson for PanicStrategy {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# `cfg_panic`
2+
3+
The tracking issue for this feature is: [#77443]
4+
5+
[#77443]: https://github.com/rust-lang/rust/issues/77443
6+
7+
------------------------
8+
9+
The `cfg_panic` feature makes it possible to execute different code
10+
depending on the panic strategy.
11+
12+
Possible values at the moment are `"unwind"` or `"abort"`, although
13+
it is possible that new panic strategies may be added to Rust in the
14+
future.
15+
16+
## Examples
17+
18+
```rust
19+
#![feature(cfg_panic)]
20+
21+
#[cfg(panic = "unwind")]
22+
fn a() {
23+
// ...
24+
}
25+
26+
#[cfg(not(panic = "unwind"))]
27+
fn a() {
28+
// ...
29+
}
30+
31+
fn b() {
32+
if cfg!(panic = "abort") {
33+
// ...
34+
} else {
35+
// ...
36+
}
37+
}
38+
```

src/test/ui/cfg/cfg-panic-abort.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// build-pass
2+
// compile-flags: -C panic=abort
3+
// no-prefer-dynamic
4+
#![feature(cfg_panic)]
5+
6+
#[cfg(panic = "unwind")]
7+
pub fn bad() -> i32 { }
8+
9+
#[cfg(not(panic = "abort"))]
10+
pub fn bad() -> i32 { }
11+
12+
#[cfg(panic = "some_imaginary_future_panic_handler")]
13+
pub fn bad() -> i32 { }
14+
15+
#[cfg(panic = "abort")]
16+
pub fn main() { }

src/test/ui/cfg/cfg-panic.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// build-pass
2+
#![feature(cfg_panic)]
3+
4+
#[cfg(panic = "abort")]
5+
pub fn bad() -> i32 { }
6+
7+
#[cfg(not(panic = "unwind"))]
8+
pub fn bad() -> i32 { }
9+
10+
#[cfg(panic = "some_imaginary_future_panic_handler")]
11+
pub fn bad() -> i32 { }
12+
13+
#[cfg(panic = "unwind")]
14+
pub fn main() { }
+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Test that `assert` works when `const_panic` is enabled.
22

3-
// revisions: stock panic
3+
// revisions: stock const_panic
44

5-
#![cfg_attr(panic, feature(const_panic))]
5+
#![cfg_attr(const_panic, feature(const_panic))]
66

77
const _: () = assert!(true);
88
//[stock]~^ ERROR panicking in constants is unstable
99

1010
const _: () = assert!(false);
1111
//[stock]~^ ERROR panicking in constants is unstable
12-
//[panic]~^^ ERROR any use of this value will cause an error
12+
//[const_panic]~^^ ERROR any use of this value will cause an error
1313

1414
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[cfg(panic = "unwind")]
2+
//~^ ERROR `cfg(panic)` is experimental and subject to change
3+
fn foo() -> bool { true }
4+
#[cfg(not(panic = "unwind"))]
5+
//~^ ERROR `cfg(panic)` is experimental and subject to change
6+
fn foo() -> bool { false }
7+
8+
9+
fn main() {
10+
assert!(foo());
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0658]: `cfg(panic)` is experimental and subject to change
2+
--> $DIR/feature-gate-cfg-panic.rs:1:7
3+
|
4+
LL | #[cfg(panic = "unwind")]
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #77443 <https://github.com/rust-lang/rust/issues/77443> for more information
8+
= help: add `#![feature(cfg_panic)]` to the crate attributes to enable
9+
10+
error[E0658]: `cfg(panic)` is experimental and subject to change
11+
--> $DIR/feature-gate-cfg-panic.rs:4:11
12+
|
13+
LL | #[cfg(not(panic = "unwind"))]
14+
| ^^^^^^^^^^^^^^^^
15+
|
16+
= note: see issue #77443 <https://github.com/rust-lang/rust/issues/77443> for more information
17+
= help: add `#![feature(cfg_panic)]` to the crate attributes to enable
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0658`.

src/test/ui/fmt/format-args-capture.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// run-pass
2-
// ignore-wasm32
3-
// ignore-wasm64
42
#![feature(format_args_capture)]
3+
#![feature(cfg_panic)]
54

65
fn main() {
76
named_argument_takes_precedence_to_captured();
8-
panic_with_single_argument_does_not_get_formatted();
9-
panic_with_multiple_arguments_is_formatted();
107
formatting_parameters_can_be_captured();
8+
9+
#[cfg(not(panic = "abort"))]
10+
{
11+
panic_with_single_argument_does_not_get_formatted();
12+
panic_with_multiple_arguments_is_formatted();
13+
}
1114
}
1215

1316
fn named_argument_takes_precedence_to_captured() {

src/test/ui/issues/issue-68696-catch-during-unwind.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// entering the catch_unwind.
55
//
66
// run-pass
7-
// ignore-wasm no panic support
8-
// ignore-emscripten no panic support
7+
#![feature(cfg_panic)]
98

109
use std::panic::catch_unwind;
1110

@@ -19,6 +18,7 @@ impl Drop for Guard {
1918
}
2019

2120
fn main() {
21+
#[cfg(panic = "unwind")]
2222
let _ = catch_unwind(|| {
2323
let _guard = Guard::default();
2424
panic!();

src/test/ui/test-attrs/test-allow-fail-attr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// run-pass
2-
// ignore-wasm32-bare compiled with panic=abort by default
32
// compile-flags: --test
43
#![feature(allow_fail)]
4+
#![feature(cfg_panic)]
55

66
#[test]
77
#[allow_fail]
88
fn test1() {
9+
#[cfg(not(panic = "abort"))]
910
panic!();
1011
}
1112

0 commit comments

Comments
 (0)