www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16268] New: Wrong optimization in code with integer overflow

https://issues.dlang.org/show_bug.cgi?id=16268

          Issue ID: 16268
           Summary: Wrong optimization in code with integer overflow
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: guillaume.boucher.d outlook.com

The following program behaves incorrectly when compiled with "-O":

import std.stdio;

void f(byte x)
{
    for (byte i = 0; i <= x && i >= 0; ++i)
    {
        assert(i >= 0);
        writeln(i);
    }
}

void main()
{
    f(byte.max);
}

Compiled without flags, this program prints the numbers from 0 to 127 and
terminates.

Compiled with "-O", this program prints the numbers from 0 to 127, and then
repeats -128, -127, ..., 127 infinitely.  It does not terminate.

The program should print the numbers from 0 to 127 once.  Signed overflow is
defined behaviour, therefore "i >= 0" can not be assumed to be true.  Note how
even the assertion is ignored.

Same problem happens with short and int instead of byte.

If one replaces 0 with 1, the program prints 0..127, -128..-1 and terminates. 
It should only print 1..127.

(DMD v2.071.1)

--
Jul 12 2016