diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 669838308a5..cc8b10bd07d 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -132,8 +132,8 @@ pub(super) fn op_to_const<'tcx>( // functionality.) _ => false, }; - let immediate = if force_as_immediate { - Right(ecx.read_immediate(op).expect("normalization works on validated constants")) + let immediate = if force_as_immediate && let Ok(imm) = ecx.read_immediate(op) { + Right(imm) } else { op.as_mplace_or_imm() }; diff --git a/tests/ui/mir/issue-117368-print-invalid-constant.rs b/tests/ui/mir/issue-117368-print-invalid-constant.rs new file mode 100644 index 00000000000..d9610391941 --- /dev/null +++ b/tests/ui/mir/issue-117368-print-invalid-constant.rs @@ -0,0 +1,11 @@ +// build-pass +// compile-flags:-Zmir-opt-level=4 --emit=mir + +#![feature(allocator_api)] + +use std::alloc::{Allocator, Global, Layout}; + +fn main() { + let layout: Layout = None.unwrap(); + let ptr: *mut u8 = Global.allocate(layout).unwrap().as_ptr() as _; +}