www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17087] New: [REG2.072] Wrong generated with cfloat and creal

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

          Issue ID: 17087
           Summary: [REG2.072] Wrong generated with cfloat and creal when
                    casting from int
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: petar.p.kirov gmail.com

//Minimal test case
void main()
{
    cfloat toComplex(int x) { return cast(cfloat)x; }
    auto a = toComplex(1);
    auto b = cast(creal)1;
    assert (a == b);
}


Output:
core.exception.AssertError /tmp/test.d(6): Assertion failure


//Longer test case
D toComplex(D, S)(S val)
{
    return cast(D) val;
}

alias Seq(T...) = T;

enum isComplex(T) = is(T == cfloat) || is(T == cdouble) || is(T == creal);

void main()
{
    bool hasErrors;

    foreach (OriginalType; Seq!(byte, short, int, long, float, double, real,
cfloat, cdouble, creal))
    foreach (DestType; Seq!(byte, short, int, long, float, double, real,
cfloat, cdouble, creal))
    {
        import std.stdio;

        static if (isComplex!OriginalType)
           enum orig = OriginalType(1 + 0i);
        else
            enum orig = OriginalType(1);

        auto a = toComplex!DestType(orig);
        auto b = cast(DestType)orig;

        if (orig != a)
        {
            hasErrors = true;
            "Error: orig != a when converting from %s to %s {orig(%s) !=
a(%s)}"
                .writefln(OriginalType.stringof, DestType.stringof, orig, a,
b);
        }

        if (orig != b)
        {
            hasErrors = true;
            "Error: orig != b when converting from %s to %s {orig(%s) !=
b(%s)}"
                .writefln(OriginalType.stringof, DestType.stringof, orig, a,
b);
        }

        if (a != b)
        {
            hasErrors = true;
            "Error: a != b when converting from %s to %s {orig(%s) a(%s) !=
b(%s)}"
                .writefln(OriginalType.stringof, DestType.stringof, orig, a,
b);
        }
    }

    !hasErrors || assert(0);
}

Output:
Error: orig != a when converting from byte to cfloat {orig(1) != a(0+0i)}
Error: a != b when converting from byte to cfloat {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from byte to creal {orig(1) != a(0+0i)}
Error: a != b when converting from byte to creal {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from short to cfloat {orig(1) != a(0+0i)}
Error: a != b when converting from short to cfloat {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from short to creal {orig(1) != a(0+0i)}
Error: a != b when converting from short to creal {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from int to cfloat {orig(1) != a(0+0i)}
Error: a != b when converting from int to cfloat {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from int to creal {orig(1) != a(0+0i)}
Error: a != b when converting from int to creal {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from long to cfloat {orig(1) != a(0+0i)}
Error: a != b when converting from long to cfloat {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from long to creal {orig(1) != a(0+0i)}
Error: a != b when converting from long to creal {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from double to cfloat {orig(1) != a(0+0i)}
Error: a != b when converting from double to cfloat {orig(1) a(0+0i) !=
b(1+0i)}
Error: orig != a when converting from double to creal {orig(1) != a(0+0i)}
Error: a != b when converting from double to creal {orig(1) a(0+0i) != b(1+0i)}
Error: orig != a when converting from cfloat to double {orig(1+0i) != a(1)}
Error: orig != b when converting from cfloat to double {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to byte {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to byte {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to short {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to short {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to int {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to int {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to long {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to long {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to float {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to float {orig(1+0i) != b(1)}
Error: orig != a when converting from cdouble to double {orig(1+0i) != a(1)}
Error: orig != b when converting from cdouble to double {orig(1+0i) != b(1)}
core.exception.AssertError /tmp/test.d(49): Assertion failure

--
Jan 13