Zero-length static array spec

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Feb 2 06:32:25 PST 2015


On 2/1/15 9:42 AM, Stefan Frijters wrote:
> So recently I ran into this discrepancy between the behaviour of dmd
> (and gdc) and ldc2:
>
> void test(int[] data)
>    in { assert(data, "data must be non-null."); }

Note, this will soon fail to compile. use assert(data.ptr) instead.

>    body { }
>
> void main() {
>    import std.stdio;
>    int[1] data1;
>    writeln(data1); // [0]
>    test(data1); // Passes
>    assert(data1.ptr !is null);
>    int[0] data0;
>    writeln(data0); // []
>    test(data0); // Passes with dmd and gdc, fails with ldc2 (2.066.1)
>    assert(data0.ptr !is null); // Passes with dmd and gdc, fails with ldc2
> }
>
> I reported this as an issue at
> https://github.com/ldc-developers/ldc/issues/831 and was asked to check
> for a more definite answer. So, in light of recent developments of
> trying to tighten up the D spec, does anyone have any insight what the
> correct behaviour should be, and can it be locked down in the spec?
>
> Currently the D spec says [1]:
>
> ---
>
> Static Arrays
> int[3] s;
> These are analogous to C arrays. Static arrays are distinguished by
> having a length fixed at compile time.
>
> The total size of a static array cannot exceed 16Mb. A dynamic array
> should be used instead for such large arrays.
>
> A static array with a dimension of 0 is allowed, but no space is
> allocated for it. It's useful as the last member of a variable length
> struct, or as the degenerate case of a template expansion.

The lynch pin here is that "it's useful as the last member of a variable 
length struct." If it's given the address of 0, then it's no longer 
useful there, so it should take an address of where it is defined.

I'd say this is definitely a bug (and the docs should be clearer on this).

-Steve


More information about the Digitalmars-d mailing list