Skip to content

Commit 46bce9f

Browse files
authoredNov 9, 2020
Rollup merge of rust-lang#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: rust-lang#74301 , rust-lang#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

+3
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

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

+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::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 {
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

+18
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() { }
+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

+9-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(panic = "unwind")]
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() {
@@ -22,6 +25,7 @@ fn named_argument_takes_precedence_to_captured() {
2225
assert_eq!(&s, "positional-named-captured");
2326
}
2427

28+
#[cfg(panic = "unwind")]
2529
fn panic_with_single_argument_does_not_get_formatted() {
2630
// panic! with a single argument does not perform string formatting.
2731
// RFC #2795 suggests that this may need to change so that captured arguments are formatted.
@@ -34,6 +38,7 @@ fn panic_with_single_argument_does_not_get_formatted() {
3438
assert_eq!(msg.downcast_ref::<&str>(), Some(&"{foo}"))
3539
}
3640

41+
#[cfg(panic = "unwind")]
3742
fn panic_with_multiple_arguments_is_formatted() {
3843
let foo = "captured";
3944

‎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)
Please sign in to comment.