mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
gh-99708: fix bug where compiler crashes on if expression with an empty body block (GH-99732)
This commit is contained in:
parent
5f4ae86a63
commit
ae185fdcca
3 changed files with 29 additions and 3 deletions
|
@ -1146,6 +1146,17 @@ if 1:
|
||||||
with self.subTest(source):
|
with self.subTest(source):
|
||||||
self.assertEqual(actual_positions, expected_positions)
|
self.assertEqual(actual_positions, expected_positions)
|
||||||
|
|
||||||
|
def test_if_expression_expression_empty_block(self):
|
||||||
|
# See regression in gh-99708
|
||||||
|
exprs = [
|
||||||
|
"assert (False if 1 else True)",
|
||||||
|
"def f():\n\tif not (False if 1 else True): raise AssertionError",
|
||||||
|
"def f():\n\tif not (False if 1 else True): return 12",
|
||||||
|
]
|
||||||
|
for expr in exprs:
|
||||||
|
with self.subTest(expr=expr):
|
||||||
|
compile(expr, "<single>", "exec")
|
||||||
|
|
||||||
|
|
||||||
@requires_debug_ranges()
|
@requires_debug_ranges()
|
||||||
class TestSourcePositions(unittest.TestCase):
|
class TestSourcePositions(unittest.TestCase):
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Fix bug where compiler crashes on an if expression with an empty body block.
|
|
@ -512,7 +512,7 @@ static int compiler_match(struct compiler *, stmt_ty);
|
||||||
static int compiler_pattern_subpattern(struct compiler *,
|
static int compiler_pattern_subpattern(struct compiler *,
|
||||||
pattern_ty, pattern_context *);
|
pattern_ty, pattern_context *);
|
||||||
|
|
||||||
static void remove_redundant_nops(basicblock *bb);
|
static int remove_redundant_nops(basicblock *bb);
|
||||||
|
|
||||||
static PyCodeObject *assemble(struct compiler *, int addNone);
|
static PyCodeObject *assemble(struct compiler *, int addNone);
|
||||||
|
|
||||||
|
@ -8666,6 +8666,17 @@ static void
|
||||||
propagate_line_numbers(basicblock *entryblock);
|
propagate_line_numbers(basicblock *entryblock);
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
|
|
||||||
|
static bool
|
||||||
|
no_redundant_nops(cfg_builder *g) {
|
||||||
|
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
|
||||||
|
if (remove_redundant_nops(b) != 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
no_redundant_jumps(cfg_builder *g) {
|
no_redundant_jumps(cfg_builder *g) {
|
||||||
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
|
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
|
||||||
|
@ -9435,7 +9446,7 @@ inline_small_exit_blocks(basicblock *bb) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static int
|
||||||
remove_redundant_nops(basicblock *bb) {
|
remove_redundant_nops(basicblock *bb) {
|
||||||
/* Remove NOPs when legal to do so. */
|
/* Remove NOPs when legal to do so. */
|
||||||
int dest = 0;
|
int dest = 0;
|
||||||
|
@ -9483,7 +9494,9 @@ remove_redundant_nops(basicblock *bb) {
|
||||||
prev_lineno = lineno;
|
prev_lineno = lineno;
|
||||||
}
|
}
|
||||||
assert(dest <= bb->b_iused);
|
assert(dest <= bb->b_iused);
|
||||||
|
int num_removed = bb->b_iused - dest;
|
||||||
bb->b_iused = dest;
|
bb->b_iused = dest;
|
||||||
|
return num_removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
@ -9694,10 +9707,11 @@ optimize_cfg(cfg_builder *g, PyObject *consts, PyObject *const_cache)
|
||||||
b->b_iused = 0;
|
b->b_iused = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eliminate_empty_basic_blocks(g);
|
|
||||||
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
|
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
|
||||||
remove_redundant_nops(b);
|
remove_redundant_nops(b);
|
||||||
}
|
}
|
||||||
|
eliminate_empty_basic_blocks(g);
|
||||||
|
assert(no_redundant_nops(g));
|
||||||
if (remove_redundant_jumps(g) < 0) {
|
if (remove_redundant_jumps(g) < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue