Bug 19095 – Static initiatization statements of an array appeared in different kind of scopes have inconsistent semantics

Status
REOPENED
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-07-18T13:57:15Z
Last change time
2024-12-13T18:59:37Z
Keywords
pull
Assigned to
No Owner
Creator
Hiroki Noda
Moved to GitHub: dmd#19458 →

Comments

Comment #0 by kubo39 — 2018-07-18T13:57:15Z
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
Note that bearophile had a few more test cases and useful links: https://issues.dlang.org/show_bug.cgi?id=10192
Comment #9 by robert.schadek — 2024-12-13T18:59:37Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19458 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB