www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22697] New: Segfault when accessing a recursive lambda

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

          Issue ID: 22697
           Summary: Segfault when accessing a recursive lambda declared
                    inside a function generated with a mixin template
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jlourenco5691 gmail.com

When passing a recursive lambda function to a mixin template, that lambda
cannot be accessed inside a function generated by the same mixin template.

```
mixin template mix(alias fun) {
    auto foo(Args...)(Args args)
    {
        return fun(args);
    }
}

void main()
{
    mixin mix!((size_t n) {
        alias self = __traits(parent, {});
        if (n < 2) return n;
        else return self(n - 1) + self(n - 2);
    });

    foo(1).writeln;
}
```

This example causes the compiler to segfault. This same example works with ldc.
If instead of a lambda a named function is used the code works as intended.

A reduced example:

```
mixin template mix(alias fun) {
    auto foo()
    {
        return fun;
    }
}

void main()
{
    mixin mix!((size_t n) {
        alias self = __traits(parent, {});
    });
}
```

--
Jan 22 2022