D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5290 - Static array literals with too few elements
Summary: Static array literals with too few elements
Status: RESOLVED WORKSFORME
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: 2010-11-29 05:55 UTC by David Simcha
Modified: 2022-03-10 06:23 UTC (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2010-11-29 05:55:24 UTC
The following invalid code is accepted:

import std.stdio;

void main() {
    real[2] foo = [1.0L];
    writeln(foo);  // [1, 0]
}
Comment 2 bearophile_hugs 2011-09-09 10:29:30 UTC
(In reply to comment #1)
> https://github.com/D-Programming-Language/dmd/pull/375

Pull 375 is not about bug 3849 too, right?
Comment 3 Kenji Hara 2011-09-09 10:40:55 UTC
Pull 375 does not support `int[$] = [1, 2, 3];`.
Comment 4 bearophile_hugs 2011-09-09 11:00:10 UTC
(In reply to comment #3)
> Pull 375 does not support `int[$] = [1, 2, 3];`.

OK. This is expected.

Currenly DMD runs code like this (I don't like this, but this is working as designed!):

int[3] arr = [1, 2];
void main() {}


I presume Pull 375 lets this kind of code pass, right?
Comment 5 Kenji Hara 2011-09-09 11:23:57 UTC
(In reply to comment #4)
> int[3] arr = [1, 2];
> void main() {}
>
> I presume Pull 375 lets this kind of code pass, right?

??? Pull 375 rejects statically above code, it is expected behavior from this issue.
Comment 6 bearophile_hugs 2011-09-09 11:31:56 UTC
(In reply to comment #5)

> ??? Pull 375 rejects statically above code, it is expected behavior from this
> issue.

I see. I suggest you to read the whole thread of bug 3849.

Code like the following is working as designed, so if your patch refuses this code, then your patch is implementing an enhancement too (it means it's changing the D specs):

int[3] arr = [1, 2];
void main() {}


Note that I support turning this kind of code into a compile-time error, but Walter (used to) wants it to compile. So at least I suggest your comment of Pull 375 to say this example is now a compile-time error.
Comment 7 bearophile_hugs 2011-09-09 11:34:38 UTC
(In reply to comment #6)

> Code like the following is working as designed, so if your patch refuses this
> code, then your patch is implementing an enhancement too (it means it's
> changing the D specs):
> 
> int[3] arr = [1, 2];
> void main() {}


Note: For the situations where people want to specify less items than the arrays length I suggested an *explicit* syntax, like:

int[3] arr = [1, 2, ...];
void main() {}
Comment 9 Walter Bright 2012-01-20 12:57:27 UTC
(In reply to comment #0)
> The following invalid code is accepted:
> import std.stdio;
> void main() {
>     real[2] foo = [1.0L];
>     writeln(foo);  // [1, 0]
> }

Running it gives:

object.Exception@src\rt\arraycat.d(31): lengths don't match for array copy
Comment 10 bearophile_hugs 2012-01-20 15:08:36 UTC
(In reply to comment #9)
> (In reply to comment #0)
> > The following invalid code is accepted:
> > import std.stdio;
> > void main() {
> >     real[2] foo = [1.0L];
> >     writeln(foo);  // [1, 0]
> > }
> 
> Running it gives:
> 
> object.Exception@src\rt\arraycat.d(31): lengths don't match for array copy

D is statically typed, so it's better to refuse it at compile-time.
Comment 11 timon.gehr 2012-01-20 15:38:41 UTC
Actually, it would be very cool if D would re-use the integer range trick here and catch all cases that can be proven not to work using it, such that the following code would error at compile time: ;)

int x;
readf("%d",&x);
real[2] foo = 
    x%100>50 ?
    [1.0L, 2.0L, 3.0L][0..2|x&1] :
    ([1.0L, 2.0L] ~ [3.0L, 4.0L] ~ 5.0L)[x&1..$-!(x&1)];
Comment 12 Don 2012-01-24 06:29:05 UTC
(In reply to comment #10)
> (In reply to comment #9)
> > (In reply to comment #0)
> > > The following invalid code is accepted:
> > > import std.stdio;
> > > void main() {
> > >     real[2] foo = [1.0L];
> > >     writeln(foo);  // [1, 0]
> > > }
> > 
> > Running it gives:
> > 
> > object.Exception@src\rt\arraycat.d(31): lengths don't match for array copy
> 
> D is statically typed, so it's better to refuse it at compile-time.


Yes, but that's bug 2547.
Comment 13 Mathias LANG 2022-03-10 06:23:19 UTC
As of 2.099, the OP sample properly errors out.

There's still a few issues related to this. For example, issue 19095 still applies, but a PR for it was made and closed due to the effect it had on code.
But closing this as "WORKSFORME" since the other issues are more fine-grained, up-to-date, and have discussions.