diff --git a/Lib/_opcode_metadata.py b/Lib/_opcode_metadata.py index 3345870e4ec..f219132b9c3 100644 --- a/Lib/_opcode_metadata.py +++ b/Lib/_opcode_metadata.py @@ -218,13 +218,9 @@ 'BUILD_CONST_KEY_MAP': 215, 'BREAK': 216, 'CONTINUE': 217, - 'JUMP_IF_FALSE_OR_POP': 218, - 'JUMP_IF_TRUE_OR_POP': 219, 'JUMP_IF_NOT_EXC_MATCH': 220, - 'LOAD_ASSERTION_ERROR': 221, 'RETURN_CONST': 222, 'SET_EXC_INFO': 223, - 'SUBSCRIPT': 224, 'INSTRUMENTED_END_FOR': 234, 'INSTRUMENTED_POP_ITER': 235, 'INSTRUMENTED_END_SEND': 236, diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index a72e425e290..93a759c7ff4 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -489,7 +489,7 @@ impl Compiler { match ctx { ast::ExprContext::Load => { emit!(self, Instruction::BuildSlice { argc }); - emit!(self, Instruction::Subscript); + emit!(self, Instruction::BinarySubscr); } ast::ExprContext::Store => { emit!(self, Instruction::BuildSlice { argc }); @@ -503,7 +503,7 @@ impl Compiler { // Emit appropriate instruction based on context match ctx { - ast::ExprContext::Load => emit!(self, Instruction::Subscript), + ast::ExprContext::Load => emit!(self, Instruction::BinarySubscr), ast::ExprContext::Store => emit!(self, Instruction::StoreSubscr), ast::ExprContext::Del => emit!(self, Instruction::DeleteSubscr), ast::ExprContext::Invalid => { @@ -4929,6 +4929,9 @@ impl Compiler { if is_async { emit!(self, Instruction::EndAsyncFor); + } else { + // Pop the iterator after loop ends + emit!(self, Instruction::PopTop); } self.compile_statements(orelse)?; @@ -5987,14 +5990,9 @@ impl Compiler { self.compile_addcompare(op); // if comparison result is false, we break with this value; if true, try the next one. - /* emit!(self, Instruction::Copy { index: 1 }); - // emit!(self, Instruction::ToBool); // TODO: Uncomment this emit!(self, Instruction::PopJumpIfFalse { target: cleanup }); emit!(self, Instruction::PopTop); - */ - - emit!(self, Instruction::JumpIfFalseOrPop { target: cleanup }); } self.compile_expression(last_comparator)?; @@ -6205,7 +6203,7 @@ impl Compiler { self.compile_expression(slice)?; emit!(self, Instruction::Copy { index: 2_u32 }); emit!(self, Instruction::Copy { index: 2_u32 }); - emit!(self, Instruction::Subscript); + emit!(self, Instruction::BinarySubscr); AugAssignKind::Subscript } ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => { @@ -7367,6 +7365,8 @@ impl Compiler { if is_async { emit!(self, Instruction::EndAsyncFor); emit!(self, Instruction::PopTop); + } else { + emit!(self, Instruction::PopTop); } } diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap index c4d2ed83796..eeef8ea4b3f 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap @@ -1,5 +1,6 @@ --- source: crates/codegen/src/compile.rs +assertion_line: 8626 expression: "compile_exec(\"\\\nasync def test():\n for stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\")" --- 3 0 LOAD_CONST (): 1 0 RESUME (0) @@ -140,7 +141,8 @@ expression: "compile_exec(\"\\\nasync def test():\n for stop_exc in (StopIter 126 POP_EXCEPT 127 RERAISE (1) >> 128 JUMP_BACKWARD (11) - >> 129 RETURN_CONST (None) + >> 129 POP_TOP + 130 RETURN_CONST (None) 1 MAKE_FUNCTION 2 STORE_NAME (0, test) diff --git a/crates/compiler-core/src/bytecode/instruction.rs b/crates/compiler-core/src/bytecode/instruction.rs index 41f86dca39b..d8ba8da7a01 100644 --- a/crates/compiler-core/src/bytecode/instruction.rs +++ b/crates/compiler-core/src/bytecode/instruction.rs @@ -272,19 +272,11 @@ pub enum Instruction { Continue { target: Arg