[Issue 15292] [REG2.068.0] Segmentation fault with self-referencing struct / inout / alias this

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Nov 16 08:47:12 PST 2015


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

--- Comment #2 from Kenji Hara <k.hara.pg at gmail.com> ---
(In reply to Vladimir Panteleev from comment #1)
> Reduced:
> 
> //////// test.d ////////
> struct NullableRef(T)
> {
>     inout(T) get() inout
>     {
>         assert(false);
>     }
> 
>     alias get this;
> }
> 
> struct Node
> {
>     NullableRef!Node n;
> }
> ////////////////////////

An infinite recursive analysis happens in the implicitly generated member
function:

    bool Node.__xopEquals(ref const Node p, ref const Node q)
    {
        return p == q;
    }

The equality test p == q is expanded to p.tupleof == q.tupleof, and it's
equivalent with p.n == q.n.

Because of the alias-this definition in NuallbeRef!Node, the comparison is
delegated to the return of get member function, then it's rewritten to
p.n.get() == q.n.get(). Finally, we'll go into an endless circle.

Now I have a local patch to detect the circle. But I'm yet not sure how
compiler should work for the following code.

    void main()
    {
        Node node;
        assert(node == node);    // ?
    }

I think the Node equality should be either:
  1. make an error
  2. implicitly fallback to bitwise comparison

--


More information about the Digitalmars-d-bugs mailing list