Bug 23381 – null/init are not accepted as initializers of a zero-sized static array

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-09-30T13:23:45Z
Last change time
2024-12-13T19:24:51Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Max Samukha
Moved to GitHub: dmd#20160 →

Comments

Comment #0 by maxsamukha — 2022-09-30T13:23:45Z
int[0] a = []; // ok int[0] a1 = null; // fail int[0] a2 = (int[]).init; // fail void main() { } onlineapp.d(2): Error: cannot implicitly convert expression `null` of type `typeof(null)` to `int[0]` onlineapp.d(3): Error: cannot implicitly convert expression `null` of type `int[]` to `int[0]` Accepted in function scopes: void main() { int[0] a1 = null; // ok int[0] a2 = (int[]).init; // ok }
Comment #1 by nick — 2022-10-03T11:59:34Z
> int[0] a1 = null; // ok I don't think that should work - a static array is not a reference. Just use `[]` or init. Especially as there are no compile-time errors when the array length is > 0 for null, but there are for `[]`: void f() { int[1] b = []; // error int[1] a = null; // no error a = []; // error a = null; // no error }
Comment #2 by maxsamukha — 2022-10-04T11:46:09Z
(In reply to Nick Treleaven from comment #1) > > int[0] a1 = null; // ok > > I don't think that should work - a static array is not a reference. Just use > `[]` or init. Especially as there are no compile-time errors when the array > length is > 0 for null, but there are for `[]`: > > void f() > { > int[1] b = []; // error > int[1] a = null; // no error > a = []; // error > a = null; // no error > } If null is implicitly convertable to a dynamic array type, I believe it should be accepted anywhere a dynamic array is accepted. void f() { > int[1] a = null; // no error > a = null; // no error } Those produce a rangle violation error at runtime. If size mismatches can be detected for [] at compile time, why can't they for 'null'?
Comment #3 by robert.schadek — 2024-12-13T19:24:51Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20160 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB