www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 874] New: Incorrect codegen (?) with tuples, string constants, and AAs

reply d-bugmail puremagic.com writes:
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.


-- 
Jan 22 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=874






Added to DStress as
http://dstress.kuehne.cn/run/o/odd_bug_13_A.d


-- 
Feb 23 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=874


Don <clugdbug yahoo.com.au> changed:

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





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: -------
Aug 12 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 30 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=874


Don <clugdbug yahoo.com.au> changed:

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



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: -------
Oct 30 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=874


Leandro Lucarella <llucax gmail.com> changed:

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



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: -------
Nov 04 2009
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Nov 06 2009