D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4534 - Dmd crash with templates
Summary: Dmd crash with templates
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-29 16:59 UTC by bearophile_hugs
Modified: 2010-08-29 19:03 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2010-07-29 16:59:44 UTC
This code crashes dmd 2.047 with:
Internal error: ..\ztc\cod3.c 1303


template Format(A...) {
    static if (A.length == 0)
        enum Format = "";
    else static if (is(typeof(A[0]) : const(char)[]))
        enum Format = FormatString!(A[0], A[1..$]);
}

template FormatString(const(char)[] F, A...) {
    static if (F.length == 0)
        enum FormatString = Format!(A);
    else static if (F.length == 1)
        enum FormatString = F[0] ~ Format!(A);
    else static if (F[0..2] == "%s")
        enum FormatString = A[0] ~ FormatString!(F[2..$], A[1..$]);
    else
        enum FormatString = F[0] ~ FormatString!(F[1..$], A);
}

template Tuple(T...) {
    alias T Tuple;
}

enum Type : string { i1 = "x", i2 = "y", i3 = "z" }
enum Size { small = 1, medium = 2, large = 3 }

void main() {
    auto types = Tuple!("a", "b", "c");
    auto sizes = Tuple!("1", "2", "3");

    Type run_t = Type.i1;
    Size run_n = Size.medium;

    enum string pattern = "if (run_t == Type.%s_type && run_n == %s && run_m == %s) foo!(%s, Size.%s, Size.%s)();";

    foreach (t; types)
        foreach (n; sizes)
            foreach (m; sizes)
                enum string txt = Format!(pattern, t, n, m, t, n, m);
}


Please reduce this test case if you can, or improve the bug title.
Comment 1 Andrej Mitrovic 2010-08-29 19:00:50 UTC
On 2.047 it causes that error, but on 2.048 it seems to be gone. I don't know how to close bug reports so you'll have to do it. :)
Comment 2 bearophile_hugs 2010-08-29 19:03:33 UTC
You are right, thank you very much. So I close this bug.