@@ -2629,24 +2629,30 @@ impl Compiler {
26292629 compile_element : & dyn Fn ( & mut Self ) -> CompileResult < ( ) > ,
26302630 ) -> CompileResult < ( ) > {
26312631 let prev_ctx = self . ctx ;
2632+ let is_async = generators. iter ( ) . any ( |g| g. is_async ) ;
26322633
26332634 self . ctx = CompileContext {
26342635 loop_data : None ,
26352636 in_class : prev_ctx. in_class ,
2636- func : FunctionContext :: Function ,
2637+ func : if is_async {
2638+ FunctionContext :: AsyncFunction
2639+ } else {
2640+ FunctionContext :: Function
2641+ } ,
26372642 } ;
26382643
26392644 // We must have at least one generator:
26402645 assert ! ( !generators. is_empty( ) ) ;
26412646
2647+ let flags = bytecode:: CodeFlags :: NEW_LOCALS | bytecode:: CodeFlags :: IS_OPTIMIZED ;
2648+ let flags = if is_async {
2649+ flags | bytecode:: CodeFlags :: IS_COROUTINE
2650+ } else {
2651+ flags
2652+ } ;
2653+
26422654 // Create magnificent function <listcomp>:
2643- self . push_output (
2644- bytecode:: CodeFlags :: NEW_LOCALS | bytecode:: CodeFlags :: IS_OPTIMIZED ,
2645- 1 ,
2646- 1 ,
2647- 0 ,
2648- name. to_owned ( ) ,
2649- ) ;
2655+ self . push_output ( flags, 1 , 1 , 0 , name. to_owned ( ) ) ;
26502656 let arg0 = self . varname ( ".0" ) ?;
26512657
26522658 let return_none = init_collection. is_none ( ) ;
@@ -2656,11 +2662,12 @@ impl Compiler {
26562662 }
26572663
26582664 let mut loop_labels = vec ! [ ] ;
2659- let mut is_async = false ;
26602665 for generator in generators {
26612666 let loop_block = self . new_block ( ) ;
26622667 let after_block = self . new_block ( ) ;
26632668
2669+ // emit!(self, Instruction::SetupLoop);
2670+
26642671 if loop_labels. is_empty ( ) {
26652672 // Load iterator onto stack (passed as first argument):
26662673 emit ! ( self , Instruction :: LoadFast ( arg0) ) ;
@@ -2679,13 +2686,16 @@ impl Compiler {
26792686 loop_labels. push ( ( loop_block, after_block) ) ;
26802687 self . switch_to_block ( loop_block) ;
26812688 if generator. is_async {
2682- is_async = true ;
2683- emit ! ( self , Instruction :: SetupExcept {
2684- handler: after_block,
2685- } ) ;
2689+ emit ! (
2690+ self ,
2691+ Instruction :: SetupExcept {
2692+ handler: after_block,
2693+ }
2694+ ) ;
26862695 emit ! ( self , Instruction :: GetANext ) ;
26872696 self . emit_constant ( ConstantData :: None ) ;
26882697 emit ! ( self , Instruction :: YieldFrom ) ;
2698+ self . compile_store ( & generator. target ) ?;
26892699 emit ! ( self , Instruction :: PopBlock ) ;
26902700 } else {
26912701 emit ! (
@@ -2694,10 +2704,9 @@ impl Compiler {
26942704 target: after_block,
26952705 }
26962706 ) ;
2707+ self . compile_store ( & generator. target ) ?;
26972708 }
26982709
2699- self . compile_store ( & generator. target ) ?;
2700-
27012710 // Now evaluate the ifs:
27022711 for if_condition in & generator. ifs {
27032712 self . compile_jump_if ( if_condition, false , loop_block) ?
0 commit comments