www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16235] New: type of conditional expression (ternary operator)

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

          Issue ID: 16235
           Summary: type of conditional expression (ternary operator) is
                    widened when operands differ in constness
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

These pass:
----
static assert(is(typeof(true ? int.init : int.init) == int));
static assert(is(typeof(true ? int.init : const(int).init) == int));
static assert(is(typeof(true ? byte.init : byte.init) == byte));
----

This one fails but should pass:
----
static assert(is(typeof(true ? byte.init : const(byte).init) == byte));
----

This makes CommonType!(byte, const byte) evaluate to int. And that makes this
little program surprisingly fail compilation:

----
void main()
{
    import std.range: chain;
    ubyte[] a = [1, 2];
    const(ubyte)[] b = [3, 4];
    ubyte f = chain(a, b).front; /* Error: cannot implicitly convert expression
(chain(a, b).front()) of type int to ubyte */
}
----

--
Jul 04 2016