www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3379] New: [tdpl] Parameter names not visible in the if clause of a template

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

           Summary: [tdpl] Parameter names not visible in the if clause of
                    a template
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrei metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei metalanguage.com> 2009-10-08
14:25:57 PDT ---
This code does not compile:

T1[] find(T1, T2)(T1[] longer, T2[] shorter)
   if (is(typeof(longer[0 .. 1] == shorter) : bool))
{
   while (longer.length >= shorter.length) {
      if (longer[0 .. shorter.length] == shorter) break;
      longer = longer[1 .. $];
   }
   return longer;
}

unittest {
   double[] d1 = [ 6.0, 1.5, 2.4, 3 ];
   double[] d2 = [ 1.5, 2.4 ];
   assert(find(d1, d2) == d1[1 .. $]);
}

(I believe I'd submitted this bug already. Please don't mark as duplicate so
all TDPL-related errors stay together.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 08 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3379


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
           Platform|Other                       |All
            Version|unspecified                 |2.032
         OS/Version|Linux                       |All
           Severity|normal                      |enhancement


--- Comment #1 from Don <clugdbug yahoo.com.au> 2009-10-16 00:33:37 PDT ---
I've convinced myself that this isn't too difficult, but it contains a lot of
subtlties. I don't have a complete patch, but I've got basic cases working.
Basically in deduceFunctionTemplateMatch() you need to create variables for
each of the parameters (by analogy to func.c line 904).

    for (i = 0; i < nfparams; i++)
    {
    Argument *fparam = Argument::getNth(fparameters, i);
    if (!fparam->ident) continue; // don't add it, if it has no name
    Type *vtype = // get the type, not sure how to do this properly
    ... deal with pure, etc.
    VarDeclaration *v = new VarDeclaration(loc, vtype, fparam->ident, NULL);

... set the storage class

    v->semantic(paramscope);
    if (!paramscope->insert(v)) error("parameter %s.%s is already defined",
toChars(), v->toChars());
}

Not sure how to deal with deal with the different flavours of variadics,
though.
I originally thought that in TemplateDeclaration::matchWithInstance() you'd be
able avoid checking the constraint again if it's a function template, but you
can't because of cases like:

T1[] find(T1, T2)(T1[] longer, T2[] shorter) if (false) 
{
   assert(0);
   return longer;
}

void main() {
   double[] d1 = [ 6.0, 1.5, 2.4, 3 ];
   double[] d2 = [ 1.5, 2.4 ];
   assert(find!(double, double)(d1, d2) == d1[1 .. $]); // THIS MUST NOT
COMPILE.
}

So it needs to have the same thing, wrapped in a 
     FuncDeclaration *fd = onemember->toAlias()->isFuncDeclaration();
     if (fd) {
   ...
}

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



--- Comment #2 from Don <clugdbug yahoo.com.au> 2009-10-16 18:41:32 PDT ---
Created an attachment (id=474)
Patch against 2.035 svn 215

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



--- Comment #3 from Don <clugdbug yahoo.com.au> 2009-10-16 18:44:16 PDT ---
Since the patch contains two nearly-identical parts they probably should be
factored out into a function. But it works.

Test cases for the patch:
------
/ bug3379
T1[] find(T1, T2)(T1[] longer, T2[] shorter)
   if (is(typeof(longer[0 .. 1] == shorter) ))
{
   while (longer.length >= shorter.length) {
      if (longer[0 .. shorter.length] == shorter) break;
      longer = longer[1 .. $];
   }
   return longer;
}

// bug2983
auto max(T...)(T a) 
      if (T.length == 2 
         && is(typeof(a[1] > a[0] ? a[1] : a[0]))
       || T.length > 2 
         && is(typeof(max(max(a[0], a[1]), a[2 .. $])))) {
   static if (T.length == 2) {
      return a[1] > a[0] ? a[1] : a[0];
   } else {
      return max(max(a[0], a[1]), a[2 .. $]);
   }
}

void main() {
    assert(max(4, 5) == 5);
    assert(max(3, 4, 5) == 5);
   double[] d1 = [ 6.0, 1.5, 2.4, 3 ];
   double[] d2 = [ 1.5, 2.4 ];
   assert(find(d1, d2) == d1[1 .. $]);
}

void bad(T)(T x) if (is(T : idouble)) {   }
static assert(!is(typeof(bad(17))));   // must be rejected
static assert(!is(typeof(bad!(int)(2)))); // reject even if types specified
static assert(is(typeof(bad(3.0i))));

void fun(T)(char a, T[] b...) if (is(typeof(b[0]>b[1]))) {}
static assert(is(typeof(fun('x', 3.0, 4.0, 5.0))));

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



--- Comment #4 from Don <clugdbug yahoo.com.au> 2009-10-16 18:45:01 PDT ---
*** Issue 2983 has been marked as a duplicate of this issue. ***

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
 Attachment #474 is|0                           |1
           obsolete|                            |


--- Comment #5 from Don <clugdbug yahoo.com.au> 2009-10-22 00:09:36 PDT ---
Created an attachment (id=480)
Revised patch against 2.035 svn 215.

My patch had a nasty segfault regression. Trivial though. The line:

FuncDeclaration *fd = onemember->toAlias()->isFuncDeclaration();
if (fd)

causes segfaults if either onemember or toAlias() returns NULL.
fd should be NULL in either of those cases.

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



--- Comment #6 from Don <clugdbug yahoo.com.au> 2009-10-23 08:25:44 PDT ---
Unfortunately my patch still isn't complete. The way I'm setting the storage 
class of the parameters sometimes interferes with the later code. This surpises 
me since I thought that the paramscope->pop() would undo all changes.
 Clearly, there's something about the compiler I don't yet understand.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
 Attachment #480 is|0                           |1
           obsolete|                            |


--- Comment #7 from Don <clugdbug yahoo.com.au> 2009-11-12 00:25:51 PST ---
Created an attachment (id=493)
Patch against svn 241

I've moved the implementation to a separate function. I fixed a couple of bugs;
it now compiles the Phobos test suite.
Currently, it only works with free functions; I've disabled it for templated
member functions (enabling it causes internal compiler errors). There's
probably something I need to do with the variable declarations to make it work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 12 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3379



--- Comment #8 from Don <clugdbug yahoo.com.au> 2009-11-12 00:28:15 PST ---
Created an attachment (id=494)
Test cases

Some test cases I've used. Includes tests which previously caused segfaults.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 12 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3379


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
 Attachment #493 is|0                           |1
           obsolete|                            |


--- Comment #9 from Don <clugdbug yahoo.com.au> 2009-11-13 00:19:54 PST ---
Created an attachment (id=495)
Patch against svn 246.

I think I've finally nailed it.
Now works for member functions. I was inserting the symbols into the wrong
scope (they need to be added into the function scope, not the struct scope).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 13 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3379


Leandro Lucarella <llucax gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |llucax gmail.com


--- Comment #10 from Leandro Lucarella <llucax gmail.com> 2009-11-22 14:32:03
PST ---
SVN commit: http://www.dsource.org/projects/dmd/changeset/263

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 22 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3379


Walter Bright <bugzilla digitalmars.com> changed:

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


--- Comment #11 from Walter Bright <bugzilla digitalmars.com> 2009-12-06
00:51:33 PST ---
Fixed dmd 2.037

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