[Issue 18770] New: Ternary operator returns incorrect value when compiling with -O option

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Apr 17 12:42:51 UTC 2018


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

          Issue ID: 18770
           Summary: Ternary operator returns incorrect value when
                    compiling with -O option
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: nebukuro09 at gmail.com

Code:

import std.stdio;

void main() {
    foreach (i; 0..2) {
        long x = (i % 2 == 0) ? 1 : -1;
        x.writeln;
    }
}

-----
Output (with -O option):
1
4294967295
-----
Output (without -O option):
1
-1
-----

It seems that -1 is implicitly converted to uint.
As far as I tried, this behavior does not occur when...

# type is int
  int x = (i % 2 == 0) ? 1 : -1 

# negative value is on the left
  long x = (i % 2 == 0) ? -2 : -1  

# condition can be evaluated at compile time(?)
  long x = false ? 1 : -1

All of the above correctly returns negative value with -O option.

--


More information about the Digitalmars-d-bugs mailing list