Bug 1899 – AA of fixed-length arrays fails to initialize
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-03-08T16:45:00Z
Last change time
2015-06-09T01:14:36Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
ddparnell
Comments
Comment #0 by ddparnell — 2008-03-08T16:45:43Z
The following code fails with an ArrayBoundsError on the assignment.
void main()
{
int[3][string] AA;
AA["abc"] = [5,4,3];
}
But this code does not fail ...
void main()
{
struct S { int[3] v; }
S[string] AA;
AA["abc"] = S([5,4,3]);
}
Comment #1 by clugdbug — 2010-07-13T08:06:19Z
*** Issue 4343 has been marked as a duplicate of this issue. ***
Comment #2 by clugdbug — 2011-01-19T13:12:10Z
PATCH: AssignExp::semantic(). We mustn't convert it into a slice assignment, because it may be a construction.
Also require an implicit conversion, so that we can allow assignment of array literals. Quick test case:
void bug1899()
{
int[3][string] AA;
int[3] x = [5,4,3];
AA["abc"] = x;
assert(AA["abc"] == x);
AA["def"] = [1,2,3];
assert(AA["def"]==[1,2,3]);
}
---
Index: expression.c
===================================================================
--- expression.c (revision 882)
+++ expression.c (working copy)
@@ -9086,10 +9086,23 @@
}
if (t1->ty == Tsarray && !refinit)
- { // Convert e1 to e1[]
+ {
+ if (e1->op == TOKindex &&
+ ((IndexExp *)e1)->e1->type->toBasetype()->ty == Taarray)
+ {
+ // Assignment to an AA of fixed-length arrays.
+ // Convert T[n][U] = T[] into T[n][U] = T[n]
+ e2 = e2->implicitCastTo(sc, e1->type);
+ if (e2->type == Type::terror)
+ return e2;
+ }
+ else
+ {
+ // Convert e1 to e1[]
Expression *e = new SliceExp(e1->loc, e1, NULL, NULL);
e1 = e->semantic(sc);
t1 = e1->type->toBasetype();
+ }
}
e2->rvalue();
@@ -9131,8 +9144,13 @@
else if (t1->ty == Tsarray)
{
/* Should have already converted e1 => e1[]
+ * unless it is an AA
*/
- assert(op == TOKconstruct);
+ if (!(e1->op == TOKindex && t2->ty == Tsarray &&
+ ((IndexExp *)e1)->e1->type->toBasetype()->ty == Taarray))
+ {
+ assert(op == TOKconstruct);
+ }
//error("cannot assign to static array %s", e1->toChars());
}
else if (e1->op == TOKslice)
Comment #3 by clugdbug — 2011-01-23T12:52:24Z
*** Issue 5292 has been marked as a duplicate of this issue. ***