Skip to content

Commit 746c95d

Browse files
DinoVfacebook-github-bot
authored andcommitted
Avoid creating new basic block unless we break to it
Summary: This is similar to D81642076, we need to avoid creating a basic block to make sure other analysis passes proceed the same. This one is a little more complicated because it depends on their being a break to create the jump to the block. Reviewed By: jbower-fb Differential Revision: D81731244 fbshipit-source-id: 405bfc33533a350c6aac6798a302e1db583d4883
1 parent dd33c21 commit 746c95d

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

cinderx/PythonLib/cinderx/compiler/pycodegen.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6425,6 +6425,49 @@ def _compile_async_comprehension(
64256425
self.set_pos(comp)
64266426
self.emit("END_ASYNC_FOR", send)
64276427

6428+
def visitBreak(self, node: ast.Break) -> None:
6429+
self.emit("NOP") # for line number
6430+
loop = self.unwind_setup_entries(preserve_tos=False, stop_on_loop=True)
6431+
if loop is None:
6432+
raise self.syntax_error("'break' outside loop", node)
6433+
self.unwind_setup_entry(loop, preserve_tos=False)
6434+
6435+
jumpblock = loop.exit
6436+
if jumpblock is None:
6437+
loop.exit = jumpblock = self.newBlock("break")
6438+
6439+
self.emitJump(jumpblock)
6440+
self.nextBlock()
6441+
6442+
def visitFor(self, node: ast.For) -> None:
6443+
start = self.newBlock("for_start")
6444+
body = self.newBlock("for_body")
6445+
cleanup = self.newBlock("for_cleanup")
6446+
6447+
setup = Entry(FOR_LOOP, start, None, None)
6448+
self.push_fblock(setup)
6449+
self.visit(node.iter)
6450+
self.set_pos(node.iter)
6451+
self.emit("GET_ITER")
6452+
6453+
self.nextBlock(start)
6454+
self.emit("FOR_ITER", cleanup)
6455+
if IS_3_12_8:
6456+
self.graph.emit_with_loc("NOP", 0, node.target)
6457+
self.nextBlock(body)
6458+
self.visit(node.target)
6459+
self.visitStatements(node.body)
6460+
self.set_no_pos()
6461+
self.emitJump(start)
6462+
self.nextBlock(cleanup)
6463+
self.emit_end_for()
6464+
self.pop_loop()
6465+
6466+
if node.orelse:
6467+
self.visitStatements(node.orelse)
6468+
if setup.exit is not None:
6469+
self.nextBlock(setup.exit)
6470+
64286471
def visitFormattedValue(self, node: ast.FormattedValue) -> None:
64296472
self.visit(node.value)
64306473

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# pyre-ignore-all-errors
2+
def f():
3+
try:
4+
for el in x:
5+
break
6+
except Exception:
7+
pass
8+
9+
foo()

0 commit comments

Comments
 (0)