Skip to content

Commit 0b8033a

Browse files
m-ou-seRalfJung
andcommitted
Improve comments about const panic handling
Co-authored-by: Ralf Jung <[email protected]>
1 parent 4e63561 commit 0b8033a

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

compiler/rustc_mir/src/const_eval/machine.rs

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
3131
instance: ty::Instance<'tcx>,
3232
args: &[OpTy<'tcx>],
3333
) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> {
34+
// The list of functions we handle here must be in sync with
35+
// `is_lang_panic_fn` in `transform/check_consts/mod.rs`.
3436
let def_id = instance.def_id();
3537
if Some(def_id) == self.tcx.lang_items().panic_fn()
3638
|| Some(def_id) == self.tcx.lang_items().panic_str()

compiler/rustc_mir/src/transform/check_consts/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ impl ConstCx<'mir, 'tcx> {
7474

7575
/// Returns `true` if this `DefId` points to one of the official `panic` lang items.
7676
pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool {
77+
// We can allow calls to these functions because `hook_panic_fn` in
78+
// `const_eval/machine.rs` ensures the calls are handled specially.
79+
// Keep in sync with what that function handles!
7780
Some(def_id) == tcx.lang_items().panic_fn()
7881
|| Some(def_id) == tcx.lang_items().panic_str()
7982
|| Some(def_id) == tcx.lang_items().begin_panic_fn()

library/core/src/panicking.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
100100
if let Some(msg) = fmt.as_str() {
101101
panic_str(msg);
102102
} else {
103-
// SAFETY: This is only evaluated at compile time, which handles this
104-
// fine (in case it turns out this branch turns out to be reachable
103+
// SAFETY: This is only evaluated at compile time, which reliably
104+
// handles this UB (in case this branch turns out to be reachable
105105
// somehow).
106106
unsafe { crate::hint::unreachable_unchecked() };
107107
}

0 commit comments

Comments
 (0)