[Issue 23164] [REG 2.097] Infinite loop on assertion failure + DMD moves struct with copy constructor

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jul 9 07:59:37 UTC 2022


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

Iain Buclaw <ibuclaw at gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw at gdcproject.org

--- Comment #5 from Iain Buclaw <ibuclaw at gdcproject.org> ---
(In reply to Mathias LANG from comment #0)
> The following code shows 2 assertions failures in v2.096.1, an infinite loop
> on v2.097.0:
> ```
--snip--
>     ~this ()
>     {
>         assert(this.ptr is &this);
>     }
--snip--
> ```
> 
> The original test was to see whether or not DMD would move a struct with a
> copy constructor. The above code compiles and runs fine with LDC & GDC, but
> asserts with DMD.
To explain GDC behaviour. *Because* there is a destructor (or postblit, copy
constructor, or anything that would other make the struct non-trivially
copyable) then the type is *always* *implicitly* passed and returned by
invisible reference.

The right reduction would be this:
```
struct std_string
{
    std_string* ptr;
    ulong[3] data;

    this (ulong data)
    {
        this.data[] = data;
        this.ptr = &this;
    }

    ~this ()
    {
    }

    // GDC and LDC forces 'auto ref' to be 'ref', even when the
    // front-end decides to drop the 'ref' from the signature.
    ref std_string opAssign()(const auto ref std_string rhs)
    {
        assert(rhs.ptr is &rhs);
        return this;
    }
}

void main ()
{
    std_string tmp;
    tmp = std_string(42);
}
```

--


More information about the Digitalmars-d-bugs mailing list