26
26
//! }
27
27
//!
28
28
//! This pass computes the meaning of the state field and the MIR locals which are live
29
- //! across a suspension point. There are however two hardcoded generator states:
29
+ //! across a suspension point. There are however three hardcoded generator states:
30
30
//! 0 - Generator have not been resumed yet
31
31
//! 1 - Generator has returned / is completed
32
32
//! 2 - Generator has been poisoned
@@ -144,6 +144,13 @@ fn self_arg() -> Local {
144
144
Local :: new ( 1 )
145
145
}
146
146
147
+ /// Generator have not been resumed yet
148
+ const UNRESUMED : u32 = 0 ;
149
+ /// Generator has returned / is completed
150
+ const RETURNED : u32 = 1 ;
151
+ /// Generator has been poisoned
152
+ const POISONED : u32 = 2 ;
153
+
147
154
struct SuspensionPoint {
148
155
state : u32 ,
149
156
resume : BasicBlock ,
@@ -278,7 +285,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for TransformVisitor<'a, 'tcx> {
278
285
279
286
state
280
287
} else { // Return
281
- 1 // state for returned
288
+ RETURNED // state for returned
282
289
} ;
283
290
data. statements . push ( self . set_state ( state, source_info) ) ;
284
291
data. terminator . as_mut ( ) . unwrap ( ) . kind = TerminatorKind :: Return ;
@@ -643,10 +650,10 @@ fn create_generator_drop_shim<'a, 'tcx>(
643
650
644
651
let mut cases = create_cases ( & mut mir, transform, |point| point. drop ) ;
645
652
646
- cases. insert ( 0 , ( 0 , drop_clean) ) ;
653
+ cases. insert ( 0 , ( UNRESUMED , drop_clean) ) ;
647
654
648
- // The returned state (1) and the poisoned state (2) falls through to
649
- // the default case which is just to return
655
+ // The returned state and the poisoned state fall through to the default
656
+ // case which is just to return
650
657
651
658
insert_switch ( tcx, & mut mir, cases, & transform, TerminatorKind :: Return ) ;
652
659
@@ -762,7 +769,7 @@ fn create_generator_resume_function<'a, 'tcx>(
762
769
for block in mir. basic_blocks_mut ( ) {
763
770
let source_info = block. terminator ( ) . source_info ;
764
771
if let & TerminatorKind :: Resume = & block. terminator ( ) . kind {
765
- block. statements . push ( transform. set_state ( 1 , source_info) ) ;
772
+ block. statements . push ( transform. set_state ( POISONED , source_info) ) ;
766
773
}
767
774
}
768
775
@@ -773,12 +780,12 @@ fn create_generator_resume_function<'a, 'tcx>(
773
780
GeneratorResumedAfterReturn ,
774
781
} ;
775
782
776
- // Jump to the entry point on the 0 state
777
- cases. insert ( 0 , ( 0 , BasicBlock :: new ( 0 ) ) ) ;
778
- // Panic when resumed on the returned (1) state
779
- cases. insert ( 1 , ( 1 , insert_panic_block ( tcx, mir, GeneratorResumedAfterReturn ) ) ) ;
780
- // Panic when resumed on the poisoned (2) state
781
- cases. insert ( 2 , ( 2 , insert_panic_block ( tcx, mir, GeneratorResumedAfterPanic ) ) ) ;
783
+ // Jump to the entry point on the unresumed
784
+ cases. insert ( 0 , ( UNRESUMED , BasicBlock :: new ( 0 ) ) ) ;
785
+ // Panic when resumed on the returned state
786
+ cases. insert ( 1 , ( RETURNED , insert_panic_block ( tcx, mir, GeneratorResumedAfterReturn ) ) ) ;
787
+ // Panic when resumed on the poisoned state
788
+ cases. insert ( 2 , ( POISONED , insert_panic_block ( tcx, mir, GeneratorResumedAfterPanic ) ) ) ;
782
789
783
790
insert_switch ( tcx, mir, cases, & transform, TerminatorKind :: Unreachable ) ;
784
791
@@ -942,7 +949,7 @@ impl MirPass for StateTransform {
942
949
mir. generator_layout = Some ( layout) ;
943
950
944
951
// Insert `drop(generator_struct)` which is used to drop upvars for generators in
945
- // the unresumed (0) state.
952
+ // the unresumed state.
946
953
// This is expanded to a drop ladder in `elaborate_generator_drops`.
947
954
let drop_clean = insert_clean_drop ( mir) ;
948
955
0 commit comments