I tried the following example today.
Without -O2, the executable generated by gcc returns 32.
With -O2, it is a non-terminating loop. Clearly, gcc assumes
(with -O2) that the sum of two positive integers is also positive
(but it is obviously false when modulo arithmetic is involved).
BTW, clang does NOT have this issue.
This kind of bug is really scary, isn't it ?!
implement
int_test() =
(
loop(0, 1)
) where
{
fun
loop
(n: int, i: int): int =
(
println!
("loop(", n, ", ", i, ")");
if i = 0 then n else loop(n+1, i+i)
)
}
--Hongwei