www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23083] New: .tupleof on static array rvalue evaluates

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

          Issue ID: 23083
           Summary: .tupleof on static array rvalue evaluates expression
                    multiple times
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: snarwin+bugzilla gmail.com

As of DMD 2.100.0-rc.1, the following program causes an AssertError at runtime:

---
int global = 0;

int[2] f()
{
    global++;
    return [123, 456];
}

void g(int a, int b) {}

void main()
{
    g(f().tupleof);
    assert(global == 1);
}
---

The error is:

---
core.exception.AssertError bug.d(14): 2 != 1
---

This happens because the expression `f().tupleof` is erroneously expanded to
`f()[0].tupleof, f()[1].tupleof`, which results in `f` being called twice
instead of once.

--
May 03 2022