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); }
ICE with GDC; works with LDC.
(In reply to kinke from comment #1) > ICE with GDC; works with LDC. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98277
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.
@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
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
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