D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5997 - Static arrays with 0 length accepted by compiler
Summary: Static arrays with 0 length accepted by compiler
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2011-05-13 19:25 UTC by Andrej Mitrovic
Modified: 2015-06-09 05:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2011-05-13 19:25:14 UTC
Is there a use case for 0-length static arrays?

If there's not a use-case, then this should probably be a compiler error:

int[0] logs;  // <- disallow this

void main()
{
    logs = [4];
}

The assignment statement causes a linker error (if you comment it out you won't get any linker errors):
/+
test.obj(test)  Offset 002DFH Record Type 009D
 Error 16: Index Range
--- errorlevel 1
+/
Comment 1 Vladimir Panteleev 2011-05-13 23:39:57 UTC
(In reply to comment #0)
> Is there a use case for 0-length static arrays?

I believe they can be used as a value type in an associative array to create a set.
Comment 2 bearophile_hugs 2011-05-14 02:57:56 UTC
One use case are variable-length structs:

struct MyArray(T) {
  size_t len;
  T data[0];

  // access methods here, that use data.offsetof
}

You create such array with a C malloc or GC malloc.
Comment 3 Andrej Mitrovic 2011-05-14 13:21:39 UTC
(In reply to comment #1)
> (In reply to comment #0)
> > Is there a use case for 0-length static arrays?
> 
> I believe they can be used as a value type in an associative array to create a
> set.

Ah, you're right. I even forgot about opening this topic which mentioned this trick: http://www.digitalmars.com/d/archives/digitalmars/D/A_case_for_valueless_AA_s_133165.html

I'm closing this down.