Commit b1ffafb1 authored by Stefan Behnel's avatar Stefan Behnel

fix constant folding of PrimaryCmpNode operands

parent 10d51a67
......@@ -3343,12 +3343,12 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
def visit_PrimaryCmpNode(self, node):
# calculate constant partial results in the comparison cascade
self.visitchildren(node, ['operand1'])
left_node = node.operand1
self._calculate_const(left_node)
cmp_node = node
while cmp_node is not None:
self.visitchildren(cmp_node, ['operand2'])
right_node = cmp_node.operand2
self._calculate_const(right_node)
cmp_node.constant_result = not_a_constant
if left_node.has_constant_result() and right_node.has_constant_result():
try:
......
......@@ -428,3 +428,31 @@ def cascaded_cmp_with_partial_constants_and_false_end(a, b, c):
"""
x = 1 < 2 < a < 4 < 5 < b < 7 < 7 < c
return x
@cython.test_assert_path_exists(
'//PrimaryCmpNode',
'//PrimaryCmpNode//IntNode',
'//PrimaryCmpNode//IntNode[@value = "0"]',
'//PrimaryCmpNode//IntNode[@value = "4294967296"]',
)
@cython.test_fail_if_path_exists(
'//PrimaryCmpNode//IntBinopNode',
'//PrimaryCmpNode//IntNode[@value = "1"]',
'//PrimaryCmpNode//IntNode[@value = "32"]',
)
def const_in_binop(v):
"""
>>> const_in_binop(-1)
1
>>> const_in_binop(0)
0
>>> const_in_binop(1 << 32)
1
>>> const_in_binop(1 << 32 - 1)
0
"""
if v < 0 or v >= (1L << 32):
return 1
else:
return 0
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