Bug 21031 – Undefined behaviour: associative array literals with side effects.

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-07-09T09:19:26Z
Last change time
2024-12-13T19:09:59Z
Assigned to
No Owner
Creator
Iain Buclaw
Moved to GitHub: dmd#17970 →

Comments

Comment #0 by ibuclaw — 2020-07-09T09:19:26Z
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).
Comment #1 by robert.schadek — 2024-12-13T19:09:59Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17970 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB