diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 53e3e8f75aa7661..c4452e38934cf88 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1278,6 +1278,11 @@ def f(x): while x: 0 if 1 else 0 + def test_remove_redundant_nop_edge_case(self): + # See gh-109889 + def f(): + a if (1 if b else c) else d + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): # Ensure that compiled code snippets have correct line and column numbers diff --git a/Python/flowgraph.c b/Python/flowgraph.c index 9fe387cc9a8e80f..bbf01c407418e50 100644 --- a/Python/flowgraph.c +++ b/Python/flowgraph.c @@ -1017,7 +1017,17 @@ remove_redundant_nops(basicblock *bb) { } /* or if last instruction in BB and next BB has same line number */ if (next) { - if (lineno == next->b_instr[0].i_loc.lineno) { + location next_loc = NO_LOCATION; + for (int j=0; j < next->b_iused; j++) { + cfg_instr *instr = &next->b_instr[j]; + if (instr->i_opcode == NOP && instr->i_loc.lineno == NO_LOCATION.lineno) { + /* Skip over NOPs without location because they will be removed */ + continue; + } + next_loc = instr->i_loc; + break; + } + if (lineno == next_loc.lineno) { continue; } }