www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2954] New: Appaling bug in associative arrays

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


-- 
May 08 2009
next sibling parent d-bugmail puremagic.com writes:
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



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


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Nov 11 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Nov 16 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2954




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: -------
Nov 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2954





 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: -------
Nov 19 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2954




19:08:10 PST ---

 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: -------
Dec 04 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2954


Walter Bright <bugzilla digitalmars.com> changed:

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



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: -------
Dec 06 2010