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
2 changes: 2 additions & 0 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum ConstEvalErrKind {
col: u32,
file: Symbol,
},
Abort,
WriteThroughImmutablePointer,
/// Called `const_make_global` twice.
ConstMakeGlobalPtrAlreadyMadeGlobal(AllocId),
Expand All @@ -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")
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])?;
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ pub fn rustc_peek<T>(_: 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.
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
1 change: 1 addition & 0 deletions src/tools/miri/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
));
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/consts/const-eval/const_panic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ edition:2015..2021
#![feature(abort_immediate)]
#![allow(non_fmt_panics)]
#![crate_type = "lib"]

Expand Down Expand Up @@ -39,3 +40,6 @@ const W_CORE: () = core::panic!(MSG);

const W2_CORE: () = core::panic!("{}", MSG);
//~^ ERROR evaluation panicked

const ABORT: () = core::process::abort_immediate();
//~^ ERROR the program aborted execution
35 changes: 22 additions & 13 deletions tests/ui/consts/const-eval/const_panic.stderr
Original file line number Diff line number Diff line change
@@ -1,75 +1,84 @@
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:44:19
|
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

For more information about this error, try `rustc --explain E0080`.
Loading