www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21479] New: ternary operator returns wrong val with ref return

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 puremagic.com
          Reporter: tobias 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);
}

--
Dec 13 2020