D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 12652 - Non-constant hash initializers should have a special-case diagnostic
Summary: Non-constant hash initializers should have a special-case diagnostic
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2014-04-26 10:48 UTC by Andrej Mitrovic
Modified: 2022-08-24 12:13 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2014-04-26 10:48:52 UTC
-----
int[int] x = [1:1];

void main() { }
-----

test.d(2): Error: non-constant expression [1:1]

Associative arrays are supposed to be a first-class feature of D, but until we make them CTFE-able we should emit better diagnostics (essentially do some special-cases when hashes are involved).

The above code is something newcomers to D very frequently run into, and the diagnostic doesn't really tell them what's wrong. Some suggestions:

-----
Error: Associative array literals currently cannot be used to initialize globals. Try using a module constructor instead.
-----

This will at least point them to a workaround. Maybe a link to a page with a workaround would be nice too.
Comment 1 Dlang Bot 2020-08-20 11:40:54 UTC
@adelavais created dlang/dmd pull request #11598 "Issue 12652 - Non-constant hash initializers should have a special-case diagnostic" mentioning this issue:

- Issue 12652 - Non-constant hash initializers should have a special-case diagnostic

https://github.com/dlang/dmd/pull/11598
Comment 2 Dlang Bot 2022-08-24 12:13:58 UTC
dlang/dmd pull request #11598 "Issue 12652 - Non-constant hash initializers should have a special-case diagnostic" was merged into master:

- 909d08d9a55c50eaf463c6548d76d00cda4a3e40 by Adela Vais:
  Issue 12652 - Non-constant hash initializers should have a special-case diagnostic

- 55bc910e1703be56a4cb35b38e7272ffd3db04ab by RazvanN7:
  Fix Issue 12652 - Non-constant hash initializers should have a special-case diagnostic

https://github.com/dlang/dmd/pull/11598