www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9935] New: static if evaluation of template causes OOM/stack overflow.

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935

           Summary: static if evaluation of template causes OOM/stack
                    overflow.
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ibuclaw ubuntu.com


--- Comment #0 from Iain Buclaw <ibuclaw ubuntu.com> 2013-04-15 03:45:11 PDT ---
Found this when trying to reduce another bug.

Attached problematic source.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 15 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935



--- Comment #1 from Iain Buclaw <ibuclaw ubuntu.com> 2013-04-15 03:45:59 PDT ---
Created an attachment (id=1209)
typetuple.d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935



--- Comment #2 from Iain Buclaw <ibuclaw ubuntu.com> 2013-04-15 03:46:52 PDT ---
Requires -unittest to reproduce.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935



--- Comment #3 from Iain Buclaw <ibuclaw ubuntu.com> 2013-04-15 03:51:38 PDT ---
And here is the change that dustmite made to cause the bug.
---

diff -pur bug.reduced/typetuple.d bug.test/typetuple.d
--- bug.reduced/typetuple.d    2013-04-15 11:48:35.739230767 +0100
+++ bug.test/typetuple.d    2013-04-15 11:49:13.303231268 +0100
   -34,7 +34,7    alias GenericReplace!(T, U, TList).resul

 template GenericReplace(args...)
 {
-alias args[0]from;
+alias args[]from;
 alias args[1]to;
 alias   args[2 .. $]  tuple;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 15 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com


--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2013-10-07
00:10:09 PDT ---
The example code:
------------------
template TypeTuple(TList...)
{
    alias TList TypeTuple;
}

template staticIndexOf(T, TList...)
{
    enum staticIndexOf = genericIndexOf!(T, TList).index;
}

template genericIndexOf(args...)
{
    alias args[0]e;
    alias   args[1 .. $]  tuple;

    alias Alias!(tuple[0]) head;
    alias   tuple[1 .. $]  tail;

    static if (isSame!(e, head))
    {
        enum index = 0;
    }
    else
    {
        enum next  = genericIndexOf!(e, tail).index;
        enum index = (next == -1) ? -1 : 1 + next;
    }
}

template Replace(alias T, alias U, TList...)
{
    alias GenericReplace!(T, U, TList).result Replace;
}

template GenericReplace(args...)
{
    alias args[]from;
    alias args[1]to;
    alias   args[2 .. $]  tuple;

    alias Alias!(tuple[0]) head;
    alias    tuple[1 .. $] tail;

    static if (isSame!(from, head))
        alias TypeTuple!(to, tail) result;
    else
        alias TypeTuple!(head,
                         GenericReplace!(from, to, tail).result) result;
}

unittest
{
    static assert(Pack!(Replace!(1111, "11",
                                 2222, 1111, 1111, 1111)).
                         equals!(2222, "11", 1111, 1111));
}

template Alias(a...)
{
    alias a Alias;
}

template isSame(ab...)
{
    static if (!__traits(compiles, expectType!ab) &&
               __traits(compiles, expectBool!(ab[0] == ab[1])))
        static if (!__traits(compiles) )
        enum isSame = ab[0] == ab[1];
    else
        enum isSame = (isSame);
    else
        enum isSame = __traits(isSame, ab, ab);
}

template expectBool(bool b) {}

template Pack(T...)
{
    template equals(U...)
    {
        static if (T.length == 0)
            enum equals = true;
        else
            enum equals = isSame!(T, U) && Pack!(T[1 .. $]).equals!(U);
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 07 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9935


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WORKSFORME


--- Comment #5 from Walter Bright <bugzilla digitalmars.com> 2013-10-07
00:11:48 PDT ---
2.064 head produces error message:

test.d(53): Error: template instance test.Replace!(1111, "11", 2222, 1111,
1111, 1111) recursive expansion

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