Bug 18534 – Wrong code for ?: operator when compiling with -O

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2018-02-27T19:53:28Z
Last change time
2018-03-20T11:26:41Z
Keywords
wrong-code
Assigned to
No Owner
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2018-02-27T19:53:28Z
Code: ------- auto foo(int invert) { import std.format : format; return format("%c", invert ? 'A' : 'A'); } void main() { auto s = foo(0); assert(s == "A"); // assertion fails } ------- Compile command: ------- dmd -O -run test.d ------- Inspecting the contents of s reveals that instead of containing the character 'A', it contains 0xFF instead. Compiling without -O fixes the problem. Replacing the conditional with just 'A' also makes the problem go away. Seems like -O generates wrong code for the ?: operator.
Comment #1 by ag0aep6g — 2018-02-27T20:11:59Z
Reduced Phobos away: ---- void f(char x) {} void foo(int invert) { char c = invert ? 'A' : 'A'; f(c); assert(c == 'A'); // assertion fails } void main() { foo(0); } ----
Comment #2 by hsteoh — 2018-02-27T20:38:07Z
Reduced code without Phobos/template dependency: ----- auto blah(char ch) { return ch; } auto foo(int i) { return blah(i ? 'A' : 'A'); } void main() { auto c = foo(0); assert(c == 'A'); } -----
Comment #3 by bugzilla — 2018-03-19T04:30:23Z
Comment #4 by github-bugzilla — 2018-03-20T11:26:40Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/f9ed34003d9b0f533a37850540b02929586e7a76 fix Issue 18534 - Wrong code for ?: operator when compiling with -O https://github.com/dlang/dmd/commit/cf2d493f07c1a8ae935d1ed9cded1666d3addbd2 Merge pull request #8052 from WalterBright/fix18534 fix Issue 18534 - Wrong code for ?: operator when compiling with -O