Thread overview
'static' keyword for positional array initialization
Aug 11, 2009
Ali Cehreli
Aug 12, 2009
Ali Cehreli
Aug 12, 2009
grauzone
Aug 12, 2009
Don
Aug 12, 2009
Ali Cehreli
Aug 12, 2009
Don
August 11, 2009
The 'static' keyword is required by dmd 2.031 for the second of these two definitions:

  int[2] static_0 = [ 1, 1 ];
  static int[2] static_1 = [ 1:1 ];

Is this inconsistency by design? Should 'static' be required for both or neither?

Ali

August 12, 2009
Ali Cehreli wrote:
> The 'static' keyword is required by dmd 2.031 for the second of these two definitions:
> 
>   int[2] static_0 = [ 1, 1 ];
>   static int[2] static_1 = [ 1:1 ];
> 
> Is this inconsistency by design? Should 'static' be required for both or neither?
> 
> Ali
> 


I've tried with DMD 2.031, and I can't reproduce this. This works fine for me:

  int[2] static_0 = [ 1, 1 ]:
  int[2] static_1 = [ 1:1 ];

Where did you put the declarations? I've tried putting them at both module level and in a class, and both times it compiled without problems.

-Lars
August 12, 2009
Lars T. Kyllingstad Wrote:

> I've tried with DMD 2.031, and I can't reproduce this. This works fine for me:
> 
>    int[2] static_0 = [ 1, 1 ]:
>    int[2] static_1 = [ 1:1 ];
> 
> Where did you put the declarations? I've tried putting them at both module level and in a class, and both times it compiled without problems.

Thank you very much for both of your answers. :)

I hadn't realized that the location of the definitions would make a difference. As you say, both of the lines work for me in the global scope (probably in a class too), but not in main:

void main()
{
    int[2] static_0 = [ 1, 1 ];
    int[2] static_1 = [ 1:1 ];
}

dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.

The 'static' keyword on the 1:1 line fixes the problem...

I am wondering what the correct syntax is... I am writing a D tutorial, so I think I ought to know. :D If this is a bug, I can leave a temporary note mentioning the bug... :)

Ali

August 12, 2009
Ali Cehreli wrote:
> Lars T. Kyllingstad Wrote:
> 
>> I've tried with DMD 2.031, and I can't reproduce this. This works fine for me:
>>
>>    int[2] static_0 = [ 1, 1 ]:
>>    int[2] static_1 = [ 1:1 ];
>>
>> Where did you put the declarations? I've tried putting them at both module level and in a class, and both times it compiled without problems.
> 
> Thank you very much for both of your answers. :)
> 
> I hadn't realized that the location of the definitions would make a difference. As you say, both of the lines work for me in the global scope (probably in a class too), but not in main:
> 
> void main()
> {
>     int[2] static_0 = [ 1, 1 ];
>     int[2] static_1 = [ 1:1 ];
> }
> 
> dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
> 
> The 'static' keyword on the 1:1 line fixes the problem...
> 
> I am wondering what the correct syntax is... I am writing a D tutorial, so I think I ought to know. :D If this is a bug, I can leave a temporary note mentioning the bug... :)
> 
> Ali


The error message shows that this is definitely a compiler bug. Would you mind adding it to Bugzilla?

  http://d.puremagic.com/issues/

-Lars
August 12, 2009
Ali Cehreli wrote:
> Lars T. Kyllingstad Wrote:
> 
>> I've tried with DMD 2.031, and I can't reproduce this. This works fine for me:
>>
>>    int[2] static_0 = [ 1, 1 ]:
>>    int[2] static_1 = [ 1:1 ];
>>
>> Where did you put the declarations? I've tried putting them at both module level and in a class, and both times it compiled without problems.
> 
> Thank you very much for both of your answers. :)
> 
> I hadn't realized that the location of the definitions would make a difference. As you say, both of the lines work for me in the global scope (probably in a class too), but not in main:
> 
> void main()
> {
>     int[2] static_0 = [ 1, 1 ];
>     int[2] static_1 = [ 1:1 ];
> }
> 
> dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
> 
> The 'static' keyword on the 1:1 line fixes the problem...

The error message is a compiler bug, but AFAIK the code above is not valid anyway. The special initializer syntax (apparently called static inittializers) for arrays and structs only works for data that's stored on the data segment. This applies for global variables, static variables, and class member initializers.

It simply doesn't work for normal variables, which allocate their storage on the stack.
August 12, 2009
Ali Cehreli wrote:
> Lars T. Kyllingstad Wrote:
> 
>> I've tried with DMD 2.031, and I can't reproduce this. This works fine for me:
>>
>>    int[2] static_0 = [ 1, 1 ]:
>>    int[2] static_1 = [ 1:1 ];
>>
>> Where did you put the declarations? I've tried putting them at both module level and in a class, and both times it compiled without problems.
> 
> Thank you very much for both of your answers. :)
> 
> I hadn't realized that the location of the definitions would make a difference. As you say, both of the lines work for me in the global scope (probably in a class too), but not in main:
> 
> void main()
> {
>     int[2] static_0 = [ 1, 1 ];
>     int[2] static_1 = [ 1:1 ];
> }
> 
> dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.

I've added this to Bugzilla as bug 3246. With a patch <g>.
August 12, 2009
Don Wrote:

> > void main()
> > {
> >     int[2] static_0 = [ 1, 1 ];
> >     int[2] static_1 = [ 1:1 ];
> > }
> > 
> > dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
> 
> I've added this to Bugzilla as bug 3246. With a patch <g>.

Thank you for the quick fix! :)

But I still can't parse the answer that I am looking for. :)

The compiler should pass the assertion, ok, but what should itdo after that?

a) Reject both
b) Accept both
c) Accept static_0 but reject static_1

Option c would contradict with the documentattion though:

  http://digitalmars.com/d/2.0/arrays.html

It says: "Static arrays are distinguished by having a length fixed at compile time."

Also says: "Static Initialization of Static Arrays: Static initalizations are supplied by a list of array element values enclosed in [ ]. The values can be optionally preceded by an index and a :."

Those definitions make me think that both static_0 and static_1 should both be accepted.

Thank you,
Ali

August 12, 2009
Ali Cehreli wrote:
> Don Wrote:
> 
>>> void main()
>>> {
>>>     int[2] static_0 = [ 1, 1 ];
>>>     int[2] static_1 = [ 1:1 ];
>>> }
>>>
>>> dmd: init.c:431: virtual Expression* ArrayInitializer::toExpression(): Assertion `j < edim' failed.
>> I've added this to Bugzilla as bug 3246. With a patch <g>.
> 
> Thank you for the quick fix! :)
> 
> But I still can't parse the answer that I am looking for. :)
> 
> The compiler should pass the assertion, ok, but what should itdo after that?
> 
> a) Reject both
> b) Accept both
> c) Accept static_0 but reject static_1
> 
> Option c would contradict with the documentattion though:
> 
>   http://digitalmars.com/d/2.0/arrays.html
> 
> It says: "Static arrays are distinguished by having a length fixed at compile time."

'static' means two different things. There, it means 'fixed length'.
It can also mean 'in the static data segment' which is quite different.
This terminology causes a lot of confusion.
> 
> Also says: "Static Initialization of Static Arrays: Static initalizations are supplied by a list of array element values enclosed in [ ]. The values can be optionally preceded by an index and a :."
> 
> Those definitions make me think that both static_0 and static_1 should both be accepted.

Definitely they should, eventually. But right now, the compiler generates really terrible code for array assignments anyway, so you're not losing much.