about summary refs log tree commit diff
path: root/src/librustc
diff options
context:
space:
mode:
authorJonas Schievink <jonasschievink@gmail.com>2020-03-10 00:17:33 +0100
committerJonas Schievink <jonasschievink@gmail.com>2020-03-14 14:09:48 +0100
commitb2779d85962b520bb8718b371e7264aa44827058 (patch)
tree17b2b7b165f1f268231d65d9b64ef12e2f76d08b /src/librustc
parent42ce9b4e9054d63c8f352f994e2d5dae7d04130f (diff)
downloadrust-b2779d85962b520bb8718b371e7264aa44827058.tar.gz
rust-b2779d85962b520bb8718b371e7264aa44827058.zip
Use smaller discriminants for generators
Diffstat (limited to 'src/librustc')
-rw-r--r--src/librustc/ty/layout.rs15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs
index dedb3035ced..f5ef9bda0ee 100644
--- a/src/librustc/ty/layout.rs
+++ b/src/librustc/ty/layout.rs
@@ -1409,12 +1409,15 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
         // locals as part of the prefix. We compute the layout of all of
         // these fields at once to get optimal packing.
         let discr_index = substs.as_generator().prefix_tys(def_id, tcx).count();
-        // FIXME(eddyb) set the correct vaidity range for the discriminant.
-        let discr_layout = self.layout_of(substs.as_generator().discr_ty(tcx))?;
-        let discr = match &discr_layout.abi {
-            Abi::Scalar(s) => s.clone(),
-            _ => bug!(),
-        };
+
+        // `info.variant_fields` already accounts for the reserved variants, so no need to add them.
+        let max_discr = (info.variant_fields.len() - 1) as u128;
+        let discr_int = Integer::fit_unsigned(max_discr);
+        let discr_int_ty = discr_int.to_ty(tcx, false);
+        let discr = Scalar { value: Primitive::Int(discr_int, false), valid_range: 0..=max_discr };
+        let discr_layout = self.tcx.intern_layout(LayoutDetails::scalar(self, discr.clone()));
+        let discr_layout = TyLayout { ty: discr_int_ty, details: discr_layout };
+
         let promoted_layouts = ineligible_locals
             .iter()
             .map(|local| subst_field(info.field_tys[local]))