about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/const_eval
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2023-11-01 18:36:38 +0000
committerCamille GILLOT <gillot.camille@gmail.com>2023-11-01 18:36:38 +0000
commit224e29030a2b576e011eace91e023aa64e99e454 (patch)
tree177800f2f4ffb337db168901ff7790eebb782238 /compiler/rustc_const_eval/src/const_eval
parent0f8f77fcc20cc5c44b5aa56b5e43620c0c44128e (diff)
Specify diagnostic path.
Diffstat (limited to 'compiler/rustc_const_eval/src/const_eval')
-rw-r--r--compiler/rustc_const_eval/src/const_eval/eval_queries.rs13
-rw-r--r--compiler/rustc_const_eval/src/const_eval/mod.rs2
-rw-r--r--compiler/rustc_const_eval/src/const_eval/valtrees.rs4
3 files changed, 13 insertions, 6 deletions
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 cc8b10bd07d..b325814bcd7 100644
--- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs
+++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs
@@ -110,6 +110,7 @@ pub(crate) fn mk_eval_cx<'mir, 'tcx>(
 pub(super) fn op_to_const<'tcx>(
     ecx: &CompileTimeEvalContext<'_, 'tcx>,
     op: &OpTy<'tcx>,
+    for_diagnostics: bool,
 ) -> ConstValue<'tcx> {
     // Handle ZST consistently and early.
     if op.layout.is_zst() {
@@ -132,8 +133,14 @@ pub(super) fn op_to_const<'tcx>(
         // functionality.)
         _ => false,
     };
-    let immediate = if force_as_immediate && let Ok(imm) = ecx.read_immediate(op) {
-        Right(imm)
+    let immediate = if force_as_immediate {
+        match ecx.read_immediate(op) {
+            Ok(imm) => Right(imm),
+            Err(err) if !for_diagnostics => {
+                panic!("normalization works on validated constants: {err:?}")
+            }
+            _ => op.as_mplace_or_imm(),
+        }
     } else {
         op.as_mplace_or_imm()
     };
@@ -205,7 +212,7 @@ pub(crate) fn turn_into_const_value<'tcx>(
     );
 
     // Turn this into a proper constant.
-    op_to_const(&ecx, &mplace.into())
+    op_to_const(&ecx, &mplace.into(), /* for diagnostics */ false)
 }
 
 #[instrument(skip(tcx), level = "debug")]
diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs
index 6b85a1276f2..e53282ffaeb 100644
--- a/compiler/rustc_const_eval/src/const_eval/mod.rs
+++ b/compiler/rustc_const_eval/src/const_eval/mod.rs
@@ -99,7 +99,7 @@ pub(crate) fn try_destructure_mir_constant_for_user_output<'tcx>(
     let fields_iter = (0..field_count)
         .map(|i| {
             let field_op = ecx.project_field(&down, i).ok()?;
-            let val = op_to_const(&ecx, &field_op);
+            let val = op_to_const(&ecx, &field_op, /* for diagnostics */ true);
             Some((val, field_op.layout.ty))
         })
         .collect::<Option<Vec<_>>>()?;
diff --git a/compiler/rustc_const_eval/src/const_eval/valtrees.rs b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
index d6dc1a62f4d..3aaaf08c856 100644
--- a/compiler/rustc_const_eval/src/const_eval/valtrees.rs
+++ b/compiler/rustc_const_eval/src/const_eval/valtrees.rs
@@ -232,7 +232,7 @@ pub fn valtree_to_const_value<'tcx>(
             let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, CanAccessStatics::No);
             let imm = valtree_to_ref(&mut ecx, valtree, *inner_ty);
             let imm = ImmTy::from_immediate(imm, tcx.layout_of(param_env_ty).unwrap());
-            op_to_const(&ecx, &imm.into())
+            op_to_const(&ecx, &imm.into(), /* for diagnostics */ false)
         }
         ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
             let layout = tcx.layout_of(param_env_ty).unwrap();
@@ -265,7 +265,7 @@ pub fn valtree_to_const_value<'tcx>(
             dump_place(&ecx, &place);
             intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &place).unwrap();
 
-            op_to_const(&ecx, &place.into())
+            op_to_const(&ecx, &place.into(), /* for diagnostics */ false)
         }
         ty::Never
         | ty::Error(_)