Closed
Description
Code
from ltypes import i32
def main0():
i1: i32 = 10
i2: i32 = 4
i1 = 3
i2 = 5
print(-i1 ^ -i2)
assert -i1 ^ -i2 == 6
main0()
Output
10
AssertionError
Reasons for failure,
value
attribute (this is the attribute which stores compile time values) is being set for non-constant variables.- This leads to compile time value of
-i1
as10
and-i2
as4
. Hence,bitwise_xor
is being evaluated at compile time. - If we set
value
attribute asnullptr
then we get random outputs because there is no implementation ofbitwise_xor
provided which can be used at runtime.
See the original code where value
attribute of Variable
ASR node is being set,
lpython/src/lpython/semantics/python_ast_to_asr.cpp
Lines 2082 to 2144 in 6ccd336
You can clearly see, init_expr
is nullptr
,
and value
is filled with initial expression,
I was compelled to follow this pattern in some of my PRs but now I think we should fix this because this is producing incorrect results.