[Issue 21479] New: ternary operator returns wrong val with ref return

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Dec 13 20:00:15 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21479

          Issue ID: 21479
           Summary: ternary operator returns wrong val with ref return
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: tobias at pankrath.net

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

--


More information about the Digitalmars-d-bugs mailing list