Skip to content

Commit eac1493

Browse files
authored
Rollup merge of rust-lang#98235 - liuw:mir-gen-drop-magic-value, r=davidtwco
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.
2 parents b12708f + c5f4880 commit eac1493

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

compiler/rustc_mir_transform/src/generator.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ const RETURNED: usize = GeneratorSubsts::RETURNED;
195195
/// Generator has panicked and is poisoned.
196196
const POISONED: usize = GeneratorSubsts::POISONED;
197197

198+
/// Number of variants to reserve in generator state. Corresponds to
199+
/// `UNRESUMED` (beginning of a generator) and `RETURNED`/`POISONED`
200+
/// (end of a generator) states.
201+
const RESERVED_VARIANTS: usize = 3;
202+
198203
/// A `yield` point in the generator.
199204
struct SuspensionPoint<'tcx> {
200205
/// State discriminant used when suspending or resuming at this point.
@@ -345,7 +350,7 @@ impl<'tcx> MutVisitor<'tcx> for TransformVisitor<'tcx> {
345350
data.statements.extend(self.make_state(state_idx, v, source_info));
346351
let state = if let Some((resume, mut resume_arg)) = resume {
347352
// Yield
348-
let state = 3 + self.suspension_points.len();
353+
let state = RESERVED_VARIANTS + self.suspension_points.len();
349354

350355
// The resume arg target location might itself be remapped if its base local is
351356
// live across a yield.
@@ -792,7 +797,6 @@ fn compute_layout<'tcx>(
792797
// Leave empty variants for the UNRESUMED, RETURNED, and POISONED states.
793798
// In debuginfo, these will correspond to the beginning (UNRESUMED) or end
794799
// (RETURNED, POISONED) of the function.
795-
const RESERVED_VARIANTS: usize = 3;
796800
let body_span = body.source_scopes[OUTERMOST_SOURCE_SCOPE].span;
797801
let mut variant_source_info: IndexVec<VariantIdx, SourceInfo> = [
798802
SourceInfo::outermost(body_span.shrink_to_lo()),

0 commit comments

Comments
 (0)