www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21031] New: Undefined behaviour: associative array literals

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

          Issue ID: 21031
           Summary: Undefined behaviour: associative array literals with
                    side effects.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ibuclaw gdcproject.org

Example:
---
int a()
{
    static int b;
    return ++b;
}
void main()
{
    import core.stdc.stdio;
    auto aa = [a:a, a:a, a:a, a:a];
    foreach (k, v; aa)
        printf("%d: %d\n", k, v);
}
---

DMD outputs:
4: 8
3: 7
2: 6
1: 5

GDC outputs:
6: 2
7: 3
5: 1
8: 4

LDC outputs:
7: 8
5: 6
3: 4
1: 2

Each answer makes perfect logical sense when you consider how the literal
expression is lowered, but not all can be the correct, intuitive answer.

(NB: GDC output is when running on x86, every other target, e.g: ARM, would
evaluate in the same order as DMD x86).

--
Jul 09 2020