www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16763] New: Associative array literal inside array or AA

https://issues.dlang.org/show_bug.cgi?id=16763

          Issue ID: 16763
           Summary: Associative array literal inside array or AA literal
                    doesn't work as initializer if variable type is known
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: accepts-invalid, rejects-valid
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: verylonglogin.reg gmail.com

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).

--
Nov 24 2016