Bug 18770 – Ternary operator returns incorrect value when compiling with -O option
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2018-04-17T12:42:51Z
Last change time
2023-05-05T10:06:15Z
Assigned to
No Owner
Creator
nebukuro09
Comments
Comment #0 by nebukuro09 — 2018-04-17T12:42:51Z
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.
Comment #1 by razvan.nitu1305 — 2023-05-05T10:06:15Z
I cannot reproduce this with the latest master. It seems that this has been fixed.