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.
This commit is contained in:
Wei Liu 2022-06-18 17:19:24 +00:00
parent 2cec6874c0
commit c5f4880e91

View File

@ -195,6 +195,11 @@ fn replace_base<'tcx>(place: &mut Place<'tcx>, new_base: Place<'tcx>, tcx: TyCtx
/// Generator has panicked and is poisoned. /// Generator has panicked and is poisoned.
const POISONED: usize = GeneratorSubsts::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. /// A `yield` point in the generator.
struct SuspensionPoint<'tcx> { struct SuspensionPoint<'tcx> {
/// State discriminant used when suspending or resuming at this point. /// 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)); data.statements.extend(self.make_state(state_idx, v, source_info));
let state = if let Some((resume, mut resume_arg)) = resume { let state = if let Some((resume, mut resume_arg)) = resume {
// Yield // 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 // The resume arg target location might itself be remapped if its base local is
// live across a yield. // live across a yield.
@ -792,7 +797,6 @@ fn compute_layout<'tcx>(
// Leave empty variants for the UNRESUMED, RETURNED, and POISONED states. // Leave empty variants for the UNRESUMED, RETURNED, and POISONED states.
// In debuginfo, these will correspond to the beginning (UNRESUMED) or end // In debuginfo, these will correspond to the beginning (UNRESUMED) or end
// (RETURNED, POISONED) of the function. // (RETURNED, POISONED) of the function.
const RESERVED_VARIANTS: usize = 3;
let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span; let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span;
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [ let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [
SourceInfo::outermost(body_span.shrink_to_lo()), SourceInfo::outermost(body_span.shrink_to_lo()),