Commit 2ad640d1 authored by Stefan Behnel's avatar Stefan Behnel

Remove overambitious constant folding of comprehensions when they occur in a...

Remove overambitious constant folding of comprehensions when they occur in a boolean context. Never assume that they are empty, since the iteration might already have required side-effects.
Closes #1920.
parent 14971192
...@@ -7983,6 +7983,7 @@ class ComprehensionNode(ScopedExprNode): ...@@ -7983,6 +7983,7 @@ class ComprehensionNode(ScopedExprNode):
child_attrs = ["loop"] child_attrs = ["loop"]
is_temp = True is_temp = True
constant_result = not_a_constant
def infer_type(self, env): def infer_type(self, env):
return self.type return self.type
......
...@@ -150,6 +150,57 @@ def listcomp_const_condition_false(): ...@@ -150,6 +150,57 @@ def listcomp_const_condition_false():
return [x*2 for x in range(3) if False] return [x*2 for x in range(3) if False]
@cython.test_fail_if_path_exists("//IfStatNode",
"//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def listcomp_const_condition_false_bool_test():
"""
>>> listcomp_const_condition_false_bool_test()
True
"""
return not [l for l in [1] if False]
@cython.test_fail_if_path_exists("//IfStatNode",
"//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode")
def listcomp_const_condition_false_assert():
"""
>>> listcomp_const_condition_false_assert()
"""
assert not [l for l in [1] if False]
@cython.test_fail_if_path_exists("//ComprehensionNode//IfStatNode",
"//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode",
"//IfStatNode")
def listcomp_const_condition_false_if():
"""
>>> listcomp_const_condition_false_if()
True
"""
if not [l for l in [1] if False]:
return True
return False
@cython.test_fail_if_path_exists("//ComprehensionNode//IfStatNode",
"//ComprehensionAppendNode")
@cython.test_assert_path_exists("//ComprehensionNode",
"//IfStatNode")
def listcomp_const_condition_false_typed_error():
"""
>>> listcomp_const_condition_false_typed_error() # doctest: +ELLIPSIS
Traceback (most recent call last):
TypeError: ...
"""
cdef str l
if not [l for l in [1] if False]:
return True
return False
@cython.test_fail_if_path_exists("//IfStatNode") @cython.test_fail_if_path_exists("//IfStatNode")
@cython.test_assert_path_exists("//ComprehensionNode", @cython.test_assert_path_exists("//ComprehensionNode",
"//ComprehensionAppendNode") "//ComprehensionAppendNode")
......
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