www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23932] New: Slot is allocated before evaluating the value

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 puremagic.com
          Reporter: dlang-bugzilla 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;
}
///////////////////////////////

--
May 20 2023