diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-07-10 18:39:25 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2021-07-28 16:18:38 +0200 |
| commit | 0b8033ad8d320c05ec24de5b4538099f6c3b2e4d (patch) | |
| tree | 22633844c613061ded1b3c9f8a795ea0aa69c217 | |
| parent | 4e6356188f14b2c05957a4671e7a8e857fa5f429 (diff) | |
| download | rust-0b8033ad8d320c05ec24de5b4538099f6c3b2e4d.tar.gz rust-0b8033ad8d320c05ec24de5b4538099f6c3b2e4d.zip | |
Improve comments about const panic handling
Co-authored-by: Ralf Jung <post@ralfj.de>
| -rw-r--r-- | compiler/rustc_mir/src/const_eval/machine.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_mir/src/transform/check_consts/mod.rs | 3 | ||||
| -rw-r--r-- | library/core/src/panicking.rs | 4 |
3 files changed, 7 insertions, 2 deletions
diff --git a/compiler/rustc_mir/src/const_eval/machine.rs b/compiler/rustc_mir/src/const_eval/machine.rs index dae2fe91294..8a90686b900 100644 --- a/compiler/rustc_mir/src/const_eval/machine.rs +++ b/compiler/rustc_mir/src/const_eval/machine.rs @@ -31,6 +31,8 @@ impl<'mir, 'tcx> InterpCx<'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> { instance: ty::Instance<'tcx>, args: &[OpTy<'tcx>], ) -> InterpResult<'tcx, Option<ty::Instance<'tcx>>> { + // The list of functions we handle here must be in sync with + // `is_lang_panic_fn` in `transform/check_consts/mod.rs`. let def_id = instance.def_id(); if Some(def_id) == self.tcx.lang_items().panic_fn() || Some(def_id) == self.tcx.lang_items().panic_str() diff --git a/compiler/rustc_mir/src/transform/check_consts/mod.rs b/compiler/rustc_mir/src/transform/check_consts/mod.rs index ba8189cf9a8..7e22ed22db4 100644 --- a/compiler/rustc_mir/src/transform/check_consts/mod.rs +++ b/compiler/rustc_mir/src/transform/check_consts/mod.rs @@ -74,6 +74,9 @@ impl ConstCx<'mir, 'tcx> { /// Returns `true` if this `DefId` points to one of the official `panic` lang items. pub fn is_lang_panic_fn(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { + // We can allow calls to these functions because `hook_panic_fn` in + // `const_eval/machine.rs` ensures the calls are handled specially. + // Keep in sync with what that function handles! Some(def_id) == tcx.lang_items().panic_fn() || Some(def_id) == tcx.lang_items().panic_str() || Some(def_id) == tcx.lang_items().begin_panic_fn() diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 65267a417cb..2ec6b4d15ff 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -100,8 +100,8 @@ pub const fn const_panic_fmt(fmt: fmt::Arguments<'_>) -> ! { if let Some(msg) = fmt.as_str() { panic_str(msg); } else { - // SAFETY: This is only evaluated at compile time, which handles this - // fine (in case it turns out this branch turns out to be reachable + // SAFETY: This is only evaluated at compile time, which reliably + // handles this UB (in case this branch turns out to be reachable // somehow). unsafe { crate::hint::unreachable_unchecked() }; } |
