Skip to content

Commit e51b400

Browse files
authored
gh-113054: Compiler no longer replaces a redundant jump with no line number by a NOP (#113139)
1 parent 76d757b commit e51b400

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/test/test_compile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ def f():
444444
self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
445445
self.assertIn("__package__", A.f.__code__.co_varnames)
446446

447+
def test_condition_expression_with_dead_blocks_compiles(self):
448+
# See gh-113054
449+
compile('if (5 if 5 else T): 0', '<eval>', 'exec')
450+
447451
def test_compile_invalid_namedexpr(self):
448452
# gh-109351
449453
m = ast.Module(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed bug where a redundant NOP is not removed, causing an assertion to fail
2+
in the compiler in debug mode.

Python/flowgraph.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,10 @@ remove_redundant_jumps(cfg_builder *g) {
11101110
* of that jump. If it is, then the jump instruction is redundant and
11111111
* can be deleted.
11121112
*/
1113+
11131114
assert(no_empty_basic_blocks(g));
1115+
1116+
bool remove_empty_blocks = false;
11141117
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
11151118
cfg_instr *last = basicblock_last_instr(b);
11161119
assert(last != NULL);
@@ -1122,10 +1125,22 @@ remove_redundant_jumps(cfg_builder *g) {
11221125
}
11231126
if (last->i_target == b->b_next) {
11241127
assert(b->b_next->b_iused);
1125-
INSTR_SET_OP0(last, NOP);
1128+
if (last->i_loc.lineno == NO_LOCATION.lineno) {
1129+
b->b_iused--;
1130+
if (b->b_iused == 0) {
1131+
remove_empty_blocks = true;
1132+
}
1133+
}
1134+
else {
1135+
INSTR_SET_OP0(last, NOP);
1136+
}
11261137
}
11271138
}
11281139
}
1140+
if (remove_empty_blocks) {
1141+
eliminate_empty_basic_blocks(g);
1142+
}
1143+
assert(no_empty_basic_blocks(g));
11291144
return SUCCESS;
11301145
}
11311146

0 commit comments

Comments
 (0)