From 4cb6fe41845e5c3f80130cbd530cba4690203586 Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Thu, 2 Oct 2025 20:05:51 -0400 Subject: [PATCH] Add panic=immediate-abort support to Cargo --- src/cargo/core/compiler/mod.rs | 5 +- src/cargo/core/features.rs | 5 + src/cargo/core/profiles.rs | 5 +- src/cargo/util/toml/mod.rs | 13 +- src/doc/src/reference/unstable.md | 19 +++ tests/testsuite/cargo/z_help/stdout.term.svg | 44 ++++--- tests/testsuite/main.rs | 1 + .../profile_panic_immediate_abort.rs | 121 ++++++++++++++++++ 8 files changed, 188 insertions(+), 25 deletions(-) create mode 100644 tests/testsuite/profile_panic_immediate_abort.rs diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 510b2d539e6..38bf7e83f96 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -1234,6 +1234,9 @@ fn build_base_args( if *panic != PanicStrategy::Unwind { cmd.arg("-C").arg(format!("panic={}", panic)); } + if *panic == PanicStrategy::ImmediateAbort { + cmd.arg("-Z").arg("unstable-options"); + } cmd.args(<o_args(build_runner, unit)); @@ -1305,7 +1308,7 @@ fn build_base_args( // flag to pass to rustc, so register that here. Eventually this flag // will simply not be needed when the behavior is stabilized in the Rust // compiler itself. - if *panic == PanicStrategy::Abort { + if *panic == PanicStrategy::Abort || *panic == PanicStrategy::ImmediateAbort { cmd.arg("-Z").arg("panic-abort-tests"); } } else if test { diff --git a/src/cargo/core/features.rs b/src/cargo/core/features.rs index f34f12ccddd..0abd8bd2068 100644 --- a/src/cargo/core/features.rs +++ b/src/cargo/core/features.rs @@ -579,6 +579,9 @@ features! { /// Allows use of multiple build scripts. (unstable, multiple_build_scripts, "", "reference/unstable.html#multiple-build-scripts"), + + /// Allows use of panic="immediate-abort". + (unstable, panic_immediate_abort, "", "reference/unstable.html#panic-immediate-abort"), } /// Status and metadata for a single unstable feature. @@ -872,6 +875,7 @@ unstable_cli_options!( no_embed_metadata: bool = ("Avoid embedding metadata in library artifacts"), no_index_update: bool = ("Do not update the registry index even if the cache is outdated"), panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"), + panic_immediate_abort: bool = ("Enable setting `panic = \"immediate-abort\"` in profiles"), profile_hint_mostly_unused: bool = ("Enable the `hint-mostly-unused` setting in profiles to mark a crate as mostly unused."), profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"), public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"), @@ -1417,6 +1421,7 @@ impl CliUnstable { "skip-rustdoc-fingerprint" => self.skip_rustdoc_fingerprint = parse_empty(k, v)?, "script" => self.script = parse_empty(k, v)?, "target-applies-to-host" => self.target_applies_to_host = parse_empty(k, v)?, + "panic-immediate-abort" => self.panic_immediate_abort = parse_empty(k, v)?, "unstable-options" => self.unstable_options = parse_empty(k, v)?, "warnings" => self.warnings = parse_empty(k, v)?, _ => bail!( diff --git a/src/cargo/core/profiles.rs b/src/cargo/core/profiles.rs index 3c4c457c158..9fe7400b85c 100644 --- a/src/cargo/core/profiles.rs +++ b/src/cargo/core/profiles.rs @@ -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), }; @@ -872,10 +873,11 @@ 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 { @@ -883,6 +885,7 @@ impl fmt::Display for PanicStrategy { match *self { PanicStrategy::Unwind => "unwind", PanicStrategy::Abort => "abort", + PanicStrategy::ImmediateAbort => "immediate-abort", } .fmt(f) } diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index f6954cd52f2..7070f0bf955 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -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" { 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(()) } diff --git a/src/doc/src/reference/unstable.md b/src/doc/src/reference/unstable.md index afda1402d63..dacadcf2916 100644 --- a/src/doc/src/reference/unstable.md +++ b/src/doc/src/reference/unstable.md @@ -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"] + +[package] +# ... + +[profile.release] +panic = "immediate-abort" +``` + ## `[lints.cargo]` * Tracking Issue: [#12235](https://github.com/rust-lang/cargo/issues/12235) diff --git a/tests/testsuite/cargo/z_help/stdout.term.svg b/tests/testsuite/cargo/z_help/stdout.term.svg index f48d5c64e5f..86cebba751f 100644 --- a/tests/testsuite/cargo/z_help/stdout.term.svg +++ b/tests/testsuite/cargo/z_help/stdout.term.svg @@ -1,4 +1,4 @@ - +