www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2029] New: Typesefe variadic functions don't work at compile time

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

           Summary: Typesefe variadic functions don't work at compile time
           Product: D
           Version: 2.013
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: bartosz relisoft.com


The following doesn't compile:

string foo (string [] a...)
{
    string result = "";
    foreach (s; a)
        result ~= s;
    return result;
}

mixin (foo ("int ", "x;"));

tError: cannot evaluate foo(cast(invariant(char)[][])((invariant(char)[][2u]
__arrayArg244 = void;
) , (__arrayArg244[0u]) = "int " , (__arrayArg244[1u]) = "x;" , __arrayArg244))
at compile time
attribute argument to mixin must be a string, not
(foo(cast(invariant(char)[][])
((invariant(char)[][2u] __arrayArg244 = void;
) , (__arrayArg244[0u]) = "int " , (__arrayArg244[1u]) = "x;" ,
__arrayArg244)))


-- 
Apr 24 2008
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2029


Tomas Lindquist Olsen <tomas famolsen.dk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tomas famolsen.dk


--- Comment #1 from Tomas Lindquist Olsen <tomas famolsen.dk> 2009-09-15
07:15:10 PDT ---
I'd like this to work too :) I guess I should make a patch ...

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Version|2.013                       |1.020


--- Comment #2 from Don <clugdbug yahoo.com.au> 2009-09-15 07:30:43 PDT ---
(In reply to comment #1)
 I'd like this to work too :) I guess I should make a patch ...

I had a quick go at it before the last release. I didn't include it because it required changes outside of interpret.c. So I did member functions instead <g>. Should work on D1 as well. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2029



--- Comment #3 from Don <clugdbug yahoo.com.au> 2009-09-15 07:42:03 PDT ---
(In reply to comment #2)
 (In reply to comment #1)
 I'd like this to work too :) I guess I should make a patch ...

I had a quick go at it before the last release. I didn't include it because it required changes outside of interpret.c. So I did member functions instead <g>. Should work on D1 as well.

To clarify: you can just comment out the lines near the top of interpret.c that displays the error message about typesafe variadics. But then you find that it doesn't compile, because it's trying to modify a global variable __arrayArg1. (The CTFE error messages make it a lot easier to understand what's happening <g>). Something similar happens with struct constructors, and I think it's wrong. It's more difficult to know how to fix it without breaking something else. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 15 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2029



--- Comment #4 from Tomas Lindquist Olsen <tomas famolsen.dk> 2009-11-18
03:40:17 PST ---
Created an attachment (id=501)
draft patch for fixing typesafe variadic and ctfe

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



--- Comment #5 from Tomas Lindquist Olsen <tomas famolsen.dk> 2009-11-18
03:46:13 PST ---
I've attached a draft patch that replaces the typesafe variadic ast rewrites to
something that works in any scope (and with ctfe).

without the patch the following function:

    void foo(int[] arr ...)

when called:

    foo(1,2,3);

is rewritten as:

    int[3] _args;
    (_args[0] = 1, _args[1] = 2, _args[2] = 3, foo(_args[]));

..

In global scope, this breaks, as you cannot assign to globals from ctfe, and in
normal ctfe, breaks somehow as well.

This patch changes the rewrite to:

    foo([1,2,3]);

with each element being implicitly converted to the array element type.

One downside is that codegen always allocates array literals on the heap, so it
degrades performance a bit, but that needs to be fixed for
http://d.puremagic.com/issues/show_bug.cgi?id=2356 as well (though it's
probably a slightly different issue), and generally I'm not getting an
impression that it's a concern (performance is secondary).

In any case I added a new "scopedLiteral" field to ArrayLiteralExp so that
codegen knows it's allowed to put the array on the stack if it wants to.

Comments appreciated.

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



--- Comment #6 from Tomas Lindquist Olsen <tomas famolsen.dk> 2009-11-18
03:48:50 PST ---
I forgot to mention, the patch is against the latest dmd-1.x branch

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



--- Comment #7 from Tomas Lindquist Olsen <tomas famolsen.dk> 2009-11-21
06:21:44 PST ---
So... Noone cares?

... This is a *draft*. I'm looking for feedback on how to better fix this,
while the patch isn't perfect, it does fix the bug.

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



--- Comment #8 from Don <clugdbug yahoo.com.au> 2009-11-25 21:23:48 PST ---
Thanks, this is helpful. The problem is, that this isn't an isolated bug: the
struct constructor bug has the same root cause. I think we need to fix both of
them at once.

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



--- Comment #9 from Don <clugdbug yahoo.com.au> 2009-12-23 01:34:48 PST ---
The solution is much, much simpler. The problem is that CTFE doesn't support
casting array literals to their base type, even though it's a no-op!


constfold.c, inside Expression *Cast(Type *type, Type *to, Expression *e1),
line 1095:
------------
    /* Allow casting from one string type to another
     */
    if (e1->op == TOKstring)
    {
    if (tb->ty == Tarray && typeb->ty == Tarray &&
        tb->nextOf()->size() == typeb->nextOf()->size())
    {
        return expType(to, e1);
    }
    }

+    if (e1->op == TOKarrayliteral && typeb == tb)
+        return e1;

    if (e1->isConst() != 1)
    return EXP_CANT_INTERPRET;
--------
And then in interpret.c, we can remove the error message (line 116):

    TypeFunction *tf = (TypeFunction *)tb;
    Type *tret = tf->next->toBasetype();
-    if (tf->varargs)
-    {    cantInterpret = 1;
-    error("Variadic functions are not yet implemented in CTFE");
-    return NULL;
-    }
    // Ensure there are no lazy parameters
    if (tf->parameters)

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



--- Comment #10 from Don <clugdbug yahoo.com.au> 2009-12-23 01:50:30 PST ---
Oops, we still don't support C-style variadics. The patch to interpret.c should
be:

 -    if (tf->varargs)
+    if (tf->varargs && arguments && parameters && arguments->dim !=
parameters->dim)
    {    cantInterpret = 1;
-    error("Variadic functions are not yet implemented in CTFE");
+    error("C-style Variadic functions are not yet implemented in CTFE");
    return NULL;
    }

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


Walter Bright <bugzilla digitalmars.com> changed:

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


--- Comment #11 from Walter Bright <bugzilla digitalmars.com> 2009-12-29
03:04:14 PST ---
Changeset 318

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



--- Comment #12 from Walter Bright <bugzilla digitalmars.com> 2009-12-29
03:08:36 PST ---
(In reply to comment #11)
 Changeset 318

Wrong changeset, oops! 318 is for bugzilla 282 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 29 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2029


Leandro Lucarella <llucax gmail.com> changed:

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


--- Comment #13 from Leandro Lucarella <llucax gmail.com> 2009-12-29 08:47:27
PST ---
http://www.dsource.org/projects/dmd/changeset/314
http://www.dsource.org/projects/dmd/changeset/315

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


Walter Bright <bugzilla digitalmars.com> changed:

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


--- Comment #14 from Walter Bright <bugzilla digitalmars.com> 2009-12-31
11:11:17 PST ---
Fixed dmd 1.054 and 2.038

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