-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add panic=immediate-abort support to Cargo #16041
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -561,6 +561,7 @@ fn merge_profile(profile: &mut Profile, toml: &TomlProfile) { | |||||||||||||||||||
profile.panic = match panic.as_str() { | ||||||||||||||||||||
"unwind" => PanicStrategy::Unwind, | ||||||||||||||||||||
"abort" => PanicStrategy::Abort, | ||||||||||||||||||||
"immediate-abort" => PanicStrategy::ImmediateAbort, | ||||||||||||||||||||
// This should be validated in TomlProfile::validate | ||||||||||||||||||||
_ => panic!("Unexpected panic setting `{}`", panic), | ||||||||||||||||||||
Comment on lines
+564
to
566
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The validation in there needs to be updated There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm afraid I'm lost. What validation? I can't find any validation of the panic setting or a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cargo/src/cargo/util/toml/mod.rs Lines 2552 to 2560 in 56bdf49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outdated comment always 🙇🏾♂️ |
||||||||||||||||||||
}; | ||||||||||||||||||||
|
@@ -872,17 +873,19 @@ impl serde::ser::Serialize for Lto { | |||||||||||||||||||
|
||||||||||||||||||||
/// The `panic` setting. | ||||||||||||||||||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash, PartialOrd, Ord, serde::Serialize)] | ||||||||||||||||||||
#[serde(rename_all = "lowercase")] | ||||||||||||||||||||
#[serde(rename_all = "kebab-case")] | ||||||||||||||||||||
pub enum PanicStrategy { | ||||||||||||||||||||
Unwind, | ||||||||||||||||||||
Abort, | ||||||||||||||||||||
ImmediateAbort, | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
impl fmt::Display for PanicStrategy { | ||||||||||||||||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||||||||||||||||||
match *self { | ||||||||||||||||||||
PanicStrategy::Unwind => "unwind", | ||||||||||||||||||||
PanicStrategy::Abort => "abort", | ||||||||||||||||||||
PanicStrategy::ImmediateAbort => "immediate-abort", | ||||||||||||||||||||
} | ||||||||||||||||||||
.fmt(f) | ||||||||||||||||||||
} | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2550,10 +2550,10 @@ pub fn validate_profile( | |
} | ||
|
||
if let Some(panic) = &root.panic { | ||
if panic != "unwind" && panic != "abort" { | ||
if panic != "unwind" && panic != "abort" && panic != "immediate-abort" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we want to make it a warning or configurable lint in the future. It seems too restrictive. Anyway, it is worth its own separate topic. |
||
bail!( | ||
"`panic` setting of `{}` is not a valid setting, \ | ||
must be `unwind` or `abort`", | ||
must be `unwind`, `abort`, or `immediate-abort`.", | ||
panic | ||
); | ||
} | ||
|
@@ -2607,6 +2607,15 @@ fn validate_profile_layer( | |
_ => {} | ||
} | ||
} | ||
if profile.panic.as_deref() == Some("immediate-abort") { | ||
match ( | ||
features.require(Feature::panic_immediate_abort()), | ||
cli_unstable.panic_immediate_abort, | ||
) { | ||
(Err(e), false) => return Err(e), | ||
_ => {} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -95,6 +95,7 @@ Each new feature described below should explain how to use it. | |||||
* [target-applies-to-host](#target-applies-to-host) --- Alters whether certain flags will be passed to host build targets. | ||||||
* [gc](#gc) --- Global cache garbage collection. | ||||||
* [open-namespaces](#open-namespaces) --- Allow multiple packages to participate in the same API namespace | ||||||
* [panic-immediate-abort](#panic-immediate-abort) --- Passes `-Cpanic=immediate-abort` to the compiler. | ||||||
* rustdoc | ||||||
* [rustdoc-map](#rustdoc-map) --- Provides mappings for documentation to link to external sites like [docs.rs](https://docs.rs/). | ||||||
* [scrape-examples](#scrape-examples) --- Shows examples within documentation. | ||||||
|
@@ -1670,6 +1671,24 @@ cargo-features = ["open-namespaces"] | |||||
# ... | ||||||
``` | ||||||
|
||||||
## panic-immediate-abort | ||||||
|
||||||
* Tracking Issue: [#16042](https://github.com/rust-lang/cargo/issues/16042) | ||||||
* Upstream Tracking Issue: [rust-lang/rust#147286](https://github.com/rust-lang/rust/issues/147286) | ||||||
|
||||||
Allow use of [`-Cpanic=immediate-abort`](../../rustc/codegen-options/index.html#panic) through a Cargo profile | ||||||
|
||||||
This can be enabled like so: | ||||||
```toml | ||||||
cargo-features = ["immediate-abort"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Thanks! |
||||||
|
||||||
[package] | ||||||
# ... | ||||||
|
||||||
[profile.release] | ||||||
panic = "immediate-abort" | ||||||
``` | ||||||
|
||||||
## `[lints.cargo]` | ||||||
|
||||||
* Tracking Issue: [#12235](https://github.com/rust-lang/cargo/issues/12235) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//! Tests for `panic = "immediate-abort"`. | ||
|
||
use crate::prelude::*; | ||
use cargo_test_support::project; | ||
use cargo_test_support::str; | ||
|
||
#[cargo_test] | ||
fn gated_manifest() { | ||
let p = project() | ||
.file( | ||
"Cargo.toml", | ||
r#" | ||
[package] | ||
name = "foo" | ||
version = "0.0.1" | ||
edition = "2015" | ||
|
||
[profile.dev] | ||
panic = "immediate-abort" | ||
"#, | ||
) | ||
.file("src/lib.rs", "") | ||
.build(); | ||
|
||
p.cargo("check") | ||
.masquerade_as_nightly_cargo(&["panic-immediate-abort"]) | ||
.with_status(101) | ||
.with_stderr_data(str![[r#" | ||
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml` | ||
|
||
Caused by: | ||
feature `panic-immediate-abort` is required | ||
... | ||
"#]]) | ||
.run(); | ||
} | ||
|
||
#[cargo_test] | ||
fn gated_config_toml() { | ||
let p = project() | ||
.file( | ||
".cargo/config.toml", | ||
r#" | ||
[profile.dev] | ||
panic = "immediate-abort" | ||
"#, | ||
) | ||
.file("src/lib.rs", "") | ||
.build(); | ||
|
||
p.cargo("check") | ||
.masquerade_as_nightly_cargo(&["panic-immediate-abort"]) | ||
.with_status(101) | ||
.with_stderr_data(str![[r#" | ||
[ERROR] config profile `dev` is not valid (defined in `[ROOT]/foo/.cargo/config.toml`) | ||
|
||
Caused by: | ||
feature `panic-immediate-abort` is required | ||
... | ||
"#]]) | ||
.run(); | ||
} | ||
|
||
#[cargo_test(nightly, reason = "-Cpanic=immediate-abort is unstable")] | ||
fn manifest_gate_works() { | ||
let p = project() | ||
.file( | ||
"Cargo.toml", | ||
r#" | ||
cargo-features = ["panic-immediate-abort"] | ||
[package] | ||
name = "foo" | ||
version = "0.0.1" | ||
edition = "2015" | ||
|
||
[profile.dev] | ||
panic = "immediate-abort" | ||
"#, | ||
) | ||
.file("src/lib.rs", "") | ||
.build(); | ||
|
||
p.cargo("build --verbose") | ||
.masquerade_as_nightly_cargo(&["panic-immediate-abort"]) | ||
.with_stderr_data(str![[r#" | ||
[COMPILING] foo v0.0.1 ([ROOT]/foo) | ||
[RUNNING] `rustc [..]-C panic=immediate-abort -Z unstable-options[..]` | ||
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
||
"#]]) | ||
.run(); | ||
} | ||
|
||
#[cargo_test(nightly, reason = "-Cpanic=immediate-abort is unstable")] | ||
fn cli_gate_works() { | ||
let p = project() | ||
.file( | ||
"Cargo.toml", | ||
r#" | ||
[package] | ||
name = "foo" | ||
version = "0.0.1" | ||
edition = "2015" | ||
|
||
[profile.dev] | ||
panic = "immediate-abort" | ||
"#, | ||
) | ||
.file("src/lib.rs", "") | ||
.build(); | ||
|
||
p.cargo("build --verbose -Z panic-immediate-abort") | ||
.masquerade_as_nightly_cargo(&["panic-immediate-abort"]) | ||
.with_stderr_data(str![[r#" | ||
[COMPILING] foo v0.0.1 ([ROOT]/foo) | ||
[RUNNING] `rustc [..]-C panic=immediate-abort -Z unstable-options[..]` | ||
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s | ||
|
||
"#]]) | ||
.run(); | ||
} |
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.
As the
[profile]
table is also supported in Cargo configuration, you might want to add both feature gates:See the first few commits in #12625 for reference