@@ -1376,9 +1376,8 @@ impl Compiler {
13761376 // The thing iterated:
13771377 self . compile_expression ( iter) ?;
13781378
1379- if is_async {
1379+ let check_asynciter_block = if is_async {
13801380 let check_asynciter_block = self . new_block ( ) ;
1381- let body_block = self . new_block ( ) ;
13821381
13831382 self . emit ( Instruction :: GetAIter ) ;
13841383
@@ -1391,21 +1390,8 @@ impl Compiler {
13911390 self . emit ( Instruction :: YieldFrom ) ;
13921391 self . compile_store ( target) ?;
13931392 self . emit ( Instruction :: PopBlock ) ;
1394- self . emit ( Instruction :: Jump { target : body_block } ) ;
13951393
1396- self . switch_to_block ( check_asynciter_block) ;
1397- self . emit ( Instruction :: Duplicate ) ;
1398- let stopasynciter = self . name ( "StopAsyncIteration" ) ;
1399- self . emit ( Instruction :: LoadGlobal ( stopasynciter) ) ;
1400- self . emit ( Instruction :: CompareOperation {
1401- op : bytecode:: ComparisonOperator :: ExceptionMatch ,
1402- } ) ;
1403- self . emit ( Instruction :: JumpIfTrue { target : else_block } ) ;
1404- self . emit ( Instruction :: Raise {
1405- kind : bytecode:: RaiseKind :: Reraise ,
1406- } ) ;
1407-
1408- self . switch_to_block ( body_block) ;
1394+ Some ( check_asynciter_block)
14091395 } else {
14101396 // Retrieve Iterator
14111397 self . emit ( Instruction :: GetIter ) ;
@@ -1415,24 +1401,27 @@ impl Compiler {
14151401
14161402 // Start of loop iteration, set targets:
14171403 self . compile_store ( target) ?;
1418- }
1404+ None
1405+ } ;
14191406
14201407 let was_in_loop = self . ctx . loop_data ;
14211408 self . ctx . loop_data = Some ( ( for_block, after_block) ) ;
14221409 self . compile_statements ( body) ?;
14231410 self . ctx . loop_data = was_in_loop;
14241411 self . emit ( Instruction :: Jump { target : for_block } ) ;
14251412
1413+ if let Some ( check_asynciter_block) = check_asynciter_block {
1414+ self . switch_to_block ( check_asynciter_block) ;
1415+ self . emit ( Instruction :: EndAsyncFor ) ;
1416+ }
1417+
14261418 self . switch_to_block ( else_block) ;
14271419 self . emit ( Instruction :: PopBlock ) ;
14281420 if let Some ( orelse) = orelse {
14291421 self . compile_statements ( orelse) ?;
14301422 }
14311423
14321424 self . switch_to_block ( after_block) ;
1433- if is_async {
1434- self . emit ( Instruction :: Pop ) ;
1435- }
14361425
14371426 Ok ( ( ) )
14381427 }
@@ -2169,14 +2158,13 @@ impl Compiler {
21692158 ) ;
21702159 let arg0 = self . varname ( ".0" ) ;
21712160
2161+ // if this is a generator expression, we need to insert a LoadConst(None) before we return;
2162+ // the other kinds load their collection types below
21722163 let is_genexpr = matches ! ( kind, ast:: ComprehensionKind :: GeneratorExpression { .. } ) ;
21732164
21742165 // Create empty object of proper type:
21752166 match kind {
2176- ast:: ComprehensionKind :: GeneratorExpression { .. } => {
2177- // pop the None that was passed to kickstart the generator
2178- self . emit ( Instruction :: Pop ) ;
2179- }
2167+ ast:: ComprehensionKind :: GeneratorExpression { .. } => { }
21802168 ast:: ComprehensionKind :: List { .. } => {
21812169 self . emit ( Instruction :: BuildList {
21822170 size : 0 ,
0 commit comments