www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15412] New: Operator ^^= fails to compile for many numeric

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

          Issue ID: 15412
           Summary: Operator ^^= fails to compile for many numeric type
                    combinations
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: thomas.bockman gmail.com

This code:
    N n;
    M m;
    n ^^ = m;

Seems to be lowered to this:
    N n;
    M m;
    n = pow(n, m);

For many numeric type combinations, this will fail to compile, yielding "Error:
cannot implicitly convert expression (pow(cast(N)n, cast(M)m)) of type R to N",
where R is some other numeric type.

In particular, operator ^^= is completely unusable with byte, ubyte, short, and
ushort, since for those types pow returns int, which cannot be implicitly
converted to a lesser type.

The fix is to include an explicit cast in the lowering:
    N n;
    M m;
    n = cast(N) pow(n, m);
This would make the behaviour of ^^= consistent with the other assignment
operators (like *=) which do not have this problem.

// Full test code ///////////////////////////////////
import std.traits : NumericTypeList;
import std.stdio;

void main(string[] args) {
    foreach(N; NumericTypeList) {
        foreach(M; NumericTypeList) {
            N n = 1;
            M m = 1;
            if(!__traits(compiles, n += m))
                writeln(N.stringof ~ " ^^= " ~ M.stringof ~ " fails to
compile.");
        }
    }
}

--
Dec 05 2015