Bug 16763 – Associative array literal inside array or AA literal doesn't work as initializer if variable type is known

Status
NEW
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-24T17:28:05Z
Last change time
2024-12-13T18:50:47Z
Keywords
accepts-invalid, pull, rejects-valid
Assigned to
No Owner
Creator
Denis Shelomovskii
Moved to GitHub: dmd#19209 →

Comments

Comment #0 by verylonglogin.reg — 2016-11-24T17:28:05Z
This code should work: --- void main() { int[int][] a = [[1 : 2]]; // ok int[int][] b = [[0 : 2]]; // expression ([[1]]) of type int[][] int[int][int] c = [1 : [0 : 2]]; // expression ([1:[2]]) of type int[][int] int[int][int] d = [1 : [3 : 2]]; // Error: not an associative array initializer static assert(!__traits(compiles, { int[][] x = [[0 : 2]]; })); // fails static assert(!__traits(compiles, { int[][int] x = [1 : [0 : 2]]; })); // fails } --- bug.d(4): Error: cannot implicitly convert expression ([[2]]) of type int[][] to int[int][] bug.d(5): Error: cannot implicitly convert expression ([1:[2]]) of type int[][int] to int[int][int] bug.d(6): Error: not an associative array initializer --- Workarounds are: * not specify variable type (e.g. use `auto`) or * initialize with identity function called over literal or * initialize with default value and assign later. Note: Compiler behavior depends on actual constants used (0 or 1 as key) so it looks like something is terribly wrong (memory corruption or alike).
Comment #1 by verylonglogin.reg — 2016-12-16T14:59:19Z
(In reply to Denis Shelomovskii from comment #0) > int[int][] b = [[0 : 2]]; // expression ([[1]]) of type int[][] It was "expression ([[2]])" actually. So, looks like if `ArrayInitializer.inferType` isn't called AA literals are treated as array literals with explicit indices (see Static Initialization of Statically Allocated Arrays [1]) like in this code: --- import std.stdio; void main() { int[][] a = [[2: 6, 1: 345, 3: 78, 6: 12]]; writeln(a); int[][] aa = [1: [5: 7], 0: [7], 0: [0: 15] /* overrides previous element 0*/, [2: 6, 1: 3], /* overrides previous element 1*/ ]; writeln(aa); } --- [[0, 345, 6, 78, 0, 0, 12]] [[15], [0, 3, 6]] --- [1] http://dlang.org/spec/arrays.html#static-init-static
Comment #2 by dlang-bot — 2024-07-01T20:38:36Z
@ntrel created dlang/dmd pull request #16644 "Fix Bugzilla 16763 - Associative array literal inside array or AA lit…" fixing this issue: - Fix Bugzilla 16763 - Associative array literal inside array or AA literal doesn't work as initializer if variable type is known https://github.com/dlang/dmd/pull/16644
Comment #3 by robert.schadek — 2024-12-13T18:50:47Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19209 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB