From b923d1e13ade0197b11a670a296a38ddfebca469 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Wed, 30 Sep 2026 22:03:17 -0500 Subject: [PATCH 1/2] ctfe: Constify the `abort` intrinsic This is a well-defined operation that we want to make use of in std/core, such as with `abort_immediate`. --- .../rustc_const_eval/src/const_eval/error.rs | 2 ++ .../src/const_eval/machine.rs | 6 +++- library/core/src/intrinsics/mod.rs | 2 +- src/tools/miri/src/intrinsics/mod.rs | 1 + tests/ui/consts/const-eval/const_panic.rs | 5 +++ tests/ui/consts/const-eval/const_panic.stderr | 32 +++++++++++-------- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_const_eval/src/const_eval/error.rs b/compiler/rustc_const_eval/src/const_eval/error.rs index 8532cad91b808..1cb6a8aed947d 100644 --- a/compiler/rustc_const_eval/src/const_eval/error.rs +++ b/compiler/rustc_const_eval/src/const_eval/error.rs @@ -30,6 +30,7 @@ pub enum ConstEvalErrKind { col: u32, file: Symbol, }, + Abort, WriteThroughImmutablePointer, /// Called `const_make_global` twice. ConstMakeGlobalPtrAlreadyMadeGlobal(AllocId), @@ -51,6 +52,7 @@ impl fmt::Display for ConstEvalErrKind { write!(f, "modifying a static's initial value from another static's initializer") } Panic { msg, .. } => write!(f, "evaluation panicked: {msg}"), + Abort => write!(f, "the program aborted execution"), RecursiveStatic => { write!(f, "encountered static that tried to access itself during initialization") } diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs index 843cd7cb4930d..3f7731ce95583 100644 --- a/compiler/rustc_const_eval/src/const_eval/machine.rs +++ b/compiler/rustc_const_eval/src/const_eval/machine.rs @@ -10,13 +10,13 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{CRATE_HIR_ID, HirId}; use rustc_lint_defs::builtin::LONG_RUNNING_CONST_EVAL; -use rustc_middle::mir; use rustc_middle::mir::AssertMessage; use rustc_middle::mir::interpret::ReportedErrorInfo; use rustc_middle::query::TyCtxtAt; use rustc_middle::ty::consts::ConstExt; use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, TyAndLayout, ValidityRequirement}; use rustc_middle::ty::{self, FieldInfo, ScalarInt, Ty, TyCtxt}; +use rustc_middle::{mir, throw_machine_stop}; use rustc_span::{Span, Symbol, bug, span_bug, sym}; use rustc_target::callconv::FnAbi; use tracing::debug; @@ -484,6 +484,10 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> { // CTFE-specific intrinsics. match intrinsic_name { + sym::abort => { + // Note that `abort` is also hooked separately in Miri. + throw_machine_stop!(ConstEvalErrKind::Abort); + } sym::ptr_guaranteed_cmp => { let a = ecx.read_scalar(&args[0])?; let b = ecx.read_scalar(&args[1])?; diff --git a/library/core/src/intrinsics/mod.rs b/library/core/src/intrinsics/mod.rs index 96999f154c42f..f44da9840b8e5 100644 --- a/library/core/src/intrinsics/mod.rs +++ b/library/core/src/intrinsics/mod.rs @@ -444,7 +444,7 @@ pub fn rustc_peek(_: T) -> T; /// The stabilization-track version of this intrinsic is [`core::process::abort_immediate`]. #[rustc_nounwind] #[rustc_intrinsic] -pub fn abort() -> !; +pub const fn abort() -> !; /// Informs the optimizer that this point in the code is not reachable, /// enabling further optimizations. diff --git a/src/tools/miri/src/intrinsics/mod.rs b/src/tools/miri/src/intrinsics/mod.rs index 9a6bdb9b48dce..997339f66a51c 100644 --- a/src/tools/miri/src/intrinsics/mod.rs +++ b/src/tools/miri/src/intrinsics/mod.rs @@ -96,6 +96,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { match intrinsic_name { // Basic control flow "abort" => { + // Note that `abort` is also hooked separately in CTFE. throw_machine_stop!(TerminationInfo::Abort( "the program aborted execution".to_owned() )); diff --git a/tests/ui/consts/const-eval/const_panic.rs b/tests/ui/consts/const-eval/const_panic.rs index e4f3a4eec1b1c..26eeec7778f7c 100644 --- a/tests/ui/consts/const-eval/const_panic.rs +++ b/tests/ui/consts/const-eval/const_panic.rs @@ -1,4 +1,5 @@ //@ edition:2015..2021 +#![feature(core_intrinsics)] #![allow(non_fmt_panics)] #![crate_type = "lib"] @@ -39,3 +40,7 @@ const W_CORE: () = core::panic!(MSG); const W2_CORE: () = core::panic!("{}", MSG); //~^ ERROR evaluation panicked + +// FIXME(abort_immediate): replace with the non-intrinsic version once available +const ABORT: () = core::intrinsics::abort(); +//~^ ERROR the program aborted execution diff --git a/tests/ui/consts/const-eval/const_panic.stderr b/tests/ui/consts/const-eval/const_panic.stderr index 8e0384b2f2af6..1288112f462fe 100644 --- a/tests/ui/consts/const-eval/const_panic.stderr +++ b/tests/ui/consts/const-eval/const_panic.stderr @@ -1,75 +1,81 @@ error[E0080]: evaluation panicked: cheese - --> $DIR/const_panic.rs:7:15 + --> $DIR/const_panic.rs:8:15 | LL | const Z: () = std::panic!("cheese"); | ^^^^^^^^^^^^^^^^^^^^^ evaluation of `Z` failed here error[E0080]: evaluation panicked: explicit panic - --> $DIR/const_panic.rs:10:16 + --> $DIR/const_panic.rs:11:16 | LL | const Z2: () = std::panic!(); | ^^^^^^^^^^^^^ evaluation of `Z2` failed here error[E0080]: evaluation panicked: internal error: entered unreachable code - --> $DIR/const_panic.rs:13:15 + --> $DIR/const_panic.rs:14:15 | LL | const Y: () = std::unreachable!(); | ^^^^^^^^^^^^^^^^^^^ evaluation of `Y` failed here error[E0080]: evaluation panicked: not implemented - --> $DIR/const_panic.rs:16:15 + --> $DIR/const_panic.rs:17:15 | LL | const X: () = std::unimplemented!(); | ^^^^^^^^^^^^^^^^^^^^^ evaluation of `X` failed here error[E0080]: evaluation panicked: hello - --> $DIR/const_panic.rs:19:15 + --> $DIR/const_panic.rs:20:15 | LL | const W: () = std::panic!(MSG); | ^^^^^^^^^^^^^^^^ evaluation of `W` failed here error[E0080]: evaluation panicked: hello - --> $DIR/const_panic.rs:22:16 + --> $DIR/const_panic.rs:23:16 | LL | const W2: () = std::panic!("{}", MSG); | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `W2` failed here error[E0080]: evaluation panicked: cheese - --> $DIR/const_panic.rs:25:20 + --> $DIR/const_panic.rs:26:20 | LL | const Z_CORE: () = core::panic!("cheese"); | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `Z_CORE` failed here error[E0080]: evaluation panicked: explicit panic - --> $DIR/const_panic.rs:28:21 + --> $DIR/const_panic.rs:29:21 | LL | const Z2_CORE: () = core::panic!(); | ^^^^^^^^^^^^^^ evaluation of `Z2_CORE` failed here error[E0080]: evaluation panicked: internal error: entered unreachable code - --> $DIR/const_panic.rs:31:20 + --> $DIR/const_panic.rs:32:20 | LL | const Y_CORE: () = core::unreachable!(); | ^^^^^^^^^^^^^^^^^^^^ evaluation of `Y_CORE` failed here error[E0080]: evaluation panicked: not implemented - --> $DIR/const_panic.rs:34:20 + --> $DIR/const_panic.rs:35:20 | LL | const X_CORE: () = core::unimplemented!(); | ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `X_CORE` failed here error[E0080]: evaluation panicked: hello - --> $DIR/const_panic.rs:37:20 + --> $DIR/const_panic.rs:38:20 | LL | const W_CORE: () = core::panic!(MSG); | ^^^^^^^^^^^^^^^^^ evaluation of `W_CORE` failed here error[E0080]: evaluation panicked: hello - --> $DIR/const_panic.rs:40:21 + --> $DIR/const_panic.rs:41:21 | LL | const W2_CORE: () = core::panic!("{}", MSG); | ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `W2_CORE` failed here -error: aborting due to 12 previous errors +error[E0080]: the program aborted execution + --> $DIR/const_panic.rs:45:19 + | +LL | const ABORT: () = core::intrinsics::abort(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `ABORT` failed here + +error: aborting due to 13 previous errors For more information about this error, try `rustc --explain E0080`. From 767210f85b306e7c3c3a920d143eeba3fba1419b Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Wed, 30 Sep 2026 22:03:17 -0500 Subject: [PATCH 2/2] core: Unstably constify `abort_immediate` For const-only code there isn't much of a reason to use this over `panic!()`, but having `abort_immediate` const will make it possible to reuse more code meant for runtime at comptime. The semantics are well-defined so this is pretty straightforward. --- library/core/src/process.rs | 3 ++- tests/ui/consts/const-eval/const_panic.rs | 5 ++--- tests/ui/consts/const-eval/const_panic.stderr | 9 ++++++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/library/core/src/process.rs b/library/core/src/process.rs index 81481b2cba26e..2da752b42ee85 100644 --- a/library/core/src/process.rs +++ b/library/core/src/process.rs @@ -35,9 +35,10 @@ /// Other signals such as `SIGABRT`, `SIGTRAP`, `SIGSEGV`, and `SIGBUS` may be /// produced instead, depending on specifics. This is not an exhaustive list. #[unstable(feature = "abort_immediate", issue = "154601")] +#[rustc_const_unstable(feature = "abort_immediate", issue = "154601")] #[cold] #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[doc(alias = "halt")] -pub fn abort_immediate() -> ! { +pub const fn abort_immediate() -> ! { crate::intrinsics::abort() } diff --git a/tests/ui/consts/const-eval/const_panic.rs b/tests/ui/consts/const-eval/const_panic.rs index 26eeec7778f7c..e4fd30d2e1678 100644 --- a/tests/ui/consts/const-eval/const_panic.rs +++ b/tests/ui/consts/const-eval/const_panic.rs @@ -1,5 +1,5 @@ //@ edition:2015..2021 -#![feature(core_intrinsics)] +#![feature(abort_immediate)] #![allow(non_fmt_panics)] #![crate_type = "lib"] @@ -41,6 +41,5 @@ const W_CORE: () = core::panic!(MSG); const W2_CORE: () = core::panic!("{}", MSG); //~^ ERROR evaluation panicked -// FIXME(abort_immediate): replace with the non-intrinsic version once available -const ABORT: () = core::intrinsics::abort(); +const ABORT: () = core::process::abort_immediate(); //~^ ERROR the program aborted execution diff --git a/tests/ui/consts/const-eval/const_panic.stderr b/tests/ui/consts/const-eval/const_panic.stderr index 1288112f462fe..19d8f3ae1c106 100644 --- a/tests/ui/consts/const-eval/const_panic.stderr +++ b/tests/ui/consts/const-eval/const_panic.stderr @@ -71,10 +71,13 @@ LL | const W2_CORE: () = core::panic!("{}", MSG); | ^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `W2_CORE` failed here error[E0080]: the program aborted execution - --> $DIR/const_panic.rs:45:19 + --> $DIR/const_panic.rs:44:19 | -LL | const ABORT: () = core::intrinsics::abort(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `ABORT` failed here +LL | const ABORT: () = core::process::abort_immediate(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `ABORT` failed inside this call + | +note: inside `abort_immediate` + --> $SRC_DIR/core/src/process.rs:LL:COL error: aborting due to 13 previous errors