summary refs log tree commit diff
path: root/compiler/rustc_const_eval
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2022-09-20 10:13:01 -0700
committerGitHub <noreply@github.com>2022-09-20 10:13:01 -0700
commitb149c481868a3efe10db36679a177cd2ede9d08a (patch)
treed0cac3f6a8cdefb992b5c116c713978b4910f3ed /compiler/rustc_const_eval
parent08b369a501d5981ecbb57d5f5f1f3bb4d34f081b (diff)
parentc54c5a3c77796e5194dd0369e9ffdbf41bf3c5d8 (diff)
downloadrust-b149c481868a3efe10db36679a177cd2ede9d08a.tar.gz
rust-b149c481868a3efe10db36679a177cd2ede9d08a.zip
Rollup merge of #102021 - lcnr:tyConst-fun, r=b-naber,BoxyUwU
some post-valtree cleanup

r? project-const-generics cc ```@b-naber```
Diffstat (limited to 'compiler/rustc_const_eval')
-rw-r--r--compiler/rustc_const_eval/src/const_eval/mod.rs8
-rw-r--r--compiler/rustc_const_eval/src/interpret/eval_context.rs9
-rw-r--r--compiler/rustc_const_eval/src/interpret/operand.rs70
3 files changed, 39 insertions, 48 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/mod.rs b/compiler/rustc_const_eval/src/const_eval/mod.rs
index d9c4ae4d53f..1c33e7845cb 100644
--- a/compiler/rustc_const_eval/src/const_eval/mod.rs
+++ b/compiler/rustc_const_eval/src/const_eval/mod.rs
@@ -100,10 +100,10 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
     tcx: TyCtxt<'tcx>,
     param_env: ty::ParamEnv<'tcx>,
     val: mir::ConstantKind<'tcx>,
-) -> InterpResult<'tcx, mir::DestructuredMirConstant<'tcx>> {
+) -> InterpResult<'tcx, mir::DestructuredConstant<'tcx>> {
     trace!("destructure_mir_constant: {:?}", val);
     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
-    let op = ecx.mir_const_to_op(&val, None)?;
+    let op = ecx.const_to_op(&val, None)?;
 
     // We go to `usize` as we cannot allocate anything bigger anyway.
     let (field_count, variant, down) = match val.ty().kind() {
@@ -129,7 +129,7 @@ pub(crate) fn try_destructure_mir_constant<'tcx>(
         .collect::<InterpResult<'tcx, Vec<_>>>()?;
     let fields = tcx.arena.alloc_from_iter(fields_iter);
 
-    Ok(mir::DestructuredMirConstant { variant, fields })
+    Ok(mir::DestructuredConstant { variant, fields })
 }
 
 #[instrument(skip(tcx), level = "debug")]
@@ -139,7 +139,7 @@ pub(crate) fn deref_mir_constant<'tcx>(
     val: mir::ConstantKind<'tcx>,
 ) -> mir::ConstantKind<'tcx> {
     let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
-    let op = ecx.mir_const_to_op(&val, None).unwrap();
+    let op = ecx.const_to_op(&val, None).unwrap();
     let mplace = ecx.deref_operand(&op).unwrap();
     if let Some(alloc_id) = mplace.ptr.provenance {
         assert_eq!(
diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs
index 3177537fee2..bdebfbb3ff5 100644
--- a/compiler/rustc_const_eval/src/interpret/eval_context.rs
+++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs
@@ -683,11 +683,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         self.stack_mut().push(frame);
 
         // Make sure all the constants required by this frame evaluate successfully (post-monomorphization check).
-        for const_ in &body.required_consts {
-            let span = const_.span;
-            let const_ =
-                self.subst_from_current_frame_and_normalize_erasing_regions(const_.literal)?;
-            self.mir_const_to_op(&const_, None).map_err(|err| {
+        for ct in &body.required_consts {
+            let span = ct.span;
+            let ct = self.subst_from_current_frame_and_normalize_erasing_regions(ct.literal)?;
+            self.const_to_op(&ct, None).map_err(|err| {
                 // If there was an error, set the span of the current frame to this constant.
                 // Avoiding doing this when evaluation succeeds.
                 self.frame_mut().loc = Err(span);
diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs
index 29c745a0886..dc5305aabcf 100644
--- a/compiler/rustc_const_eval/src/interpret/operand.rs
+++ b/compiler/rustc_const_eval/src/interpret/operand.rs
@@ -534,7 +534,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
                 // * During ConstProp, with `TooGeneric` or since the `required_consts` were not all
                 //   checked yet.
                 // * During CTFE, since promoteds in `const`/`static` initializer bodies can fail.
-                self.mir_const_to_op(&val, layout)?
+                self.const_to_op(&val, layout)?
             }
         };
         trace!("{:?}: {:?}", mir_op, *op);
@@ -549,50 +549,42 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         ops.iter().map(|op| self.eval_operand(op, None)).collect()
     }
 
-    // Used when the miri-engine runs into a constant and for extracting information from constants
-    // in patterns via the `const_eval` module
-    /// The `val` and `layout` are assumed to already be in our interpreter
-    /// "universe" (param_env).
     pub fn const_to_op(
         &self,
-        c: ty::Const<'tcx>,
-        layout: Option<TyAndLayout<'tcx>>,
-    ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
-        match c.kind() {
-            ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => throw_inval!(TooGeneric),
-            ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
-                throw_inval!(AlreadyReported(reported))
-            }
-            ty::ConstKind::Unevaluated(uv) => {
-                // NOTE: We evaluate to a `ValTree` here as a check to ensure
-                // we're working with valid constants, even though we never need it.
-                let instance = self.resolve(uv.def, uv.substs)?;
-                let cid = GlobalId { instance, promoted: None };
-                let _valtree = self
-                    .tcx
-                    .eval_to_valtree(self.param_env.and(cid))?
-                    .unwrap_or_else(|| bug!("unable to create ValTree for {:?}", uv));
-
-                Ok(self.eval_to_allocation(cid)?.into())
-            }
-            ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
-                span_bug!(self.cur_span(), "const_to_op: Unexpected ConstKind {:?}", c)
-            }
-            ty::ConstKind::Value(valtree) => {
-                let ty = c.ty();
-                let const_val = self.tcx.valtree_to_const_val((ty, valtree));
-                self.const_val_to_op(const_val, ty, layout)
-            }
-        }
-    }
-
-    pub fn mir_const_to_op(
-        &self,
         val: &mir::ConstantKind<'tcx>,
         layout: Option<TyAndLayout<'tcx>>,
     ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
         match val {
-            mir::ConstantKind::Ty(ct) => self.const_to_op(*ct, layout),
+            mir::ConstantKind::Ty(ct) => {
+                match ct.kind() {
+                    ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(..) => {
+                        throw_inval!(TooGeneric)
+                    }
+                    ty::ConstKind::Error(DelaySpanBugEmitted { reported, .. }) => {
+                        throw_inval!(AlreadyReported(reported))
+                    }
+                    ty::ConstKind::Unevaluated(uv) => {
+                        // NOTE: We evaluate to a `ValTree` here as a check to ensure
+                        // we're working with valid constants, even though we never need it.
+                        let instance = self.resolve(uv.def, uv.substs)?;
+                        let cid = GlobalId { instance, promoted: None };
+                        let _valtree = self
+                            .tcx
+                            .eval_to_valtree(self.param_env.and(cid))?
+                            .unwrap_or_else(|| bug!("unable to create ValTree for {uv:?}"));
+
+                        Ok(self.eval_to_allocation(cid)?.into())
+                    }
+                    ty::ConstKind::Bound(..) | ty::ConstKind::Infer(..) => {
+                        span_bug!(self.cur_span(), "unexpected ConstKind in ctfe: {ct:?}")
+                    }
+                    ty::ConstKind::Value(valtree) => {
+                        let ty = ct.ty();
+                        let const_val = self.tcx.valtree_to_const_val((ty, valtree));
+                        self.const_val_to_op(const_val, ty, layout)
+                    }
+                }
+            }
             mir::ConstantKind::Val(val, ty) => self.const_val_to_op(*val, *ty, layout),
             mir::ConstantKind::Unevaluated(uv, _) => {
                 let instance = self.resolve(uv.def, uv.substs)?;