]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/mir/tcx.rs
Remove PlaceBase enum and make Place base field be local: Local
[rust.git] / src / librustc / mir / tcx.rs
index 445fa6ea8cab31b7fb2d982994debcbb08e70175..e2aac562cc4ccf9ef81482b263e37f30ce8688e3 100644 (file)
@@ -4,11 +4,11 @@
  */
 
 use crate::mir::*;
-use crate::ty::subst::Subst;
-use crate::ty::{self, Ty, TyCtxt};
 use crate::ty::layout::VariantIdx;
-use crate::hir;
+use crate::ty::subst::Subst;
 use crate::ty::util::IntTypeExt;
+use crate::ty::{self, Ty, TyCtxt};
+use rustc_hir as hir;
 
 #[derive(Copy, Clone, Debug, TypeFoldable)]
 pub struct PlaceTy<'tcx> {
@@ -78,36 +78,34 @@ pub fn projection_ty_core<V, T>(
     {
         let answer = match *elem {
             ProjectionElem::Deref => {
-                let ty = self.ty
-                             .builtin_deref(true)
-                             .unwrap_or_else(|| {
-                                 bug!("deref projection of non-dereferenceable ty {:?}", self)
-                             })
-                             .ty;
+                let ty = self
+                    .ty
+                    .builtin_deref(true)
+                    .unwrap_or_else(|| {
+                        bug!("deref projection of non-dereferenceable ty {:?}", self)
+                    })
+                    .ty;
                 PlaceTy::from_ty(ty)
             }
-            ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } =>
-                PlaceTy::from_ty(self.ty.builtin_index().unwrap()),
+            ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
+                PlaceTy::from_ty(self.ty.builtin_index().unwrap())
+            }
             ProjectionElem::Subslice { from, to, from_end } => {
                 PlaceTy::from_ty(match self.ty.kind {
                     ty::Slice(..) => self.ty,
-                    ty::Array(inner, _) if !from_end => {
-                        tcx.mk_array(inner, (to - from) as u64)
-                    }
+                    ty::Array(inner, _) if !from_end => tcx.mk_array(inner, (to - from) as u64),
                     ty::Array(inner, size) if from_end => {
                         let size = size.eval_usize(tcx, param_env);
                         let len = size - (from as u64) - (to as u64);
                         tcx.mk_array(inner, len)
                     }
-                    _ => {
-                        bug!("cannot subslice non-array type: `{:?}`", self)
-                    }
+                    _ => bug!("cannot subslice non-array type: `{:?}`", self),
                 })
             }
-            ProjectionElem::Downcast(_name, index) =>
-                PlaceTy { ty: self.ty, variant_index: Some(index) },
-            ProjectionElem::Field(ref f, ref fty) =>
-                PlaceTy::from_ty(handle_field(&self, f, fty)),
+            ProjectionElem::Downcast(_name, index) => {
+                PlaceTy { ty: self.ty, variant_index: Some(index) }
+            }
+            ProjectionElem::Field(ref f, ref fty) => PlaceTy::from_ty(handle_field(&self, f, fty)),
         };
         debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
         answer
@@ -116,41 +114,32 @@ pub fn projection_ty_core<V, T>(
 
 impl<'tcx> Place<'tcx> {
     pub fn ty_from<D>(
-        base: &PlaceBase<'tcx>,
+        local: &Local,
         projection: &[PlaceElem<'tcx>],
         local_decls: &D,
-        tcx: TyCtxt<'tcx>
+        tcx: TyCtxt<'tcx>,
     ) -> PlaceTy<'tcx>
-        where D: HasLocalDecls<'tcx>
+    where
+        D: HasLocalDecls<'tcx>,
     {
-        projection.iter().fold(
-            base.ty(local_decls),
-            |place_ty, elem| place_ty.projection_ty(tcx, elem)
-        )
+        projection
+            .iter()
+            .fold(PlaceTy::from_ty(local_decls.local_decls()[*local].ty), |place_ty, elem| {
+                place_ty.projection_ty(tcx, elem)
+            })
     }
 
     pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
     where
         D: HasLocalDecls<'tcx>,
     {
-        Place::ty_from(&self.base, &self.projection, local_decls, tcx)
-    }
-}
-
-impl<'tcx> PlaceBase<'tcx> {
-    pub fn ty<D>(&self, local_decls: &D) -> PlaceTy<'tcx>
-        where D: HasLocalDecls<'tcx>
-    {
-        match self {
-            PlaceBase::Local(index) => PlaceTy::from_ty(local_decls.local_decls()[*index].ty),
-            PlaceBase::Static(data) => PlaceTy::from_ty(data.ty),
-        }
+        Place::ty_from(&self.local, &self.projection, local_decls, tcx)
     }
 }
 
 pub enum RvalueInitializationState {
     Shallow,
-    Deep
+    Deep,
 }
 
 impl<'tcx> Rvalue<'tcx> {
@@ -160,17 +149,14 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
     {
         match *self {
             Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
-            Rvalue::Repeat(ref operand, count) => {
-                tcx.mk_array(operand.ty(local_decls, tcx), count)
-            }
+            Rvalue::Repeat(ref operand, count) => tcx.mk_array(operand.ty(local_decls, tcx), count),
             Rvalue::Ref(reg, bk, ref place) => {
                 let place_ty = place.ty(local_decls, tcx).ty;
-                tcx.mk_ref(reg,
-                    ty::TypeAndMut {
-                        ty: place_ty,
-                        mutbl: bk.to_mutbl_lossy()
-                    }
-                )
+                tcx.mk_ref(reg, ty::TypeAndMut { ty: place_ty, mutbl: bk.to_mutbl_lossy() })
+            }
+            Rvalue::AddressOf(mutability, ref place) => {
+                let place_ty = place.ty(local_decls, tcx).ty;
+                tcx.mk_ptr(ty::TypeAndMut { ty: place_ty, mutbl: mutability.into() })
             }
             Rvalue::Len(..) => tcx.types.usize,
             Rvalue::Cast(.., ty) => ty,
@@ -185,8 +171,7 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
                 let ty = op.ty(tcx, lhs_ty, rhs_ty);
                 tcx.intern_tup(&[ty, tcx.types.bool])
             }
-            Rvalue::UnaryOp(UnOp::Not, ref operand) |
-            Rvalue::UnaryOp(UnOp::Neg, ref operand) => {
+            Rvalue::UnaryOp(UnOp::Not, ref operand) | Rvalue::UnaryOp(UnOp::Neg, ref operand) => {
                 operand.ty(local_decls, tcx)
             }
             Rvalue::Discriminant(ref place) => {
@@ -202,25 +187,15 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
             }
             Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
             Rvalue::NullaryOp(NullOp::SizeOf, _) => tcx.types.usize,
-            Rvalue::Aggregate(ref ak, ref ops) => {
-                match **ak {
-                    AggregateKind::Array(ty) => {
-                        tcx.mk_array(ty, ops.len() as u64)
-                    }
-                    AggregateKind::Tuple => {
-                        tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx)))
-                    }
-                    AggregateKind::Adt(def, _, substs, _, _) => {
-                        tcx.type_of(def.did).subst(tcx, substs)
-                    }
-                    AggregateKind::Closure(did, substs) => {
-                        tcx.mk_closure(did, substs)
-                    }
-                    AggregateKind::Generator(did, substs, movability) => {
-                        tcx.mk_generator(did, substs, movability)
-                    }
+            Rvalue::Aggregate(ref ak, ref ops) => match **ak {
+                AggregateKind::Array(ty) => tcx.mk_array(ty, ops.len() as u64),
+                AggregateKind::Tuple => tcx.mk_tup(ops.iter().map(|op| op.ty(local_decls, tcx))),
+                AggregateKind::Adt(def, _, substs, _, _) => tcx.type_of(def.did).subst(tcx, substs),
+                AggregateKind::Closure(did, substs) => tcx.mk_closure(did, substs),
+                AggregateKind::Generator(did, substs, movability) => {
+                    tcx.mk_generator(did, substs, movability)
                 }
-            }
+            },
         }
     }
 
@@ -230,7 +205,7 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
     pub fn initialization_state(&self) -> RvalueInitializationState {
         match *self {
             Rvalue::NullaryOp(NullOp::Box, _) => RvalueInitializationState::Shallow,
-            _ => RvalueInitializationState::Deep
+            _ => RvalueInitializationState::Deep,
         }
     }
 }
@@ -241,8 +216,7 @@ pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
         D: HasLocalDecls<'tcx>,
     {
         match self {
-            &Operand::Copy(ref l) |
-            &Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
+            &Operand::Copy(ref l) | &Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
             &Operand::Constant(ref c) => c.literal.ty,
         }
     }
@@ -252,8 +226,14 @@ impl<'tcx> BinOp {
     pub fn ty(&self, tcx: TyCtxt<'tcx>, lhs_ty: Ty<'tcx>, rhs_ty: Ty<'tcx>) -> Ty<'tcx> {
         // FIXME: handle SIMD correctly
         match self {
-            &BinOp::Add | &BinOp::Sub | &BinOp::Mul | &BinOp::Div | &BinOp::Rem |
-            &BinOp::BitXor | &BinOp::BitAnd | &BinOp::BitOr => {
+            &BinOp::Add
+            | &BinOp::Sub
+            | &BinOp::Mul
+            | &BinOp::Div
+            | &BinOp::Rem
+            | &BinOp::BitXor
+            | &BinOp::BitAnd
+            | &BinOp::BitOr => {
                 // these should be integers or floats of the same size.
                 assert_eq!(lhs_ty, rhs_ty);
                 lhs_ty
@@ -261,8 +241,7 @@ pub fn ty(&self, tcx: TyCtxt<'tcx>, lhs_ty: Ty<'tcx>, rhs_ty: Ty<'tcx>) -> Ty<'t
             &BinOp::Shl | &BinOp::Shr | &BinOp::Offset => {
                 lhs_ty // lhs_ty can be != rhs_ty
             }
-            &BinOp::Eq | &BinOp::Lt | &BinOp::Le |
-            &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
+            &BinOp::Eq | &BinOp::Lt | &BinOp::Le | &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
                 tcx.types.bool
             }
         }
@@ -272,17 +251,17 @@ pub fn ty(&self, tcx: TyCtxt<'tcx>, lhs_ty: Ty<'tcx>, rhs_ty: Ty<'tcx>) -> Ty<'t
 impl BorrowKind {
     pub fn to_mutbl_lossy(self) -> hir::Mutability {
         match self {
-            BorrowKind::Mut { .. } => hir::Mutability::Mutable,
-            BorrowKind::Shared => hir::Mutability::Immutable,
+            BorrowKind::Mut { .. } => hir::Mutability::Mut,
+            BorrowKind::Shared => hir::Mutability::Not,
 
             // We have no type corresponding to a unique imm borrow, so
             // use `&mut`. It gives all the capabilities of an `&uniq`
             // and hence is a safe "over approximation".
-            BorrowKind::Unique => hir::Mutability::Mutable,
+            BorrowKind::Unique => hir::Mutability::Mut,
 
             // We have no type corresponding to a shallow borrow, so use
             // `&` as an approximation.
-            BorrowKind::Shallow => hir::Mutability::Immutable,
+            BorrowKind::Shallow => hir::Mutability::Not,
         }
     }
 }
@@ -306,7 +285,7 @@ pub fn to_hir_binop(self) -> hir::BinOpKind {
             BinOp::Gt => hir::BinOpKind::Gt,
             BinOp::Le => hir::BinOpKind::Le,
             BinOp::Ge => hir::BinOpKind::Ge,
-            BinOp::Offset => unreachable!()
+            BinOp::Offset => unreachable!(),
         }
     }
 }