diff options
| author | Wei Liu <liuw@liuw.name> | 2022-06-18 17:19:24 +0000 |
|---|---|---|
| committer | Wei Liu <liuw@liuw.name> | 2022-06-20 10:05:04 +0000 |
| commit | c5f4880e9153d07cd95fa53221a0d976934cb938 (patch) | |
| tree | f285a9016793bdabc7a1f1d42705fc3bbe6d0a58 | |
| parent | 2cec6874c086a8fa107115dcca73d93228a3bef6 (diff) | |
| download | rust-c5f4880e9153d07cd95fa53221a0d976934cb938.tar.gz rust-c5f4880e9153d07cd95fa53221a0d976934cb938.zip | |
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.
| -rw-r--r-- | compiler/rustc_mir_transform/src/generator.rs | 8 |
1 files 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 @@ const RETURNED: usize = GeneratorSubsts::RETURNED; /// 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 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> { 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<VariantIdx, SourceInfo> = [ SourceInfo::outermost(body_span.shrink_to_lo()), |
