]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/ty/layout.rs
Rollup merge of #44562 - eddyb:ugh-rustdoc, r=nikomatsakis
[rust.git] / src / librustc / ty / layout.rs
index cf21a66d51538f9d66147b9185783030d17e7cfe..84d7745a64f0a2218f81ff4d646da86b9553f602 100644 (file)
@@ -386,7 +386,7 @@ pub fn align<C: HasDataLayout>(&self, cx: C) -> Align {
         }
     }
 
-    pub fn to_ty<'a, 'tcx>(&self, tcx: &ty::TyCtxt<'a, 'tcx, 'tcx>,
+    pub fn to_ty<'a, 'tcx>(&self, tcx: &TyCtxt<'a, 'tcx, 'tcx>,
                            signed: bool) -> Ty<'tcx> {
         match (*self, signed) {
             (I1, false) => tcx.types.u8,
@@ -837,12 +837,22 @@ fn non_zero_field_in_type(tcx: TyCtxt<'a, 'tcx, 'tcx>,
 
             // Is this a fixed-size array of something non-zero
             // with at least one element?
-            (_, &ty::TyArray(ety, d)) if d > 0 => {
-                Struct::non_zero_field_paths(
-                    tcx,
-                    param_env,
-                    Some(ety).into_iter(),
-                    None)
+            (_, &ty::TyArray(ety, mut count)) => {
+                if count.has_projections() {
+                    count = tcx.normalize_associated_type_in_env(&count, param_env);
+                    if count.has_projections() {
+                        return Err(LayoutError::Unknown(ty));
+                    }
+                }
+                if count.val.to_const_int().unwrap().to_u64().unwrap() != 0 {
+                    Struct::non_zero_field_paths(
+                        tcx,
+                        param_env,
+                        Some(ety).into_iter(),
+                        None)
+                } else {
+                    Ok(None)
+                }
             }
 
             (_, &ty::TyProjection(_)) | (_, &ty::TyAnon(..)) => {
@@ -1174,12 +1184,17 @@ pub fn compute_uncached(tcx: TyCtxt<'a, 'tcx, 'tcx>,
             }
 
             // Arrays and slices.
-            ty::TyArray(element, count) => {
+            ty::TyArray(element, mut count) => {
+                if count.has_projections() {
+                    count = tcx.normalize_associated_type_in_env(&count, param_env);
+                    if count.has_projections() {
+                        return Err(LayoutError::Unknown(ty));
+                    }
+                }
+
                 let element = element.layout(tcx, param_env)?;
                 let element_size = element.size(dl);
-                // FIXME(eddyb) Don't use host `usize` for array lengths.
-                let usize_count: usize = count;
-                let count = usize_count as u64;
+                let count = count.val.to_const_int().unwrap().to_u64().unwrap();
                 if element_size.checked_mul(count, dl).is_none() {
                     return Err(LayoutError::SizeOverflow(ty));
                 }
@@ -1344,7 +1359,7 @@ pub fn compute_uncached(tcx: TyCtxt<'a, 'tcx, 'tcx>,
                     } else {
                         let st = Struct::new(dl, &fields, &def.repr,
                           kind, ty)?;
-                        let non_zero = Some(def.did) == tcx.lang_items.non_zero();
+                        let non_zero = Some(def.did) == tcx.lang_items().non_zero();
                         Univariant { variant: st, non_zero: non_zero }
                     };
                     return success(layout);
@@ -2043,7 +2058,7 @@ pub fn compute(ty: Ty<'tcx>,
                     if let Some(SizeSkeleton::Pointer { non_zero, tail }) = v0 {
                         return Ok(SizeSkeleton::Pointer {
                             non_zero: non_zero ||
-                                Some(def.did) == tcx.lang_items.non_zero(),
+                                Some(def.did) == tcx.lang_items().non_zero(),
                             tail,
                         });
                     } else {