From c5f4880e9153d07cd95fa53221a0d976934cb938 Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Sat, 18 Jun 2022 17:19:24 +0000 Subject: [PATCH] Drop magic value 3 from code Magic value 3 is used to create state for a yield point. It is in fact the number of reserved variants. Lift RESERVED_VARIANTS out to module scope and use it instead. --- compiler/rustc_mir_transform/src/generator.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_mir_transform/src/generator.rs b/compiler/rustc_mir_transform/src/generator.rs index 7f0d3b0a612..d777d13d7a5 100644 --- a/compiler/rustc_mir_transform/src/generator.rs +++ b/compiler/rustc_mir_transform/src/generator.rs @@ -195,6 +195,11 @@ fn replace_base<'tcx>(place: &mut Place<'tcx>, new_base: Place<'tcx>, tcx: TyCtx /// Generator has panicked and is poisoned. const POISONED: usize = GeneratorSubsts::POISONED; +/// Number of variants to reserve in generator state. Corresponds to +/// `UNRESUMED` (beginning of a generator) and `RETURNED`/`POISONED` +/// (end of a generator) states. +const RESERVED_VARIANTS: usize = 3; + /// A `yield` point in the generator. struct SuspensionPoint<'tcx> { /// State discriminant used when suspending or resuming at this point. @@ -345,7 +350,7 @@ fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockDat data.statements.extend(self.make_state(state_idx, v, source_info)); let state = if let Some((resume, mut resume_arg)) = resume { // Yield - let state = 3 + self.suspension_points.len(); + let state = RESERVED_VARIANTS + self.suspension_points.len(); // The resume arg target location might itself be remapped if its base local is // live across a yield. @@ -792,7 +797,6 @@ fn compute_layout<'tcx>( // Leave empty variants for the UNRESUMED, RETURNED, and POISONED states. // In debuginfo, these will correspond to the beginning (UNRESUMED) or end // (RETURNED, POISONED) of the function. - const RESERVED_VARIANTS: usize = 3; let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span; let mut variant_source_info: IndexVec = [ SourceInfo::outermost(body_span.shrink_to_lo()),