Skip to content

Commit 4056082

Browse files
committed
Auto merge of #146317 - saethlin:panic=immediate-abort, r=nnethercote
Add panic=immediate-abort MCP: rust-lang/compiler-team#909 This adds a new panic strategy, `-Cpanic=immediate-abort`. This panic strategy essentially just codifies use of `-Zbuild-std-features=panic_immediate_abort`. This PR is intended to just set up infrastructure, and while it will change how the compiler is invoked for users of the feature, there should be no other impacts. In many parts of the compiler, `PanicStrategy::ImmediateAbort` behaves just like `PanicStrategy::Abort`, because actually most parts of the compiler just mean to ask "can this unwind?" so I've added a helper function so we can say `sess.panic_strategy().unwinds()`. The panic and unwind strategies have some level of compatibility, which mostly means that we can pre-compile the sysroot with unwinding panics then the sysroot can be linked with aborting panics later. The immediate-abort strategy is all-or-nothing, enforced by `compiler/rustc_metadata/src/dependency_format.rs` and this is tested for in `tests/ui/panic-runtime/`. We could _technically_ be more compatible with the other panic strategies, but immediately-aborting panics primarily exist for users who want to eliminate all the code size responsible for the panic runtime. I'm open to other use cases if people want to present them, but not right now. This PR is already large. `-Cpanic=immediate-abort` sets both `cfg(panic = "immediate-abort")` _and_ `cfg(panic = "abort")`. bjorn3 pointed out that people may be checking for the abort cfg to ask if panics will unwind, and also the sysroot feature this is replacing used to require `-Cpanic=abort` so this seems like a good back-compat step. At least for the moment. Unclear if this is a good idea indefinitely. I can imagine this being confusing. The changes to the standard library attributes are purely mechanical. Apart from that, I removed an `unsafe` we haven't needed for a while since the `abort` intrinsic became safe, and I've added a helpful diagnostic for people trying to use the old feature. To test that `-Cpanic=immediate-abort` conflicts with other panic strategies, I've beefed up the core-stubs infrastructure a bit. There is now a separate attribute to set flags on it. I've added a test that this produces the desired codegen, called `tests/run-make-cargo/panic-immediate-abort-codegen/` and also a separate run-make-cargo test that checks that we can build a binary.
2 parents f6092f2 + 4c1595a commit 4056082

File tree

74 files changed

+561
-157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+561
-157
lines changed

compiler/rustc_builtin_macros/src/test_harness.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ pub fn inject(
6363

6464
if sess.is_test_crate() {
6565
let panic_strategy = match (panic_strategy, sess.opts.unstable_opts.panic_abort_tests) {
66-
(PanicStrategy::Abort, true) => PanicStrategy::Abort,
67-
(PanicStrategy::Abort, false) => {
66+
(PanicStrategy::Abort | PanicStrategy::ImmediateAbort, true) => panic_strategy,
67+
(PanicStrategy::Abort | PanicStrategy::ImmediateAbort, false) => {
6868
if panic_strategy == platform_panic_strategy {
6969
// Silently allow compiling with panic=abort on these platforms,
7070
// but with old behavior (abort if a test fails).
@@ -287,10 +287,8 @@ fn mk_main(cx: &mut TestCtxt<'_>) -> Box<ast::Item> {
287287
let ecx = &cx.ext_cx;
288288
let test_ident = Ident::new(sym::test, sp);
289289

290-
let runner_name = match cx.panic_strategy {
291-
PanicStrategy::Unwind => "test_main_static",
292-
PanicStrategy::Abort => "test_main_static_abort",
293-
};
290+
let runner_name =
291+
if cx.panic_strategy.unwinds() { "test_main_static" } else { "test_main_static_abort" };
294292

295293
// test::test_main_static(...)
296294
let mut test_runner = cx.test_runner.clone().unwrap_or_else(|| {

compiler/rustc_codegen_gcc/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use rustc_middle::mir::mono::Visibility;
1515
use rustc_middle::ty::TyCtxt;
1616
use rustc_session::config::DebugInfo;
1717
use rustc_span::Symbol;
18+
use rustc_target::spec::RelocModel;
1819
#[cfg(feature = "master")]
1920
use rustc_target::spec::SymbolVisibility;
20-
use rustc_target::spec::{PanicStrategy, RelocModel};
2121

2222
use crate::builder::Builder;
2323
use crate::context::CodegenCx;
@@ -101,7 +101,7 @@ pub fn compile_codegen_unit(
101101
// Instantiate monomorphizations without filling out definitions yet...
102102
let context = new_context(tcx);
103103

104-
if tcx.sess.panic_strategy() == PanicStrategy::Unwind {
104+
if tcx.sess.panic_strategy().unwinds() {
105105
context.add_command_line_option("-fexceptions");
106106
context.add_driver_option("-fexceptions");
107107
}

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_middle::ty::layout::LayoutOf;
2929
use rustc_middle::ty::{self, Instance, Ty};
3030
use rustc_span::{Span, Symbol, sym};
3131
use rustc_target::callconv::{ArgAbi, PassMode};
32-
use rustc_target::spec::PanicStrategy;
3332

3433
#[cfg(feature = "master")]
3534
use crate::abi::FnAbiGccExt;
@@ -1334,7 +1333,7 @@ fn try_intrinsic<'a, 'b, 'gcc, 'tcx>(
13341333
_catch_func: RValue<'gcc>,
13351334
dest: PlaceRef<'tcx, RValue<'gcc>>,
13361335
) {
1337-
if bx.sess().panic_strategy() == PanicStrategy::Abort {
1336+
if !bx.sess().panic_strategy().unwinds() {
13381337
bx.call(bx.type_void(), None, None, try_func, &[data], None, None);
13391338
// Return 0 unconditionally from the intrinsic call;
13401339
// we can never unwind.

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_middle::{bug, span_bug};
1818
use rustc_span::{Span, Symbol, sym};
1919
use rustc_symbol_mangling::{mangle_internal_symbol, symbol_name_for_instance_in_crate};
2020
use rustc_target::callconv::PassMode;
21-
use rustc_target::spec::PanicStrategy;
2221
use tracing::debug;
2322

2423
use crate::abi::FnAbiLlvmExt;
@@ -674,7 +673,7 @@ fn catch_unwind_intrinsic<'ll, 'tcx>(
674673
catch_func: &'ll Value,
675674
dest: PlaceRef<'tcx, &'ll Value>,
676675
) {
677-
if bx.sess().panic_strategy() == PanicStrategy::Abort {
676+
if !bx.sess().panic_strategy().unwinds() {
678677
let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
679678
bx.call(try_func_ty, None, None, try_func, &[data], None, None);
680679
// Return 0 unconditionally from the intrinsic call;

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ unsafe fn configure_llvm(sess: &Session) {
106106

107107
if sess.target.os == "emscripten"
108108
&& !sess.opts.unstable_opts.emscripten_wasm_eh
109-
&& sess.panic_strategy() == PanicStrategy::Unwind
109+
&& sess.panic_strategy().unwinds()
110110
{
111111
add("-enable-emscripten-cxx-exceptions", false);
112112
}

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ use rustc_span::Symbol;
4747
use rustc_target::spec::crt_objects::CrtObjects;
4848
use rustc_target::spec::{
4949
BinaryFormat, Cc, LinkOutputKind, LinkSelfContainedComponents, LinkSelfContainedDefault,
50-
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, PanicStrategy, RelocModel, RelroLevel,
51-
SanitizerSet, SplitDebuginfo,
50+
LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, RelocModel, RelroLevel, SanitizerSet,
51+
SplitDebuginfo,
5252
};
5353
use tracing::{debug, info, warn};
5454

@@ -2512,10 +2512,10 @@ fn add_order_independent_options(
25122512
if sess.target.os == "emscripten" {
25132513
cmd.cc_arg(if sess.opts.unstable_opts.emscripten_wasm_eh {
25142514
"-fwasm-exceptions"
2515-
} else if sess.panic_strategy() == PanicStrategy::Abort {
2516-
"-sDISABLE_EXCEPTION_CATCHING=1"
2517-
} else {
2515+
} else if sess.panic_strategy().unwinds() {
25182516
"-sDISABLE_EXCEPTION_CATCHING=0"
2517+
} else {
2518+
"-sDISABLE_EXCEPTION_CATCHING=1"
25192519
});
25202520
}
25212521

compiler/rustc_interface/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ interface_out_dir_error =
4747
failed to find or create the directory specified by `--out-dir`
4848
4949
interface_proc_macro_crate_panic_abort =
50-
building proc macro crate with `panic=abort` may crash the compiler should the proc-macro panic
50+
building proc macro crate with `panic=abort` or `panic=immediate-abort` may crash the compiler should the proc-macro panic
5151
5252
interface_temps_dir_error =
5353
failed to find or create the directory specified by `--temps-dir`

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use rustc_span::{
4242
DUMMY_SP, ErrorGuaranteed, ExpnKind, FileName, SourceFileHash, SourceFileHashAlgorithm, Span,
4343
Symbol, sym,
4444
};
45-
use rustc_target::spec::PanicStrategy;
4645
use rustc_trait_selection::{solve, traits};
4746
use tracing::{info, instrument};
4847

@@ -282,7 +281,7 @@ fn configure_and_expand(
282281
feature_err(sess, sym::export_stable, DUMMY_SP, "`sdylib` crate type is unstable").emit();
283282
}
284283

285-
if is_proc_macro_crate && sess.panic_strategy() == PanicStrategy::Abort {
284+
if is_proc_macro_crate && !sess.panic_strategy().unwinds() {
286285
sess.dcx().emit_warn(errors::ProcMacroCratePanicAbort);
287286
}
288287

compiler/rustc_metadata/messages.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ metadata_full_metadata_not_found =
9898
metadata_global_alloc_required =
9999
no global memory allocator found but one is required; link to std or add `#[global_allocator]` to a static item that implements the GlobalAlloc trait
100100
101+
metadata_incompatible_with_immediate_abort =
102+
the crate `{$crate_name}` was compiled with a panic strategy which is incompatible with `immediate-abort`
103+
104+
metadata_incompatible_with_immediate_abort_core =
105+
the crate `core` was compiled with a panic strategy which is incompatible with `immediate-abort`
106+
.help = consider building the standard library from source with `cargo build -Zbuild-std`
107+
101108
metadata_incompatible_panic_in_drop_strategy =
102109
the crate `{$crate_name}` is compiled with the panic-in-drop strategy `{$found_strategy}` which is incompatible with this crate's strategy of `{$desired_strategy}`
103110

compiler/rustc_metadata/src/creader.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@ impl CStore {
10271027
let name = match desired_strategy {
10281028
PanicStrategy::Unwind => sym::panic_unwind,
10291029
PanicStrategy::Abort => sym::panic_abort,
1030+
PanicStrategy::ImmediateAbort => {
1031+
// Immediate-aborting panics don't use a runtime.
1032+
return;
1033+
}
10301034
};
10311035
info!("panic runtime not found -- loading {}", name);
10321036

0 commit comments

Comments
 (0)