Skip to content

Commit a7fee5a

Browse files
committed
Define generator discriminant type in only one place
1 parent b3934b6 commit a7fee5a

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/librustc/ty/sty.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ impl<'tcx> GeneratorSubsts<'tcx> {
467467
}
468468

469469
impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
470+
/// The type of the state "discriminant" used in the generator type.
471+
pub fn discr_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
472+
tcx.types.u32
473+
}
474+
470475
/// This returns the types of the MIR locals which had to be stored across suspension points.
471476
/// It is calculated in rustc_mir::transform::generator::StateTransform.
472477
/// All the types here must be in the tuple in GeneratorInterior.
@@ -481,15 +486,15 @@ impl<'a, 'gcx, 'tcx> GeneratorSubsts<'tcx> {
481486

482487
/// This is the types of the fields of a generate which
483488
/// is available before the generator transformation.
484-
/// It includes the upvars and the state discriminant which is u32.
489+
/// It includes the upvars and the state discriminant.
485490
pub fn pre_transforms_tys(self, def_id: DefId, tcx: TyCtxt<'a, 'gcx, 'tcx>) ->
486491
impl Iterator<Item=Ty<'tcx>> + 'a
487492
{
488-
self.upvar_tys(def_id, tcx).chain(iter::once(tcx.types.u32))
493+
self.upvar_tys(def_id, tcx).chain(iter::once(self.discr_ty(tcx)))
489494
}
490495

491496
/// This is the types of all the fields stored in a generator.
492-
/// It includes the upvars, state types and the state discriminant which is u32.
497+
/// It includes the upvars, state types and the state discriminant.
493498
pub fn field_tys(self, def_id: DefId, tcx: TyCtxt<'a, 'gcx, 'tcx>) ->
494499
impl Iterator<Item=Ty<'tcx>> + Captures<'gcx> + 'a
495500
{

src/librustc_mir/build/expr/as_rvalue.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,16 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
255255
let movability = movability.unwrap();
256256
// Add the state operand since it follows the upvars in the generator
257257
// struct. See librustc_mir/transform/generator.rs for more details.
258+
let discr_ty = substs.discr_ty(this.hir.tcx());
258259
operands.push(Operand::Constant(box Constant {
259260
span: expr_span,
260-
ty: this.hir.tcx().types.u32,
261+
ty: discr_ty,
261262
user_ty: None,
262263
literal: this.hir.tcx().mk_const(
263264
ty::Const::from_bits(
264265
this.hir.tcx(),
265266
0,
266-
ty::ParamEnv::empty().and(this.hir.tcx().types.u32),
267+
ty::ParamEnv::empty().and(discr_ty),
267268
),
268269
),
269270
}));

src/librustc_mir/transform/generator.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ struct TransformVisitor<'a, 'tcx: 'a> {
166166
// The index of the generator state in the generator struct
167167
state_field: usize,
168168

169+
// The type of the generator state in the generator struct
170+
discr_ty: Ty<'tcx>,
171+
169172
// Mapping from Local to (type of local, generator struct index)
170173
// FIXME(eddyb) This should use `IndexVec<Local, Option<_>>`.
171174
remap: FxHashMap<Local, (Ty<'tcx>, usize)>,
@@ -200,15 +203,15 @@ impl<'a, 'tcx> TransformVisitor<'a, 'tcx> {
200203

201204
// Create a statement which changes the generator state
202205
fn set_state(&self, state_disc: u32, source_info: SourceInfo) -> Statement<'tcx> {
203-
let state = self.make_field(self.state_field, self.tcx.types.u32);
206+
let state = self.make_field(self.state_field, self.discr_ty);
204207
let val = Operand::Constant(box Constant {
205208
span: source_info.span,
206-
ty: self.tcx.types.u32,
209+
ty: self.discr_ty,
207210
user_ty: None,
208211
literal: self.tcx.mk_const(ty::Const::from_bits(
209212
self.tcx,
210213
state_disc.into(),
211-
ty::ParamEnv::empty().and(self.tcx.types.u32)
214+
ty::ParamEnv::empty().and(self.discr_ty)
212215
)),
213216
});
214217
Statement {
@@ -889,10 +892,11 @@ impl MirPass for StateTransform {
889892
let gen_ty = mir.local_decls.raw[1].ty;
890893

891894
// Get the interior types and substs which typeck computed
892-
let (upvars, interior, movable) = match gen_ty.sty {
895+
let (upvars, interior, discr_ty, movable) = match gen_ty.sty {
893896
ty::Generator(_, substs, movability) => {
894897
(substs.upvar_tys(def_id, tcx).collect(),
895898
substs.witness(def_id, tcx),
899+
substs.discr_ty(tcx),
896900
movability == hir::GeneratorMovability::Movable)
897901
}
898902
_ => bug!(),
@@ -937,6 +941,7 @@ impl MirPass for StateTransform {
937941
suspension_points: Vec::new(),
938942
new_ret_local,
939943
state_field,
944+
discr_ty,
940945
};
941946
transform.visit_mir(mir);
942947

0 commit comments

Comments
 (0)