[Issue 5996] New: [CTFE] Undefined function call in auto return function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 13 13:14:31 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5996

           Summary: [CTFE] Undefined function call in auto return function
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-05-13 13:10:26 PDT ---
This is wrong D2 code, because min() is not defined:


auto foo(int n) {
    uint two = 2, three = 3, five = 5;
    auto h = new typeof(two)[n];
    h[0] = 1;
    auto x2 = two, x3 = three, x5 = five;
    int i, j, k;
    for (int w = 1; w < n; w++) {
        auto el = h[w] = min(x2, x3, x5);
        if (x2 == el) x2 = two   * h[++i];
        if (x3 == el) x3 = three * h[++j];
        if (x5 == el) x5 = five  * h[++k];
    }
    return h[$ - 1];
}
enum uint h = foo(1691);
static assert(h == 2123366400);
void main() {}


DMD 2.053beta prints:
Assertion failure: '0' on line 1601 in file 'expression.c'

---------------------

If I have remove the three if() lines:


auto foo(int n) {
    uint two = 2, three = 3, five = 5;
    auto h = new typeof(two)[n];
    h[0] = 1;
    auto x2 = two, x3 = three, x5 = five;
    int i, j, k;
    for (int w = 1; w < n; w++) {
        auto el = h[w] = min(x2, x3, x5);
    }
    return h[$ - 1];
}
enum uint h = foo(1691);
static assert(h == 2123366400);
void main() {}


Now the error messages are:

Error: array index 4294967295 is out of bounds [][0 .. 0]
test2.d(10): Error: h[__dollar - 1u] cannot be interpreted at compile time
test2.d(12): Error: cannot evaluate foo(1691) at compile time
test2.d(12): Error: cannot evaluate foo(1691) at compile time
test2.d(13): Error: static assert  (foo(1691) == 2123366400u) is not
evaluatable at compile time

Note an error message without line number too.


DMD 2.052 instead prints:

test2.d(4): Error: array index 0 is out of bounds [][0..0]
test2.d(12): Error: cannot evaluate foo(1691) at compile time
test2.d(12): Error: cannot evaluate foo(1691) at compile time
test2.d(12): Error: cannot evaluate foo(1691) at compile time
test2.d(13): Error: static assert  (foo(1691) == 2123366400u) is not
evaluatable at compile time

---------------------

If I remove "auto" as return type and put a "uint":


uint foo(int n) {
    uint two = 2, three = 3, five = 5;
    auto h = new typeof(two)[n];
    h[0] = 1;
    auto x2 = two, x3 = three, x5 = five;
    int i, j, k;
    for (int w = 1; w < n; w++) {
        auto el = h[w] = min(x2, x3, x5);
    }
    return h[$ - 1];
}
enum uint h = foo(1691);
static assert(h == 2123366400);
void main() {}


Finally the error messages are correct:

test3.d(8): Error: undefined identifier min, did you mean function main?
test3.d(12): Error: cannot evaluate foo(1691) at compile time

--------------------------

Now adding the missing min() fixes the problem, and it works with auto return
type too:


T min(T)(T a, T b, T c) {
    return (a > b) ?   ((b > c) ? c : b)  :  ((a > c) ? c : a);
}
auto foo(int n) {
    uint two = 2, three = 3, five = 5;
    auto h = new typeof(two)[n];
    h[0] = 1;
    auto x2 = two, x3 = three, x5 = five;
    int i, j, k;
    for (int w = 1; w < n; w++) {
        auto el = h[w] = min(x2, x3, x5);
        if (x2 == el) x2 = two   * h[++i];
        if (x3 == el) x3 = three * h[++j];
        if (x5 == el) x5 = five  * h[++k];
    }
    return h[$ - 1];
}
enum uint h = foo(1691);
static assert(h == 2125764000);
void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list