D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15292 - [REG2.068.0] Segmentation fault with self-referencing struct / inout / alias this
Summary: [REG2.068.0] Segmentation fault with self-referencing struct / inout / alias ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 regression
Assignee: No Owner
URL:
Keywords: ice, pull
Depends on:
Blocks:
 
Reported: 2015-11-05 20:15 UTC by Vladimir Panteleev
Modified: 2015-12-01 13:22 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2015-11-05 20:15:31 UTC
From http://stackoverflow.com/q/33553013/21501:

///////////// test.d ////////////
import std.typecons;

struct Node {
    int value;
    NullableRef!Node left, right;
}

void main() {
    Node n;
}
/////////////////////////////////

Introduced in https://github.com/D-Programming-Language/dmd/pull/4820
Comment 1 Vladimir Panteleev 2015-11-05 20:21:07 UTC
Reduced:

//////// test.d ////////
struct NullableRef(T)
{
    inout(T) get() inout
    {
        assert(false);
    }

    alias get this;
}

struct Node
{
    NullableRef!Node n;
}
////////////////////////
Comment 2 Kenji Hara 2015-11-16 16:47:12 UTC
(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
Comment 4 github-bugzilla 2015-11-19 02:53:29 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/2f0cc57aa71a6ff67f27ec3727841ec756c843fb
fix Issue 15292 - Segmentation fault with self-referencing struct / inout / alias this

If you read the `expr.aliasthis` to `expr.tupleof`, you could understand that
the mechanism to detect recursive tupleof expansion is same with for alias-this'es.

https://github.com/D-Programming-Language/dmd/commit/dff37b9526d154f844f7a35340015ef3e93975d3
Merge pull request #5274 from 9rnsr/fix15292

[REG2.068.0] Issue 15292 - Segmentation fault with self-referencing struct / inout / alias this
Comment 5 github-bugzilla 2015-12-01 13:22:33 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/2f0cc57aa71a6ff67f27ec3727841ec756c843fb
fix Issue 15292 - Segmentation fault with self-referencing struct / inout / alias this

https://github.com/D-Programming-Language/dmd/commit/dff37b9526d154f844f7a35340015ef3e93975d3
Merge pull request #5274 from 9rnsr/fix15292