Thread overview
[Issue 874] New: Incorrect codegen (?) with tuples, string constants, and AAs
Jan 23, 2007
d-bugmail
Feb 23, 2007
d-bugmail
Aug 12, 2009
Don
[Issue 874] Bad codegen: wrong value variable in tuple foreach, D1 only
Oct 30, 2009
Don
Oct 30, 2009
Don
Nov 04, 2009
Leandro Lucarella
Nov 06, 2009
Walter Bright
January 23, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=874

           Summary: Incorrect codegen (?) with tuples, string constants, and
                    AAs
           Product: D
           Version: 1.00
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: jarrett.billingsley@gmail.com


I don't know how else to describe this.

template AA(V, K)
{
        V[K] AA(T...)(T args)
        {
                V[K] ret;
                K key;

                foreach(i, arg; args)
                {
                        static if(!(i & 1))
                                key = arg;
                        else
                                ret[key] = arg;
                }

                return ret;
        }
}

void main()
{
        char[][char[]] array = AA!(char[], char[])("a", "b", "c", "d");

        writefln("length = ", array.length);

        foreach(k, v; array)
                writefln("array[", k, "] = ", v);
}

This code will give bizarre output when printing out the contents of the returned AA.  This usually is weird characters, and usually results in a "4invalid UTF-8 sequence" exception.  Stranger still, the output changes depending on how many string literals there are in main().  This happens in both -debug and -release modes, although they give different output.


-- 

February 23, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=874





------- Comment #1 from thomas-dloop@kuehne.cn  2007-02-23 16:36 -------
Added to DStress as http://dstress.kuehne.cn/run/o/odd_bug_13_A.d


-- 

August 12, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=874


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

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




--- Comment #2 from Don <clugdbug@yahoo.com.au>  2009-08-12 06:17:48 PDT ---
The equivalent code (below) works correctly on D2. This is a D1-only bug.
---------
import std.stdio;
template AA(V, K) {
    V[K] AA(T...)(T args) {
        V[K] ret;
        K key;

        foreach(i, arg; args) {
            static if(!(i & 1))
                key = arg;
            else
                ret[key] = arg;
        }
        return ret;
    }
}
void main()
{
    string[string] array = AA!(string, string)("a", "b"[], "c"[], "d"[]);
    writefln("length = %d\n", array.length);
    foreach(k, v; array)
        writefln("array[%d]=%s", k, v);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 30, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=874


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Incorrect codegen with      |Bad codegen: wrong value
                   |tuples, string constants,   |variable in tuple foreach,
                   |and AAs, D1 only            |D1 only


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2009-10-30 01:53:50 PDT ---
Original title was: "Incorrect codegen with tuples, string constants, and AAs".

This bug has nothing to do with AAs, actually. The problem is that the 'value'
in a tuple foreach isn't dealt with correctly.
Workarounds: Change the code into:  ret = arg.dup; or ret = args[0];

char[] bug874(T...)(T args)
{
    char[] ret;

    foreach(arg; args) {
        ret = arg;
    }

    assert(ret=="b"); // passes
    return ret;
}

void main()
{
    char[] s = bug874("b");
    assert(s == "b"); // fails
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 30, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=874


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

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


--- Comment #4 from Don <clugdbug@yahoo.com.au> 2009-10-30 02:16:21 PDT ---
Looks like this is just a D2 fix which didn't get transferred across to D1.
PATCH (against DMD1 svn 227):
expression.c, ForeachStatement::semantic, line 1380.

        // Declare value
        if (arg->storageClass & (STCout | STCref | STClazy))
        error("no storage class for value %s", arg->ident->toChars());
        Dsymbol *var;
        if (te)
-        {
-        if (e->type->toBasetype()->ty == Tfunction &&
-            e->op == TOKvar)
+        {    Type *tb = e->type->toBasetype();
+        if ((tb->ty == Tfunction || tb->ty == Tsarray) && e->op == TOKvar)
        {   VarExp *ve = (VarExp *)e;
            var = new AliasDeclaration(loc, arg->ident, ve->var);
        }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=874


Leandro Lucarella <llucax@gmail.com> changed:

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


--- Comment #5 from Leandro Lucarella <llucax@gmail.com> 2009-11-04 06:40:53 PST ---
SVN commit: http://www.dsource.org/projects/dmd/changeset/236

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


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> 2009-11-06 11:32:29 PST ---
Fixed dmd 1.051

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