Skip to content

Commit 46bce9f

Browse files
authored
Rollup merge of #74754 - davidhewitt:cfg-panic, r=ecstatic-morse
Add `#[cfg(panic = '...')]` This PR adds conditional compilation according to the panic strategy. I've come across a need for a flag like this a couple of times while writing tests: #74301 , #73670 (comment) I'm not sure if I need to add a feature gate for this flag?
2 parents 25f6938 + 8d43b3c commit 46bce9f

File tree

15 files changed

+136
-10
lines changed

15 files changed

+136
-10
lines changed

compiler/rustc_feature/src/active.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ declare_features! (
613613
/// Allows the use of destructuring assignments.
614614
(active, destructuring_assignment, "1.49.0", Some(71126), None),
615615

616+
/// Enables `#[cfg(panic = "...")]` config key.
617+
(active, cfg_panic, "1.49.0", Some(77443), None),
618+
616619
// -------------------------------------------------------------------------
617620
// feature-group-end: actual feature gates
618621
// -------------------------------------------------------------------------

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ symbols! {
326326
cfg_attr,
327327
cfg_attr_multi,
328328
cfg_doctest,
329+
cfg_panic,
329330
cfg_sanitize,
330331
cfg_target_feature,
331332
cfg_target_has_atomic,

compiler/rustc_target/src/spec/mod.rs

Lines changed: 8 additions & 0 deletions
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::ops::Deref;
4243
use std::path::{Path, PathBuf};
@@ -176,6 +177,13 @@ impl PanicStrategy {
176177
PanicStrategy::Abort => "abort",
177178
}
178179
}
180+
181+
pub fn desc_symbol(&self) -> Symbol {
182+
match *self {
183+
PanicStrategy::Unwind => sym::unwind,
184+
PanicStrategy::Abort => sym::abort,
185+
}
186+
}
179187
}
180188

181189
impl ToJson for PanicStrategy {
Lines changed: 38 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// build-pass
2+
// compile-flags: -C panic=unwind
3+
// ignore-emscripten no panic_unwind implementation
4+
// ignore-wasm32 no panic_unwind implementation
5+
// ignore-wasm64 no panic_unwind implementation
6+
#![feature(cfg_panic)]
7+
8+
#[cfg(panic = "abort")]
9+
pub fn bad() -> i32 { }
10+
11+
#[cfg(not(panic = "unwind"))]
12+
pub fn bad() -> i32 { }
13+
14+
#[cfg(panic = "some_imaginary_future_panic_handler")]
15+
pub fn bad() -> i32 { }
16+
17+
#[cfg(panic = "unwind")]
18+
pub fn main() { }
Lines changed: 3 additions & 3 deletions
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() {}

0 commit comments

Comments
 (0)