Skip to content

Commit 4129f0c

Browse files
committed
Fix async for compilation
1 parent 235074f commit 4129f0c

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

bytecode/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ impl Instruction {
978978
}
979979
GetAIter => 0,
980980
GetANext => 1,
981-
EndAsyncFor => -1,
981+
EndAsyncFor => -2,
982982
}
983983
}
984984

compiler/src/compile.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,22 +1391,18 @@ impl Compiler {
13911391
// The thing iterated:
13921392
self.compile_expression(iter)?;
13931393

1394-
let check_asynciter_block = if is_async {
1395-
let check_asynciter_block = self.new_block();
1396-
1394+
if is_async {
13971395
self.emit(Instruction::GetAIter);
13981396

13991397
self.switch_to_block(for_block);
14001398
self.emit(Instruction::SetupExcept {
1401-
handler: check_asynciter_block,
1399+
handler: else_block,
14021400
});
14031401
self.emit(Instruction::GetANext);
14041402
self.emit_constant(ConstantData::None);
14051403
self.emit(Instruction::YieldFrom);
14061404
self.compile_store(target)?;
14071405
self.emit(Instruction::PopBlock);
1408-
1409-
Some(check_asynciter_block)
14101406
} else {
14111407
// Retrieve Iterator
14121408
self.emit(Instruction::GetIter);
@@ -1416,7 +1412,6 @@ impl Compiler {
14161412

14171413
// Start of loop iteration, set targets:
14181414
self.compile_store(target)?;
1419-
None
14201415
};
14211416

14221417
let was_in_loop = self.ctx.loop_data;
@@ -1425,12 +1420,10 @@ impl Compiler {
14251420
self.ctx.loop_data = was_in_loop;
14261421
self.emit(Instruction::Jump { target: for_block });
14271422

1428-
if let Some(check_asynciter_block) = check_asynciter_block {
1429-
self.switch_to_block(check_asynciter_block);
1423+
self.switch_to_block(else_block);
1424+
if is_async {
14301425
self.emit(Instruction::EndAsyncFor);
14311426
}
1432-
1433-
self.switch_to_block(else_block);
14341427
self.emit(Instruction::PopBlock);
14351428
self.compile_statements(orelse)?;
14361429

vm/src/frame.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,7 @@ impl ExecutingFrame<'_> {
904904
}
905905
bytecode::Instruction::EndAsyncFor => {
906906
let exc = self.pop_value();
907+
self.pop_value(); // async iterator we were calling __anext__ on
907908
if exc.isinstance(&vm.ctx.exceptions.stop_async_iteration) {
908909
vm.take_exception().expect("Should have exception in stack");
909910
Ok(None)

0 commit comments

Comments
 (0)