Thread overview
[Issue 2954] New: Appaling bug in associative arrays
May 08, 2009
d-bugmail
[Issue 2954] Appalling bug in associative arrays (D2 only)
Sep 14, 2009
Don
[Issue 2954] [tdpl] Appalling bug in associative arrays (D2 only)
Nov 12, 2010
Walter Bright
Nov 16, 2010
Don
Nov 19, 2010
Walter Bright
Nov 19, 2010
Don
Dec 05, 2010
Walter Bright
Dec 06, 2010
Walter Bright
May 08, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2954

           Summary: Appaling bug in associative arrays
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: blocker
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: andrei@metalanguage.com


This program compiles and prints "Abc":

import std.stdio;

void main() {
    uint[string] hash;
    char[] a = "abc".dup;
    hash[a] = 42;
    a[0] = 'A';
    writeln(hash.keys);
}

It should not compile because char[] is obviously not convertible to string. By this I am also reiterating the necessity to make associative arrays a true library type and have the compiler rewrite the literals and the type name to use that library type.


-- 

September 14, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2954


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
            Version|unspecified                 |2.020
             Blocks|                            |1934
            Summary|Appalling bug in            |Appalling bug in
                   |associative arrays          |associative arrays (D2
                   |                            |only)
         OS/Version|Linux                       |All


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2009-09-13 23:51:08 PDT ---
This test case, from bug 1934, is part of the same issue: index expressions for AAs don't have proper type checking. In the case below, it's not converting the string literal into a char[3], and consequently, bad code generation results. Both asserts fail.

void main()
{
    char[char[3]] ac;
    char[3] c = "abc";
    ac["abc"]='a';
    assert(ac[c]=='a');

    char[dchar[3]] ad;
    dchar[3] d = "abc"d;
    ad["abc"d]='a';
    assert(ad[d]=='a');
}

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


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2010-11-11 17:56:01 PST ---
http://www.dsource.org/projects/dmd/changeset/749

This fixes Andrei's bug, but Don's is a different bug.

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


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

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


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2010-11-16 01:10:11 PST ---
The fix for bug 2684 was incorrect; it was too general. We can't skip the implicit conversion in all cases where the arrays are equality comparable.

For example, this code:

    char[char[3]] ac;
    dchar[3] d = "abc"d;
    ac[d] = 'w';
gives a poor error message:
bug.d(6): Error: array equality comparison type mismatch, dchar[3u] vs char
[3u]
instead of "cannot implicitly convert"

PATCH: (1) Add this function to cast.c

/***********************************
 * See if both types are arrays that can be compared
 * for equality without any casting. Return !=0 if so.
 * This is to enable comparing things like an immutable
 * array with a mutable one.
 */
int arrayTypeCompatibleWithoutCasting(Loc loc, Type *t1, Type *t2)
{
    t1 = t1->toBasetype();
    t2 = t2->toBasetype();

    if ((t1->ty == Tarray || t1->ty == Tsarray || t1->ty == Tpointer) &&
        t2->ty == t1->ty)
    {
        if (t1->nextOf()->implicitConvTo(t2->nextOf()) >= MATCHconst ||
            t2->nextOf()->implicitConvTo(t1->nextOf()) >= MATCHconst)
            return 1;
    }
    return 0;
}

(2) expression.c line 8580

        case Taarray:
        {   TypeAArray *taa = (TypeAArray *)t1;
+            /* We can skip the implicit conversion if they differ only by
+             * constness (Bugzilla 2684, see also bug 2954b)
+             */
+            if (!arrayTypeCompatibleWithoutCasting(e2->loc, e2->type,
taa->index) )
-            if (!arrayTypeCompatible(e2->loc, e2->type, taa->index))
            {
                e2 = e2->implicitCastTo(sc, taa->index);        // type
checking
            }
            type = taa->next;

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



--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2010-11-18 20:48:37 PST ---
The fix still isn't right, as it fails at compile time on line 10.

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



--- Comment #5 from Don <clugdbug@yahoo.com.au> 2010-11-19 06:36:06 PST ---
(In reply to comment #4)
> The fix still isn't right, as it fails at compile time on line 10.

It works for me. Hmm.
I think this might also be relying on the patch for bug 5218, which I still
have active in my local copy. Sorry about that -- it needs to be included as
well.

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



--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> 2010-12-04 19:08:10 PST ---
(In reply to comment #5)
> I think this might also be relying on the patch for bug 5218, which I still have active in my local copy. Sorry about that -- it needs to be included as well.

The patch for bug 5218 enables it to compile, but it fails at runtime with:

core.exception.RangeError@test(6): Range violation

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


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2010-12-06 12:21:48 PST ---
http://www.dsource.org/projects/dmd/changeset/789

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