Bug 21479 – ternary operator returns wrong val with ref return

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-12-13T20:00:15Z
Last change time
2020-12-20T14:08:12Z
Keywords
backend, pull
Assigned to
No Owner
Creator
Tobias Pankrath
See also
https://issues.dlang.org/show_bug.cgi?id=10926

Comments

Comment #0 by tobias — 2020-12-13T20:00:15Z
import std.stdio; enum Side { left, right } struct Both(T) { T left; T right; ref T get(Side side) { // this works /*if (side == Side.left) return left; else return right;*/ // this is broken, but works if left and right are interchanged return side == Side.left ? left : right; } } unittest { Both!(int[]) t; t.get(Side.left) ~= 1; assert (t.left.length == 1); t.get(Side.right) ~= 1; t.get(Side.right) ~= 2; assert (t.right.length == 2); }
Comment #1 by kinke — 2020-12-13T22:51:58Z
ICE with GDC; works with LDC.
Comment #2 by ibuclaw — 2020-12-14T14:29:28Z
(In reply to kinke from comment #1) > ICE with GDC; works with LDC. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98277
Comment #3 by ibuclaw — 2020-12-14T18:37:19Z
One underlying issue with the constructed AST is that Side.left is not constant propagated, this differs with other kinds of expressions/statements that use enum members. They always send expanded literals to the back-end.
Comment #4 by dlang-bot — 2020-12-14T19:26:52Z
@ibuclaw created dlang/dmd pull request #12027 "fix Issue 21479 - ternary operator returns wrong val with ref return" fixing this issue: - fix Issue 21479 - ternary operator returns wrong val with ref return https://github.com/dlang/dmd/pull/12027
Comment #5 by dlang-bot — 2020-12-15T02:54:25Z
dlang/dmd pull request #12027 "fix Issue 21479 - ternary operator returns wrong val with ref return" was merged into stable: - 56fd37bdb321f24019966e9d65ba76df60564561 by Iain Buclaw: fix Issue 21479 - ternary operator returns wrong val with ref return https://github.com/dlang/dmd/pull/12027
Comment #6 by dlang-bot — 2020-12-20T14:08:12Z
dlang/dmd pull request #12040 "merge stable" was merged into master: - d0b16ff78f117e740d5768d49b617b62e4897b82 by Iain Buclaw: fix Issue 21479 - ternary operator returns wrong val with ref return https://github.com/dlang/dmd/pull/12040