Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-113054: Compiler no longer replaces a redundant jump with no line number by a NOP #113139

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ def f():
self.assertIn("_A__mangled_mod", A.f.__code__.co_varnames)
self.assertIn("__package__", A.f.__code__.co_varnames)

def test_condition_expression_with_dead_blocks_compiles(self):
# See gh-113054
compile('if 5 if 5 else T:y', '<eval>', 'exec')
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved

def test_compile_invalid_namedexpr(self):
# gh-109351
m = ast.Module(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed bug where a redindant NOP is not removed, causing an assertion to fail
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved
in the compiler in debug mode.
17 changes: 16 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,10 @@ remove_redundant_jumps(cfg_builder *g) {
* of that jump. If it is, then the jump instruction is redundant and
* can be deleted.
*/

assert(no_empty_basic_blocks(g));

bool remove_empty_blocks = false;
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
cfg_instr *last = basicblock_last_instr(b);
assert(last != NULL);
Expand All @@ -1122,10 +1125,22 @@ remove_redundant_jumps(cfg_builder *g) {
}
if (last->i_target == b->b_next) {
assert(b->b_next->b_iused);
INSTR_SET_OP0(last, NOP);
if (last->i_loc.lineno == NO_LOCATION.lineno) {
b->b_iused--;
if (b->b_iused == 0) {
remove_empty_blocks = true;
}
}
else {
INSTR_SET_OP0(last, NOP);
}
}
}
}
if (remove_empty_blocks) {
eliminate_empty_basic_blocks(g);
}
assert(no_empty_basic_blocks(g));
return SUCCESS;
}

Expand Down
Loading