Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,18 +2547,28 @@ impl Compiler {
)?;
}
self.compile_statements(finalbody)?;
// RERAISE 0 is emitted BEFORE pop_fblock
// This ensures RERAISE goes to cleanup block (FinallyEnd handler)
// which then properly restores prev_exc before going to outer handler

// Pop FinallyEnd fblock BEFORE emitting RERAISE
// This ensures RERAISE routes to outer exception handler, not cleanup block
// Cleanup block is only for new exceptions raised during finally body execution
if finally_cleanup_block.is_some() {
self.pop_fblock(FBlockType::FinallyEnd);
}

// Restore prev_exc as current exception before RERAISE
// Stack: [prev_exc, exc] -> COPY 2 -> [prev_exc, exc, prev_exc]
// POP_EXCEPT pops prev_exc and sets exc_info->exc_value = prev_exc
// Stack after POP_EXCEPT: [prev_exc, exc]
emit!(self, Instruction::Copy { index: 2_u32 });
emit!(self, Instruction::PopExcept);

// RERAISE 0: re-raise the original exception to outer handler
emit!(
self,
Instruction::RaiseVarargs {
kind: bytecode::RaiseKind::ReraiseFromStack
}
);
if finally_cleanup_block.is_some() {
self.pop_fblock(FBlockType::FinallyEnd);
}
}

if let Some(cleanup) = finally_cleanup_block {
Expand Down Expand Up @@ -2837,21 +2847,28 @@ impl Compiler {
// Run finally body
self.compile_statements(finalbody)?;

// RERAISE 0 is emitted BEFORE pop_fblock
// This ensures RERAISE goes to cleanup block (FinallyEnd handler)
// which then properly restores prev_exc before going to outer handler
// RERAISE 0: reraise the exception on TOS
// Pop FinallyEnd fblock BEFORE emitting RERAISE
// This ensures RERAISE routes to outer exception handler, not cleanup block
// Cleanup block is only for new exceptions raised during finally body execution
if finally_cleanup_block.is_some() {
self.pop_fblock(FBlockType::FinallyEnd);
}

// Restore prev_exc as current exception before RERAISE
// Stack: [lasti, prev_exc, exc] -> COPY 2 -> [lasti, prev_exc, exc, prev_exc]
// POP_EXCEPT pops prev_exc and sets exc_info->exc_value = prev_exc
// Stack after POP_EXCEPT: [lasti, prev_exc, exc]
emit!(self, Instruction::Copy { index: 2_u32 });
emit!(self, Instruction::PopExcept);

// RERAISE 0: re-raise the original exception to outer handler
// Stack: [lasti, prev_exc, exc] - exception is on top
emit!(
self,
Instruction::RaiseVarargs {
kind: bytecode::RaiseKind::ReraiseFromStack,
}
);

if finally_cleanup_block.is_some() {
self.pop_fblock(FBlockType::FinallyEnd);
}
}

// finally cleanup block
Expand Down