@@ -242,11 +242,7 @@ impl CodeInfo {
242242 normalize_jumps ( & mut self . blocks ) ;
243243 reorder_conditional_exit_and_jump_blocks ( & mut self . blocks ) ;
244244 reorder_conditional_jump_and_exit_blocks ( & mut self . blocks ) ;
245- // This reordering improves some bytecode layouts, but it can detach
246- // cleanup jump targets in nested try/except/finally shapes such as
247- // Lib/poplib.py:POP3.close(). Keep it disabled until the layout update
248- // is made target-safe for exception cleanup segments.
249- // reorder_jump_over_exception_cleanup_blocks(&mut self.blocks);
245+ reorder_jump_over_exception_cleanup_blocks ( & mut self . blocks ) ;
250246 inline_small_or_no_lineno_blocks ( & mut self . blocks ) ;
251247 self . dce ( ) ; // re-run within-block DCE after normalize_jumps creates new instructions
252248 self . eliminate_unreachable_blocks ( ) ;
@@ -261,7 +257,7 @@ impl CodeInfo {
261257 jump_threading_unconditional ( & mut self . blocks ) ;
262258 reorder_conditional_exit_and_jump_blocks ( & mut self . blocks ) ;
263259 reorder_conditional_jump_and_exit_blocks ( & mut self . blocks ) ;
264- // reorder_jump_over_exception_cleanup_blocks(&mut self.blocks);
260+ reorder_jump_over_exception_cleanup_blocks ( & mut self . blocks ) ;
265261 self . eliminate_unreachable_blocks ( ) ;
266262 remove_redundant_nops_and_jumps ( & mut self . blocks ) ;
267263 self . add_checks_for_loads_of_uninitialized_variables ( ) ;
@@ -3559,6 +3555,13 @@ fn reorder_jump_over_exception_cleanup_blocks(blocks: &mut [Block]) {
35593555 current = next;
35603556 continue ;
35613557 }
3558+ // Keep the target anchored to the first target block. If we have to
3559+ // skip leading empty blocks here, reordering can leave the jump shape
3560+ // inconsistent in nested cleanup chains such as poplib.POP3.close().
3561+ if target_start != target {
3562+ current = next;
3563+ continue ;
3564+ }
35623565
35633566 let mut cleanup_end = BlockIdx :: NULL ;
35643567 let mut saw_exceptional = false ;
0 commit comments