Issue 2333 - Hash initializer does not work
Summary: Hash initializer does not work
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: wrong-code
: 2265 (view as issue list)
Depends on:
Blocks:
 
Reported: 2008-09-02 23:15 UTC by Andrei Alexandrescu
Modified: 2015-06-09 05:15 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2008-09-02 23:15:08 UTC
The assertion in this program fails:

void main()
{
    int[string] stopWords = [ "abc"[]:1 ];
    assert("abc" in stopWords);
}

This is so bad I must in fact be doing something wrong...
Comment 1 Sergey Gromov 2008-09-03 02:32:11 UTC
Even more: this program:

import std.stdio;
void main()
{
	int[string] foo = ["aaa":1, "bbb":2];
	foo["ccc"] = 3;
	writeln(foo);
	writeln("aaa" in foo);
	writeln("ccc" in foo);
}

prints this:

	[aaa:1,bbb:2,ccc:3]
	0
	8F4FB4

when compiled using Windows DMD 2.018.
Comment 2 Gide Nwawudu 2008-09-03 06:41:38 UTC
The docs says 'An AssocArrayLiteral cannot be used to statically initialize anything', so the code should be rejected by the compiler. Changed keyword to acepts-invalid.
http://www.digitalmars.com/d/2.0/expression.html#AssocArrayLiteral

BTW I've made the same mistake, see BUG 1727.
Comment 3 Andrei Alexandrescu 2008-09-03 07:14:03 UTC
(In reply to comment #1)
> Even more: this program:
> 
> import std.stdio;
> void main()
> {
>         int[string] foo = ["aaa":1, "bbb":2];
>         foo["ccc"] = 3;
>         writeln(foo);
>         writeln("aaa" in foo);
>         writeln("ccc" in foo);
> }
> 
> prints this:
> 
>         [aaa:1,bbb:2,ccc:3]
>         0
>         8F4FB4
> 
> when compiled using Windows DMD 2.018.

The last line is fine. "in" returns a pointer.
Comment 4 Andrei Alexandrescu 2008-09-03 07:14:44 UTC
(In reply to comment #2)
> The docs says 'An AssocArrayLiteral cannot be used to statically initialize
> anything', so the code should be rejected by the compiler. Changed keyword to
> acepts-invalid.
> http://www.digitalmars.com/d/2.0/expression.html#AssocArrayLiteral
> 
> BTW I've made the same mistake, see BUG 1727.

Notice that I'm using straight initialization, not static initialization.
Comment 5 Gide Nwawudu 2008-09-03 07:57:16 UTC
In that case my comment on bug 1727 was valid, I'll repeat it here. The following code compiles on both GDC and DMD, but DMD compiled code throws an array bounds exception when executed.

test.d
------
import std.stdio;

void main() {
        int[char[]] aa =  ["aa":1, "bb":2, "cc":3]; // Fails later
        //int[char[]] aa; aa =  ["aa":1, "bb":2, "cc":3]; // Fails later 
        //int[char[]] aa; aa["aa"] = 1; aa["bb"] = 2; aa["cc"] = 3; // OK

        assert(aa.length == 3);
        assert(aa["aa"]==1); // DMD fails here. Error: ArrayBoundsError test(9)
        writefln(aa["aa"],aa["bb"],aa["cc"]); // On GDC output -> 123
}
Comment 6 Sergey Gromov 2008-09-03 10:33:42 UTC
(In reply to comment #3)
> (In reply to comment #1)
> >         [aaa:1,bbb:2,ccc:3]
> >         0
> >         8F4FB4
> 
> The last line is fine. "in" returns a pointer.

I know. I posted this to maybe help to narrow down the issue. The printed array contents are correct. The explicitly added key ("ccc") is found correctly. But, at the same time, the key "aaa" is not found even though it seems to be present in the AA.
Comment 7 Walter Bright 2008-10-20 22:22:27 UTC
Fixed dmd 2.020
Comment 8 Gide Nwawudu 2008-11-18 18:43:50 UTC
*** Bug 2265 has been marked as a duplicate of this bug. ***