Thread overview
[Issue 4652] New: Compiler hangs on overloading variadic template + default argument
Aug 16, 2010
David Simcha
Aug 16, 2010
Don
[Issue 4652] Compiler hangs on template with zero-length tuple and another argument
Aug 16, 2010
Don
Aug 16, 2010
Don
Aug 18, 2010
Don
Aug 19, 2010
Don
Aug 28, 2010
Walter Bright
August 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652

           Summary: Compiler hangs on overloading variadic template +
                    default argument
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dsimcha@yahoo.com


--- Comment #0 from David Simcha <dsimcha@yahoo.com> 2010-08-15 20:29:17 PDT ---
The following code simply hangs DMD 2.048.  I haven't the slightest clue why.

struct Foo(Args...) if(Args.length > 1)
{
    Args args;
}

template Foo(Arg)
{
    alias Arg Foo;
}

Foo!(Args) getFoo(Args...)(Args args)
{
    static if(Args.length > 1)
    {
        return Foo!(Args)(args);
    }
    else
    {
        return args[0];
    }
}

Foo!(Args) getFoo(Args...)(Args args, uint num)
{
    static if(Args.length > 1)
    {
        return Foo!(Args)(args);
    }
    else
    {
        return args[0];
    }
}

void main()
{
    getFoo(1);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ice-on-valid-code
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2010-08-16 00:00:43 PDT ---
Bug exists in 2.020. Not a regression.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Compiler hangs on           |Compiler hangs on template
                   |overloading variadic        |with zero-length tuple and
                   |template + default argument |another argument


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2010-08-16 02:49:01 PDT ---
Reduced test case.
-------
void bug4652(T...)(T x, int n){}

void instantiate4652()
{
    bug4652(1);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 16, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|D2                          |D1 & D2


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2010-08-16 02:53:24 PDT ---
And it fails on D1, as well (as far back as D1.028).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 18, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2010-08-18 06:37:38 PDT ---
This is caused by a confusion in the code for deduceFunctionTemplateMatch.
The comment and loop condition implies that we're looping over the function
_parameters_. But the rest of the loop assumes it's a loop over the function
_arguments_.
In the case where the tuple is of length zero, an infinite loop results.
Apart from the ICE, this also causes valid code to be rejected.
------
void bug4652(U, T...)(long y, T x, U num){}
void instantiate4652()
{
    bug4652(2, 'c', 27, 'e', 'f',1); // rejects-valid
    bug4652(2, 1);  // infinite loop on valid code
}
----

PATCH: template.c line 1090. Replace the first section of code, with the second.
---------------
    // Loop through the function parameters
    for (i = 0; i < nfparams; i++)
    {
        /* Skip over function parameters which wound up
         * as part of a template tuple parameter.
         */
        if (i == fptupindex)
        {   if (fptupindex == nfparams - 1)
                break;
            i += tuple_dim - 1;
            continue;
        }

        Parameter *fparam = Parameter::getNth(fparameters, i);
---------------
    // Loop through the function arguments
    for (i = 0; i < nfargs; i++)
    {
        /* Skip over function parameters which wound up
         * as part of a template tuple parameter.
         */
        if (i >= fptupindex && i< fptupindex+tuple_dim)
            continue;
        /* Function parameters correspond to function arguments as follows.
         * Note that tuple_dim may be zero.
         * arg [0..fptupindex] == param[0..fptupindex]
         * arg [fptupindex..fptupindex+tuple_dim] == param[fptupindex]
         * arg[fputupindex+dim..nfargs] == param[fptupindex+1..nfparams]
         */
        size_t parami = i;
        if (fptupindex >= 0 && i >= fptupindex) {
            parami -= tuple_dim-1;
        }
        Parameter *fparam = Parameter::getNth(fparameters, parami);
--------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652



--- Comment #5 from Don <clugdbug@yahoo.com.au> 2010-08-19 04:30:12 PDT ---
That patch was incorrect, it failed to deal with default and variadic
parameters. This new test case incorporates the test case from bug 4676 as
well,
which is also fixed by this patch.
---
void bug4652(U, T...)(long y, T x, U num){}
void bug4652default(T) (T value, int x=2) {}
void bug4652default(T) (T value, int y){ }
void bug4676(T...)(T args, string str) {}
void bug4676(T...)(T args) {}
void instantiate4652()
{
    bug4652(2, 'c', 27, 'e', 'f',1); // rejects-valid
    bug4652(2, 1);  // infinite loop on valid code
    bug4652default(true);
    bug4676(1, 2, 3);
}
---


Revised patch. Template.c, line 1090, deduceFunctionTemplateMatch().
==========================

 #endif

     // Loop through the function parameters
-    for (i = 0; i < nfparams; i++)
+    for (size_t parami = 0; parami < nfparams; parami++)
     {
         /* Skip over function parameters which wound up
          * as part of a template tuple parameter.
          */
-        if (i == fptupindex)
-        {   if (fptupindex == nfparams - 1)
-                break;
+        if (parami == fptupindex)
+            continue;
+        /* Set i = index into function arguments
+         * Function parameters correspond to function arguments as follows.
+         * Note that tuple_dim may be zero, and there may be default or
+         * variadic arguments at the end.
+         *  arg [0..fptupindex] == param[0..fptupindex]
+         *  arg [fptupindex..fptupindex+tuple_dim] == param[fptupindex]
+         *  arg[fputupindex+dim.. ] == param[fptupindex+1.. ]
+         */
+        i = parami;
+        if (fptupindex >= 0 && parami > fptupindex)
             i += tuple_dim - 1;
-            continue;
-        }

-        Parameter *fparam = Parameter::getNth(fparameters, i);
+        Parameter *fparam = Parameter::getNth(fparameters, parami);

         if (i >= nfargs)                // if not enough arguments
         {

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4652


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> 2010-08-27 20:54:58 PDT ---
http://www.dsource.org/projects/dmd/changeset/635

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