about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
diff options
context:
space:
mode:
authorouz-a <ouz.agz@gmail.com>2023-08-28 11:19:19 +0300
committerouz-a <ouz.agz@gmail.com>2023-10-02 23:39:44 +0300
commitcd7f47193182d1d557a83593bcff6afe0568fc53 (patch)
tree5ae299b1d8d300839a57994f58b37d0f20541594 /compiler/rustc_const_eval/src
parent3148e6a9933b17b28ed6c7b8d8bd6c8e49fe4a50 (diff)
downloadrust-cd7f47193182d1d557a83593bcff6afe0568fc53.tar.gz
rust-cd7f47193182d1d557a83593bcff6afe0568fc53.zip
Add docs, remove code, change subtyper code
Diffstat (limited to 'compiler/rustc_const_eval/src')
-rw-r--r--compiler/rustc_const_eval/src/interpret/operand.rs3
-rw-r--r--compiler/rustc_const_eval/src/interpret/projection.rs3
-rw-r--r--compiler/rustc_const_eval/src/transform/validate.rs43
3 files changed, 21 insertions, 28 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs
index b33396de33b..a32ea204f98 100644
--- a/compiler/rustc_const_eval/src/interpret/operand.rs
+++ b/compiler/rustc_const_eval/src/interpret/operand.rs
@@ -665,9 +665,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         let mut op = self.local_to_op(self.frame(), mir_place.local, layout)?;
         // Using `try_fold` turned out to be bad for performance, hence the loop.
         for elem in mir_place.projection.iter() {
-            if elem.is_subtype() {
-                continue;
-            }
             op = self.project(&op, elem)?
         }
 
diff --git a/compiler/rustc_const_eval/src/interpret/projection.rs b/compiler/rustc_const_eval/src/interpret/projection.rs
index 3c24381f93d..70df3d8fd78 100644
--- a/compiler/rustc_const_eval/src/interpret/projection.rs
+++ b/compiler/rustc_const_eval/src/interpret/projection.rs
@@ -319,6 +319,8 @@ where
             OpaqueCast(ty) => {
                 span_bug!(self.cur_span(), "OpaqueCast({ty}) encountered after borrowck")
             }
+            // We don't want anything happening here, this is here as a dummy.
+            Subtype(_) => base.transmute(base.layout(), self)?,
             Field(field, _) => self.project_field(base, field.index())?,
             Downcast(_, variant) => self.project_downcast(base, variant)?,
             Deref => self.deref_pointer(&base.to_op(self)?)?.into(),
@@ -332,7 +334,6 @@ where
                 self.project_constant_index(base, offset, min_length, from_end)?
             }
             Subslice { from, to, from_end } => self.project_subslice(base, from, to, from_end)?,
-            Subtype(ty) => base.transmute(self.layout_of(ty)?, self)?,
         })
     }
 }
diff --git a/compiler/rustc_const_eval/src/transform/validate.rs b/compiler/rustc_const_eval/src/transform/validate.rs
index 6f2802b921f..6a5a3628617 100644
--- a/compiler/rustc_const_eval/src/transform/validate.rs
+++ b/compiler/rustc_const_eval/src/transform/validate.rs
@@ -16,6 +16,8 @@ use rustc_target::spec::abi::Abi;
 
 use crate::util::is_within_packed;
 
+use crate::util::is_subtype;
+
 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
 enum EdgeKind {
     Unwind,
@@ -602,35 +604,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
             return true;
         }
 
-        crate::util::is_subtype(self.tcx, self.param_env, src, dest)
+        return crate::util::is_subtype(self.tcx, self.param_env, src, dest);
     }
 }
 
 impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
     fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
-        match operand {
-            Operand::Copy(place) | Operand::Move(place) => {
-                if let Some(stmt) = self.body.stmt_at(location).left() {
-                    match &stmt.kind {
-                        StatementKind::Assign(box (lval, rvalue)) => {
-                            let place_ty = place.ty(&self.body.local_decls, self.tcx).ty;
-                            let lval_ty = lval.ty(&self.body.local_decls, self.tcx).ty;
-
-                            if !place.is_subtype()
-                                && place_ty != lval_ty
-                                && rvalue.ty(&self.body.local_decls, self.tcx) != lval_ty
-                                && (rvalue.ty(&self.body.local_decls, self.tcx).is_closure()
-                                    != lval_ty.is_closure())
-                            {
-                                self.fail(location, format!("Subtyping is not allowed between types {place_ty:#?} and {lval_ty:#?}"))
-                            }
-                        }
-                        _ => (),
-                    }
-                }
-            }
-            _ => (),
-        }
         // This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
         if self.tcx.sess.opts.unstable_opts.validate_mir
             && self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial)
@@ -776,6 +755,22 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
                     }
                 }
             }
+            ProjectionElem::Subtype(ty) => {
+                if !is_subtype(
+                    self.tcx,
+                    self.param_env,
+                    ty,
+                    place_ref.ty(&self.body.local_decls, self.tcx).ty,
+                ) {
+                    self.fail(
+                        location,
+                        format!(
+                            "Failed subtyping {ty:#?} and {:#?}",
+                            place_ref.ty(&self.body.local_decls, self.tcx).ty
+                        ),
+                    )
+                }
+            }
             _ => {}
         }
         self.super_projection_elem(place_ref, elem, context, location);