Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&lto_args(build_runner, unit));

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Copy link
Member

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:

  • Unstable feature in Cargo.toml manifest
  • Unstable flag in CLI/config.toml

See the first few commits in #12625 for reference

}

/// Status and metadata for a single unstable feature.
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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!(
Expand Down
5 changes: 4 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation in there needs to be updated

Copy link
Member Author

Choose a reason for hiding this comment

The 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 TomlProfile::validate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if let Some(panic) = &root.panic {
if panic != "unwind" && panic != "abort" {
bail!(
"`panic` setting of `{}` is not a valid setting, \
must be `unwind` or `abort`",
panic
);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated comment always 🙇🏾‍♂️

};
Expand Down Expand Up @@ -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)
}
Expand Down
13 changes: 11 additions & 2 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Copy link
Member

Choose a reason for hiding this comment

The 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
);
}
Expand Down Expand Up @@ -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(())
}

Expand Down
19 changes: 19 additions & 0 deletions src/doc/src/reference/unstable.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be

Suggested change
cargo-features = ["immediate-abort"]
cargo-features = ["panic-immediate-abort"]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Thanks!

#16054


[package]
# ...

[profile.release]
panic = "immediate-abort"
```

## `[lints.cargo]`

* Tracking Issue: [#12235](https://github.com/rust-lang/cargo/issues/12235)
Expand Down
44 changes: 23 additions & 21 deletions tests/testsuite/cargo/z_help/stdout.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ mod proc_macro;
mod profile_config;
mod profile_custom;
mod profile_overrides;
mod profile_panic_immediate_abort;
mod profile_targets;
mod profile_trim_paths;
mod profiles;
Expand Down
121 changes: 121 additions & 0 deletions tests/testsuite/profile_panic_immediate_abort.rs
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();
}