D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21479 - ternary operator returns wrong val with ref return
Summary: ternary operator returns wrong val with ref return
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 critical
Assignee: No Owner
URL:
Keywords: backend, pull
Depends on:
Blocks:
 
Reported: 2020-12-13 20:00 UTC by Tobias Pankrath
Modified: 2020-12-20 14:08 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Tobias Pankrath 2020-12-13 20:00:15 UTC
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 kinke 2020-12-13 22:51:58 UTC
ICE with GDC; works with LDC.
Comment 2 Iain Buclaw 2020-12-14 14:29:28 UTC
(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 Iain Buclaw 2020-12-14 18:37:19 UTC
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 Dlang Bot 2020-12-14 19:26:52 UTC
@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 Dlang Bot 2020-12-15 02:54:25 UTC
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 Dlang Bot 2020-12-20 14:08:12 UTC
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