Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@nogc check fails when there are both nested @nogc function **and** a closure inside __trait(compiles) #20917

Open
yanok opened this issue Feb 26, 2025 · 0 comments

Comments

@yanok
Copy link
Contributor

yanok commented Feb 26, 2025

Here is an example:

auto test(int a) @nogc
{
    int foo() @nogc { return a + 3; }  // (1)
    enum yes = __traits(compiles, { return a; });  // (2)
    return 1;
}

This produces an error:

<source>(1): Error: function `example.test` is `@nogc` yet allocates closure for `test()` with the GC
auto test(int a) @nogc
     ^
<source>(4):        delegate `() => a` closes over variable `a`
    enum yes = __traits(compiles, { return a; });
                                  ^
<source>(1):        `a` declared here
auto test(int a) @nogc
              ^

... but if I comment out either line (1) or line (2) it works fine. It also seems to be important that both foo and a closure in __trait(compiles) close over the same variable.

It's probably expected, but also worth mentioning that it's not just static analysis, codegen is also affected. If I remove @nogc from test declaration, the code compiles, but it does call _d_allocmemory, while commenting either line (1) or line (2) makes it work without memory allocation.

The example in godbolt: https://godbolt.org/z/Wzaq1Tzo7

Kudos to @EyalIO for spotting the bug and to @ljmf00 for making a minimal example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant