Bug 1253 – array initializers as expressions are not allowed in const arrays

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-06-01T08:23:00Z
Last change time
2015-06-09T01:20:50Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
oskar.linde
Depends on
2096, 2170, 2356, 2375, 2380

Comments

Comment #0 by oskar.linde — 2007-06-01T08:23:27Z
void main() { struct T { int a; int b;} const T[] arr = [{1,1},{2,2}]; T[] arr2 = arr.dup; assert(arr2 == arr); } Gives: test.d:3: Error: array initializers as expressions are not allowed test.d:3: Error: array initializers as expressions are not allowed Interestingly, commenting out the assert line makes the code compile. changing const -> static also makes the code compile. The bug is confirmed to exist on 0.175, 0.176, 0.178, 1.006, 1.007, 1.013 (gdc 0.23) and 1.014 On DMD 0.174 (and earlier), the code compiles ok.
Comment #1 by thomas-dloop — 2007-08-25T16:42:02Z
Comment #2 by dfj1esp02 — 2008-05-11T10:38:18Z
exists in 2.012, doesn't depend on asserts struct A { int a,b; } void f() { A[2] rg=[{1,2},{1,2}]; }
Comment #3 by dfj1esp02 — 2008-05-11T10:51:31Z
workaround :-/ struct A { int a,b; } void f() { A[2] rg=[A(1,2),A(3,4)]; }
Comment #4 by dfj1esp02 — 2008-09-30T04:47:11Z
other initialization-related info bug 2096, bug 2170, bug 2356, bug 2375
Comment #5 by clugdbug — 2009-09-01T07:33:56Z
This works for me on D1.046. It was fixed somewhere between 1.020 and 1.041. It also works on DMD2.031, if you change 'const' to 'enum' to preserve the semantics. Oddly, however, on DMD2.031 the code as-written still displays: bug.d(173): Error: array initializers as expressions are not allowed So this is now a D2-only bug, which is not a regression. It's never worked with these semantics. Checked as far back as 2.022.
Comment #6 by clugdbug — 2009-09-01T08:24:13Z
And here's a patch. Cause: The initializer is used in declaration.c, VarDeclaration::semantic(). We see the lines: ------ Expression *e = init->toExpression(); if (!e) { init = init->semantic(sc, type); e = init->toExpression(); if (!e) ------ The initializer is supposed to return NULL if the initializer semantic hasn't been run yet. But the array initializer doesn't -- it prints an error, and returns an ErrorExp instead! It never has a chance. PATCH: init.c, last lines of ArrayInitializer::toExpression() (around line 473). Lno: delete elements; - error(loc, "array initializers as expressions are not allowed"); - return new ErrorExp(); + return NULL; }
Comment #7 by bugzilla — 2009-10-06T02:19:51Z
Fixed dmd 2.033