Skip to content

Commit

Permalink
pythongh-109889: fix redundant NOP detection to look past NOPs with n…
Browse files Browse the repository at this point in the history
…o lineno when looking for the next instruction's lineno
  • Loading branch information
iritkatriel committed Sep 27, 2023
1 parent cc54bcf commit 10cc46f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 10cc46f

Please sign in to comment.