Commit 4cfaa9f6 authored by Stefan Behnel's avatar Stefan Behnel

strip down while-loops with constant condition

parent d2ac07b1
......@@ -3241,6 +3241,15 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
node.iterator.sequence = sequence.as_tuple()
return node
def visit_WhileStatNode(self, node):
self.visitchildren(node)
if node.condition.has_constant_result():
if node.condition.constant_result:
node.else_clause = None
else:
return node.else_clause
return node
def _find_genexpr_yield_stat(self, node):
body_node_types = (Nodes.ForInStatNode, Nodes.IfStatNode)
while isinstance(node, body_node_types):
......
......@@ -143,3 +143,45 @@ def str_in_and_not_in():
"""
if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': return True
else: return False
@cython.test_fail_if_path_exists(
"//WhileStatNode",
)
def while_false():
"""
>>> while_false()
"""
while 1 == 0:
return False
@cython.test_fail_if_path_exists(
"//WhileStatNode",
)
def while_false_else():
"""
>>> while_false_else()
True
"""
while 1 == 0:
return False
else:
return True
@cython.test_fail_if_path_exists(
"//WhileStatNode//PrintStatNode",
)
@cython.test_assert_path_exists(
"//WhileStatNode",
)
def while_true():
"""
>>> while_true()
True
"""
while 1 == 1:
return True
else:
print("FAIL")
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment