]> git.lizzy.rs Git - rust.git/blobdiff - src/unsize.rs
Rollup merge of #81618 - bjorn3:sync_cg_clif-2021-02-01, r=bjorn3
[rust.git] / src / unsize.rs
index 57399e10b7ab9324aed1a862ae9c1039cc6e9913..c77ff5d56ba626859e85d806f1c184ee3e41d712 100644 (file)
@@ -1,3 +1,7 @@
+//! Codegen of the [`PointerCast::Unsize`] operation.
+//!
+//! [`PointerCast::Unsize`]: `rustc_middle::ty::adjustment::PointerCast::Unsize`
+
 use crate::prelude::*;
 
 // Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/base.rs#L159-L307
 /// The `old_info` argument is a bit funny. It is intended for use
 /// in an upcast, where the new vtable for an object will be derived
 /// from the old one.
-pub fn unsized_info<'a, 'tcx: 'a>(
-    fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
+pub(crate) fn unsized_info<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     source: Ty<'tcx>,
     target: Ty<'tcx>,
     old_info: Option<Value>,
 ) -> Value {
-    let (source, target) = fx.tcx.struct_lockstep_tails(source, target);
-    match (&source.sty, &target.sty) {
-        (&ty::Array(_, len), &ty::Slice(_)) => fx
-            .bcx
-            .ins()
-            .iconst(fx.pointer_type, len.unwrap_usize(fx.tcx) as i64),
+    let (source, target) =
+        fx.tcx
+            .struct_lockstep_tails_erasing_lifetimes(source, target, ParamEnv::reveal_all());
+    match (&source.kind(), &target.kind()) {
+        (&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst(
+            fx.pointer_type,
+            len.eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64,
+        ),
         (&ty::Dynamic(..), &ty::Dynamic(..)) => {
             // For now, upcasts are limited to changes in marker
             // traits, and hence never actually require an actual
             // change to the vtable.
             old_info.expect("unsized_info: missing old info for trait upcast")
         }
-        (_, &ty::Dynamic(ref data, ..)) => crate::vtable::get_vtable(fx, source, data.principal()),
+        (_, &ty::Dynamic(ref data, ..)) => {
+            crate::vtable::get_vtable(fx, fx.layout_of(source), data.principal())
+        }
         _ => bug!(
             "unsized_info: invalid unsizing {:?} -> {:?}",
             source,
@@ -36,13 +44,13 @@ pub fn unsized_info<'a, 'tcx: 'a>(
 }
 
 /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
-pub fn unsize_thin_ptr<'a, 'tcx: 'a>(
-    fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
+fn unsize_thin_ptr<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     src: Value,
-    src_ty: Ty<'tcx>,
-    dst_ty: Ty<'tcx>,
+    src_layout: TyAndLayout<'tcx>,
+    dst_layout: TyAndLayout<'tcx>,
 ) -> (Value, Value) {
-    match (&src_ty.sty, &dst_ty.sty) {
+    match (&src_layout.ty.kind(), &dst_layout.ty.kind()) {
         (&ty::Ref(_, a, _), &ty::Ref(_, b, _))
         | (&ty::Ref(_, a, _), &ty::RawPtr(ty::TypeAndMut { ty: b, .. }))
         | (&ty::RawPtr(ty::TypeAndMut { ty: a, .. }), &ty::RawPtr(ty::TypeAndMut { ty: b, .. })) => {
@@ -50,15 +58,13 @@ pub fn unsize_thin_ptr<'a, 'tcx: 'a>(
             (src, unsized_info(fx, a, b, None))
         }
         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) if def_a.is_box() && def_b.is_box() => {
-            let (a, b) = (src_ty.boxed_ty(), dst_ty.boxed_ty());
+            let (a, b) = (src_layout.ty.boxed_ty(), dst_layout.ty.boxed_ty());
             assert!(!fx.layout_of(a).is_unsized());
             (src, unsized_info(fx, a, b, None))
         }
         (&ty::Adt(def_a, _), &ty::Adt(def_b, _)) => {
             assert_eq!(def_a, def_b);
 
-            let src_layout = fx.layout_of(src_ty);
-            let dst_layout = fx.layout_of(dst_ty);
             let mut result = None;
             for i in 0..src_layout.fields.count() {
                 let src_f = src_layout.field(fx, i);
@@ -72,7 +78,7 @@ pub fn unsize_thin_ptr<'a, 'tcx: 'a>(
                 let dst_f = dst_layout.field(fx, i);
                 assert_ne!(src_f.ty, dst_f.ty);
                 assert_eq!(result, None);
-                result = Some(unsize_thin_ptr(fx, src, src_f.ty, dst_f.ty));
+                result = Some(unsize_thin_ptr(fx, src, src_f, dst_f));
             }
             result.unwrap()
         }
@@ -82,8 +88,8 @@ pub fn unsize_thin_ptr<'a, 'tcx: 'a>(
 
 /// Coerce `src`, which is a reference to a value of type `src_ty`,
 /// to a value of type `dst_ty` and store the result in `dst`
-pub fn coerce_unsized_into<'a, 'tcx: 'a>(
-    fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
+pub(crate) fn coerce_unsized_into<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
     src: CValue<'tcx>,
     dst: CPlace<'tcx>,
 ) {
@@ -99,11 +105,11 @@ pub fn coerce_unsized_into<'a, 'tcx: 'a>(
             src.load_scalar_pair(fx)
         } else {
             let base = src.load_scalar(fx);
-            unsize_thin_ptr(fx, base, src_ty, dst_ty)
+            unsize_thin_ptr(fx, base, src.layout(), dst.layout())
         };
         dst.write_cvalue(fx, CValue::by_val_pair(base, info, dst.layout()));
     };
-    match (&src_ty.sty, &dst_ty.sty) {
+    match (&src_ty.kind(), &dst_ty.kind()) {
         (&ty::Ref(..), &ty::Ref(..))
         | (&ty::Ref(..), &ty::RawPtr(..))
         | (&ty::RawPtr(..), &ty::RawPtr(..)) => coerce_ptr(),
@@ -135,12 +141,11 @@ pub fn coerce_unsized_into<'a, 'tcx: 'a>(
 
 // Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/glue.rs
 
-pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
-    fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
-    ty: Ty<'tcx>,
+pub(crate) fn size_and_align_of_dst<'tcx>(
+    fx: &mut FunctionCx<'_, 'tcx, impl Module>,
+    layout: TyAndLayout<'tcx>,
     info: Value,
 ) -> (Value, Value) {
-    let layout = fx.layout_of(ty);
     if !layout.is_unsized() {
         let size = fx
             .bcx
@@ -152,7 +157,7 @@ pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
             .iconst(fx.pointer_type, layout.align.abi.bytes() as i64);
         return (size, align);
     }
-    match ty.sty {
+    match layout.ty.kind() {
         ty::Dynamic(..) => {
             // load size/align from vtable
             (
@@ -175,7 +180,7 @@ pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
             // First get the size of all statically known fields.
             // Don't use size_of because it also rounds up to alignment, which we
             // want to avoid, as the unsized field's alignment could be smaller.
-            assert!(!ty.is_simd());
+            assert!(!layout.ty.is_simd());
 
             let i = layout.fields.count() - 1;
             let sized_size = layout.fields.offset(i).bytes();
@@ -184,8 +189,8 @@ pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
 
             // Recurse to get the size of the dynamically sized field (must be
             // the last field).
-            let field_ty = layout.field(fx, i).ty;
-            let (unsized_size, mut unsized_align) = size_and_align_of_dst(fx, field_ty, info);
+            let field_layout = layout.field(fx, i);
+            let (unsized_size, mut unsized_align) = size_and_align_of_dst(fx, field_layout, info);
 
             // FIXME (#26403, #27023): We should be adding padding
             // to `sized_size` (to accommodate the `unsized_align`
@@ -198,7 +203,7 @@ pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
             let size = fx.bcx.ins().iadd_imm(unsized_size, sized_size as i64);
 
             // Packed types ignore the alignment of their fields.
-            if let ty::Adt(def, _) = ty.sty {
+            if let ty::Adt(def, _) = layout.ty.kind() {
                 if def.repr.packed() {
                     unsized_align = sized_align;
                 }
@@ -224,8 +229,7 @@ pub fn size_and_align_of_dst<'a, 'tcx: 'a>(
             //   `(size + (align-1)) & -align`
             let addend = fx.bcx.ins().iadd_imm(align, -1);
             let add = fx.bcx.ins().iadd(size, addend);
-            let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
-            let neg = fx.bcx.ins().isub(zero, align);
+            let neg = fx.bcx.ins().ineg(align);
             let size = fx.bcx.ins().band(add, neg);
 
             (size, align)