www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1678] New: ref with varargs generates invalid code

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

           Summary: ref with varargs generates invalid code
           Product: D
           Version: 1.023
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: ary esperanto.org.ar


Given this code:

***
module main;

import std.stdio;
import std.stdarg;

void foo(int x, ...) {
    writefln("%d arguments", _arguments.length);
    for (int i = 0; i < _arguments.length; i++) {   
            _arguments[i].print();
        int j = va_arg!(int)(_argptr);
            writefln("\t%d", j);
    }
}

void fooRef(ref int x, ...) {
    writefln("%d arguments", _arguments.length);
    for (int i = 0; i < _arguments.length; i++) {   
            _arguments[i].print();
        int j = va_arg!(int)(_argptr);
            writefln("\t%d", j);
    }
}

void main() {
        foo(1, 2, 3, 4, 5);

        writefln("---");

        int x = 1;
        fooRef(x, 2, 3, 4, 5);
}
***

the output is:

***
4 arguments
int
        2
int
        3
int
        4
int
        5
---
4 arguments
int
        1245056
int
        4203171
int
        1
int
        1245004
***

Note that both functions are identical, except the last one has a ref
parameter. Without it, the function works as expected.


-- 
Nov 18 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678


smjg iname.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg iname.com
           Keywords|                            |wrong-code




------- Comment #1 from smjg iname.com  2007-11-19 09:16 -------
Please remember to assign keywords to bug reports.  To everybody reading this:
Please look through issues you've reported and check for missing keywords.


-- 
Nov 19 2007
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678





------- Comment #2 from ary esperanto.org.ar  2007-11-19 09:29 -------
Is there a standard keyword list? I always see "accepts-invalid", and now that
I know it, I'll use it. For this one, it's "wrong-code", but I was going to
write "invalid-code" but I was afraid that wasn't the "standard" keyword. Also,
I was going to write "ref varargs"...


-- 
Nov 19 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1678
 
 
 
 
 
 ------- Comment #2 from ary esperanto.org.ar  2007-11-19 09:29 -------
 Is there a standard keyword list? I always see "accepts-invalid", and now that
 I know it, I'll use it. For this one, it's "wrong-code", but I was going to
 write "invalid-code" but I was afraid that wasn't the "standard" keyword. Also,
 I was going to write "ref varargs"...

If you click on the word "Keywords" in the report it will take you to a page listing the keywords. It's a bad interface though. If there were some kind of clicky list probably more people would add keywords. Me included. --bb
Nov 19 2007
parent Ary Borenszweig <ary esperanto.org.ar> writes:
Bill Baxter wrote:
 d-bugmail puremagic.com wrote:
 http://d.puremagic.com/issues/show_bug.cgi?id=1678





 ------- Comment #2 from ary esperanto.org.ar  2007-11-19 09:29 -------
 Is there a standard keyword list? I always see "accepts-invalid", and 
 now that
 I know it, I'll use it. For this one, it's "wrong-code", but I was 
 going to
 write "invalid-code" but I was afraid that wasn't the "standard" 
 keyword. Also,
 I was going to write "ref varargs"...

If you click on the word "Keywords" in the report it will take you to a page listing the keywords. It's a bad interface though. If there were some kind of clicky list probably more people would add keywords. Me included. --bb

Thanks, I didn't see that link.
Nov 19 2007
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678





------- Comment #3 from smjg iname.com  2007-11-19 09:44 -------
Keywords in Bugzilla aren't quite like the keywords you use on web pages to
guide search engines.  They're more like flags.  And if you'd bothered to
follow the link that is the word "Keywords", you would have found a list of
them.


-- 
Nov 19 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:fhsb1m$2bek$1 digitalmars.com...
 http://d.puremagic.com/issues/show_bug.cgi?id=1678

 And if you'd bothered to
 follow the link that is the word "Keywords", you would have found a list 
 of
 them.

You know, you don't always have to be nasty to everyone.
Nov 19 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678


Markus Dangl <sky q1cc.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sky q1cc.net


--- Comment #5 from Markus Dangl <sky q1cc.net> 2010-06-16 17:18:23 PDT ---
*** Issue 4336 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: -------
Jun 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #6 from Don <clugdbug yahoo.com.au> 2010-07-24 22:30:56 PDT ---
This also applies to 'out' parameters, as well as 'ref'.

The problem is in func.c, FuncDeclaration::semantic3(), around line 1380, where
__argptr is created. It's set equal to (&p + offset), where p is the last
non-variadic parameter. This fails if p is a reference -- it gives the address
of the thing p is pointing to.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 24 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678


Don <clugdbug yahoo.com.au> changed:

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


--- Comment #7 from Don <clugdbug yahoo.com.au> 2010-07-26 13:03:13 PDT ---
There's probably a simpler way to patch this. What I've done is find the first
non-ref parameter, and set __argptr off it.

Index: func.c
===================================================================
--- func.c    (revision 588)
+++ func.c    (working copy)
   -1367,18 +1367,32   
 #else
                 Type *t = argptr->type;
                 VarDeclaration *p;
-                unsigned offset;
-
+                unsigned offset = 0;
                 Expression *e1 = new VarExp(0, argptr);
+                // Find the last non-ref parameter
                 if (parameters && parameters->dim)
-                    p = (VarDeclaration *)parameters->data[parameters->dim -
1];
+                {
+                    int lastNonref = parameters->dim -1;
+                    p = (VarDeclaration *)parameters->data[lastNonref];
+                    while (p->storage_class & (STCout | STCref))
+                    {
+                        --lastNonref;
+                        offset += PTRSIZE;
+                        if (lastNonref < 0)
+                        {
+                            p = v_arguments;
+                            break;
+                        }
+                        p = (VarDeclaration *)parameters->data[lastNonref];
+                    }
+                }
                 else
-                    p = v_arguments;            // last parameter is
_arguments[]
+                    p = v_arguments;
                 if (p->storage_class & STClazy)
                     // If the last parameter is lazy, it's the size of a
delegate
-                    offset = PTRSIZE * 2;
+                    offset += PTRSIZE * 2;
                 else
-                    offset = p->type->size();
+                    offset += p->type->size();
                 offset = (offset + 3) & ~3;     // assume stack aligns on 4
                 Expression *e = new SymOffExp(0, p, offset);
                 e->type = Type::tvoidptr;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 26 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1678


Walter Bright <bugzilla digitalmars.com> changed:

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


--- Comment #8 from Walter Bright <bugzilla digitalmars.com> 2010-08-05
23:09:30 PDT ---
http://www.dsource.org/projects/dmd/changeset/603

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