[Issue 14506] New: Wrong floating point type inferred for function with auto return type

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Apr 26 14:09:35 PDT 2015


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

          Issue ID: 14506
           Summary: Wrong floating point type inferred for function with
                    auto return type
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: DMD
          Assignee: nobody at puremagic.com
          Reporter: monkeyworks12 at hotmail.com

import std.random;

auto test(int n)
{
    if (n >= 0 && n < 33)
    {
        return int(0);
    }
    else if (n >= 33 && n < 66)
    {
        return float(0);
    }
    else
    {
        return real(0);
    }
}

void main()
{
    auto n = uniform(0, 100);
    auto res = test(n);
    //typeof(res) should be inferred as real
    assert(is(typeof(res) == float));
}




Another example from Ali Çehreli:

import std.traits;
import std.typetuple;
import std.format;

auto foo(A, B)(int n)
{
    if (n) {
        return A(0);

    } else {

        return B(0);
    }
}

void main()
{
    alias types = TypeTuple!(float, double, real);

    foreach (A; types) {
        foreach (B; types) {
            alias ReturnType = typeof(foo!(A, B)(0));

            pragma(msg, format("%s %s -> %s%s",
                               A.stringof, B.stringof,
                               ReturnType.stringof,
                               (is (ReturnType == CommonType!(A, B))
                                ? ""
                                : " <-- BUG")));
        }
    }
}


Output:

float float -> float
float double -> float <-- BUG
float real -> float <-- BUG
double float -> double
double double -> double
double real -> double <-- BUG
real float -> real
real double -> real
real real -> real

--


More information about the Digitalmars-d-bugs mailing list