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

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

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
@@ -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

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::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 {
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: 14 additions & 0 deletions
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() { }
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)