From 3ef617014259d26502fa430ad96be4bd50625741 Mon Sep 17 00:00:00 2001
From: bjorn3 <bjorn3@users.noreply.github.com>
Date: Sun, 29 Mar 2020 11:51:43 +0200
Subject: [PATCH] Remove CPlace::NoPlace

---
 src/abi/comments.rs    |  1 -
 src/abi/mod.rs         |  2 +-
 src/abi/returning.rs   |  2 +-
 src/base.rs            |  6 ++---
 src/debuginfo/mod.rs   |  1 -
 src/value_and_place.rs | 56 ++++++++++++------------------------------
 6 files changed, 21 insertions(+), 47 deletions(-)

diff --git a/src/abi/comments.rs b/src/abi/comments.rs
index d949bf7f57d..4405f98addf 100644
--- a/src/abi/comments.rs
+++ b/src/abi/comments.rs
@@ -76,7 +76,6 @@ pub(super) fn add_local_place_comments<'tcx>(
             assert_eq!(local, var);
             ("ssa", std::borrow::Cow::Borrowed(""))
         }
-        CPlaceInner::NoPlace => ("zst", "".into()),
         CPlaceInner::Addr(ptr, meta) => {
             let meta = if let Some(meta) = meta {
                 Cow::Owned(format!(",meta={}", meta))
diff --git a/src/abi/mod.rs b/src/abi/mod.rs
index 87a36113b84..582021cb083 100644
--- a/src/abi/mod.rs
+++ b/src/abi/mod.rs
@@ -657,7 +657,7 @@ pub(crate) fn codegen_drop<'tcx>(
         let drop_fn_ty = drop_fn.monomorphic_ty(fx.tcx);
         match ty.kind {
             ty::Dynamic(..) => {
-                let (ptr, vtable) = drop_place.to_ptr_maybe_unsized(fx);
+                let (ptr, vtable) = drop_place.to_ptr_maybe_unsized();
                 let ptr = ptr.get_addr(fx);
                 let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap());
 
diff --git a/src/abi/returning.rs b/src/abi/returning.rs
index afe36d6890e..449c77c3c09 100644
--- a/src/abi/returning.rs
+++ b/src/abi/returning.rs
@@ -70,7 +70,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx, B: Backend, T>(
     let return_ptr = match output_pass_mode {
         PassMode::NoPass => None,
         PassMode::ByRef { sized: true } => match ret_place {
-            Some(ret_place) => Some(ret_place.to_ptr(fx).get_addr(fx)),
+            Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)),
             None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)),
         },
         PassMode::ByRef { sized: false } => todo!(),
diff --git a/src/base.rs b/src/base.rs
index 4b19895c450..414f6c30ce5 100644
--- a/src/base.rs
+++ b/src/base.rs
@@ -730,7 +730,7 @@ fn codegen_array_len<'tcx>(
             fx.bcx.ins().iconst(fx.pointer_type, len)
         }
         ty::Slice(_elem_ty) => place
-            .to_ptr_maybe_unsized(fx)
+            .to_ptr_maybe_unsized()
             .1
             .expect("Length metadata for slice place"),
         _ => bug!("Rvalue::Len({:?})", place),
@@ -776,7 +776,7 @@ pub(crate) fn trans_place<'tcx>(
                     ty::Array(elem_ty, _len) => {
                         assert!(!from_end, "array subslices are never `from_end`");
                         let elem_layout = fx.layout_of(elem_ty);
-                        let ptr = cplace.to_ptr(fx);
+                        let ptr = cplace.to_ptr();
                         cplace = CPlace::for_ptr(
                             ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * from as i64),
                             fx.layout_of(fx.tcx.mk_array(elem_ty, to as u64 - from as u64)),
@@ -785,7 +785,7 @@ pub(crate) fn trans_place<'tcx>(
                     ty::Slice(elem_ty) => {
                         assert!(from_end, "slice subslices should be `from_end`");
                         let elem_layout = fx.layout_of(elem_ty);
-                        let (ptr, len) = cplace.to_ptr_maybe_unsized(fx);
+                        let (ptr, len) = cplace.to_ptr_maybe_unsized();
                         let len = len.unwrap();
                         cplace = CPlace::for_ptr_with_extra(
                             ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * from as i64),
diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs
index 62898f075e2..51da712b55d 100644
--- a/src/debuginfo/mod.rs
+++ b/src/debuginfo/mod.rs
@@ -348,7 +348,6 @@ fn place_location<'a, 'tcx>(
             // For PointerBase::Stack:
             //AttributeValue::Exprloc(Expression(translate_loc(ValueLoc::Stack(*stack_slot), &context.func.stack_slots).unwrap()))
         }
-        CPlaceInner::NoPlace => AttributeValue::Exprloc(Expression(vec![])),
     }
 }
 
diff --git a/src/value_and_place.rs b/src/value_and_place.rs
index 6d164e70f0e..619b7199b53 100644
--- a/src/value_and_place.rs
+++ b/src/value_and_place.rs
@@ -100,7 +100,7 @@ impl<'tcx> CValue<'tcx> {
             CValueInner::ByVal(_) | CValueInner::ByValPair(_, _) => {
                 let cplace = CPlace::new_stack_slot(fx, layout);
                 cplace.write_cvalue(fx, self);
-                (cplace.to_ptr(fx), None)
+                (cplace.to_ptr(), None)
             }
         }
     }
@@ -259,7 +259,6 @@ pub(crate) struct CPlace<'tcx> {
 pub(crate) enum CPlaceInner {
     Var(Local),
     Addr(Pointer, Option<Value>),
-    NoPlace,
 }
 
 impl<'tcx> CPlace<'tcx> {
@@ -273,7 +272,7 @@ impl<'tcx> CPlace<'tcx> {
 
     pub(crate) fn no_place(layout: TyAndLayout<'tcx>) -> CPlace<'tcx> {
         CPlace {
-            inner: CPlaceInner::NoPlace,
+            inner: CPlaceInner::Addr(Pointer::dangling(layout.align.pref), None),
             layout,
         }
     }
@@ -284,10 +283,7 @@ impl<'tcx> CPlace<'tcx> {
     ) -> CPlace<'tcx> {
         assert!(!layout.is_unsized());
         if layout.size.bytes() == 0 {
-            return CPlace {
-                inner: CPlaceInner::NoPlace,
-                layout,
-            };
+            return CPlace::no_place(layout);
         }
 
         let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
@@ -343,33 +339,20 @@ impl<'tcx> CPlace<'tcx> {
                     CValue::by_ref(ptr, layout)
                 }
             }
-            CPlaceInner::NoPlace => CValue::by_ref(
-                Pointer::dangling(self.layout.align.pref),
-                layout,
-            ),
         }
     }
 
-    pub(crate) fn to_ptr(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>) -> Pointer {
-        match self.to_ptr_maybe_unsized(fx) {
+    pub(crate) fn to_ptr(self) -> Pointer {
+        match self.to_ptr_maybe_unsized() {
             (ptr, None) => ptr,
             (_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
         }
     }
 
-    pub(crate) fn to_ptr_maybe_unsized(
-        self,
-        fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
-    ) -> (Pointer, Option<Value>) {
+    pub(crate) fn to_ptr_maybe_unsized(self) -> (Pointer, Option<Value>) {
         match self.inner {
             CPlaceInner::Addr(ptr, extra) => (ptr, extra),
-            CPlaceInner::NoPlace => {
-                (
-                    Pointer::const_addr(fx, i64::try_from(self.layout.align.pref.bytes()).unwrap()),
-                    None,
-                )
-            }
-            CPlaceInner::Var(_) => bug!("Expected CPlace::Addr, found CPlace::Var"),
+            CPlaceInner::Var(_) => bug!("Expected CPlace::Addr, found {:?}", self),
         }
     }
 
@@ -460,12 +443,11 @@ impl<'tcx> CPlace<'tcx> {
                 fx.bcx.def_var(mir_var(var), data);
                 return;
             }
-            CPlaceInner::Addr(ptr, None) => ptr,
-            CPlaceInner::NoPlace => {
-                if dst_layout.abi != Abi::Uninhabited {
-                    assert_eq!(dst_layout.size.bytes(), 0, "{:?}", dst_layout);
+            CPlaceInner::Addr(ptr, None) => {
+                if dst_layout.size == Size::ZERO || dst_layout.abi == Abi::Uninhabited {
+                    return;
                 }
-                return;
+                ptr
             }
             CPlaceInner::Addr(_, Some(_)) => bug!("Can't write value to unsized place {:?}", self),
         };
@@ -524,7 +506,7 @@ impl<'tcx> CPlace<'tcx> {
         field: mir::Field,
     ) -> CPlace<'tcx> {
         let layout = self.layout();
-        let (base, extra) = self.to_ptr_maybe_unsized(fx);
+        let (base, extra) = self.to_ptr_maybe_unsized();
 
         let (field_ptr, field_layout) = codegen_field(fx, base, extra, layout, field);
         if field_layout.is_unsized() {
@@ -540,8 +522,8 @@ impl<'tcx> CPlace<'tcx> {
         index: Value,
     ) -> CPlace<'tcx> {
         let (elem_layout, ptr) = match self.layout().ty.kind {
-            ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_ptr(fx)),
-            ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_ptr_maybe_unsized(fx).0),
+            ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_ptr()),
+            ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_ptr_maybe_unsized().0),
             _ => bug!("place_index({:?})", self.layout().ty),
         };
 
@@ -565,7 +547,7 @@ impl<'tcx> CPlace<'tcx> {
 
     pub(crate) fn write_place_ref(self, fx: &mut FunctionCx<'_, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
         if has_ptr_meta(fx.tcx, self.layout().ty) {
-            let (ptr, extra) = self.to_ptr_maybe_unsized(fx);
+            let (ptr, extra) = self.to_ptr_maybe_unsized();
             let ptr = CValue::by_val_pair(
                 ptr.get_addr(fx),
                 extra.expect("unsized type without metadata"),
@@ -573,19 +555,13 @@ impl<'tcx> CPlace<'tcx> {
             );
             dest.write_cvalue(fx, ptr);
         } else {
-            let ptr = CValue::by_val(self.to_ptr(fx).get_addr(fx), dest.layout());
+            let ptr = CValue::by_val(self.to_ptr().get_addr(fx), dest.layout());
             dest.write_cvalue(fx, ptr);
         }
     }
 
     pub(crate) fn unchecked_cast_to(self, layout: TyAndLayout<'tcx>) -> Self {
         assert!(!self.layout().is_unsized());
-        match self.inner {
-            CPlaceInner::NoPlace => {
-                assert!(layout.size.bytes() == 0);
-            }
-            _ => {}
-        }
         CPlace {
             inner: self.inner,
             layout,