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);
}
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