I think this is not a bug, but it seems like unnatural behavior.
---
auto foo()
{
return [1];
}
int[2] ARR = [1];
int[2] ARR_2 = foo(); // Error: mismatched array lengths, 2 and 1
void main()
{
assert(ARR == [1, 0]); // Success.
int[2]arr = [1]; // Error: mismatched array lengths, 2 and 1
}
---
Comment #1 by b2.temp — 2018-07-18T15:00:44Z
Not a bug as you say. And this is too different constructs, not related to static initialization (which means that the title of the report is not correct btw), see https://dlang.org/spec/declaration.html#NonVoidInitializer
ArrayInitializer
int[2] ARR = [1]; // "[" -> left square so parse a "ArrayInitializer"
ExpInitializer
int[2] ARR_2 = foo(); // "f" -> ident char so parse a "ExpInitializer"
ArrayInitializer is useful to make lookup tables, i.e you mostly want the default value but a particular value for a few cases. This cant be fixed, it's part of the language and it's used, notably, in the source code of the runtime for example. It would not be reasonable to force all entries to be initialized.
Comment #2 by b2.temp — 2018-07-18T15:14:35Z
(In reply to Basile B. from comment #1)
> Not a bug as you say. And this is too different constructs
Forgot to say but, the error you made is to think that the call to "foo()" is eluded, i.e simplified to its return.
Comment #3 by b2.temp — 2018-07-18T15:20:16Z
(In reply to Basile B. from comment #2)
> (In reply to Basile B. from comment #1)
> > Not a bug as you say. And this is too different constructs
>
> Forgot to say but, the error you made is to think that the call to "foo()"
> is eluded, i.e simplified to its return.
In the foo() call you have an 'ArrayLiteral', which does not allow the freedom of of 'ArrayInitializer', for obvious reasons : when used in expressions the whole stuff is required, while in an initializer a special semantic can be applied since the type of the thing to stuff is (usually) known.
Comment #4 by kubo39 — 2018-07-19T12:20:29Z
Thank you for the explanations, yes, the title of the report is not correct.
I am still not convinced that the global array initialization has been successful in this case.
(Attemptive PR : https://github.com/dlang/dmd/pull/8503)
Comment #5 by b2.temp — 2018-07-19T14:32:01Z
Sorry if i've closed this a bit eagerly but actually i wanted to do the same something like 18 months ago and you'll see that in addition to your PR you'll have to fix druntime and Phobos which use this syntax, which means that the change is much less trivial that it appears at first glance. The final decision owns to the D team so you'll see what people think in the PR.
Comment #6 by dlang-bot — 2019-11-18T09:21:02Z
@RazvanN7 created dlang/dmd pull request #10585 "Fix Issue 19095 - deprecate array length mismatch in global static arrays" fixing this issue:
- Fix Issue 19095 - deprecate array length mismatch in global static arrays
https://github.com/dlang/dmd/pull/10585
Comment #7 by pro.mathias.lang — 2020-05-15T03:42:55Z
*** Issue 10192 has been marked as a duplicate of this issue. ***
Comment #8 by pro.mathias.lang — 2020-05-15T03:43:31Z