[Issue 23932] New: Slot is allocated before evaluating the value during associative array initialization

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat May 20 19:52:14 UTC 2023


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

          Issue ID: 23932
           Summary: Slot is allocated before evaluating the value during
                    associative array initialization
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dlang-bugzilla at thecybershadow.net

//////////////////// test.d ////////////////////
class C
{
    int[int] aa;

    this()
    {
        try
        {
            aa[42] = {
                if (true)
                    throw new Exception("oops");
                else
                    return 9;
            }();
        }
        catch (Exception e) {}
    }
}

void main()
{
    auto c = new C;
    assert(c.aa.length == 0);
}
////////////////////////////////////////////////

This allows obtaining default-initialized objects with `@disable this()`.

The order of operations needs to be changed. The value to be assigned to the AA
must be evaluated first, and only then a slot should be allocated for the value
to be moved into.

Another example which illustrates the problem:

//////////// test.d ///////////
final class C
{
    int[int] aa;

    this()
    {
        aa[42] = getValue();
    }

    int getValue()
    {
        assert(aa.length == 0);
        return 0;
    }
}

void main()
{
    auto c = new C;
}
///////////////////////////////

--


More information about the Digitalmars-d-bugs mailing list