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

template function inference breaks when taking the address of the function inside the template #19575

Open
dlangBugzillaToGithub opened this issue Jun 2, 2019 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

Jonathan Marler (@marler8997) reported this on 2019-06-02T05:31:50Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=19934

Description

Reproduce issue with:
```
void func()()
{
    pragma(msg, typeof(&func!()).stringof);
}
void main() @nogc
{
    auto funcPtr = &func!();
    funcPtr();
}

```

This fails with:
 Error: `@nogc` function `D main` cannot call non-@nogc function pointer `funcPtr`


Normally `func` would be @nogc here, but when DMD is forced to resolve typeof(&func!()) while analyzing func, it causes an issue template inference.

Note that this issue occurs for all function attributes such as `pure` and `nothrow`.

Note that any of the following modifications will fix the issue:

1. remove the pragma, if dmd doesn't have to resolve typeof(&func) inside of func then attribute inference still works correctly

2. don't use auto for funcPtr, i.e.

    void main() @nogc
    {
        void function() @nogc funcPtr = &func!();
        funcPtr();
    }

3. call the function pointer without assigning it to a variable:

    void main() @nogc
    {
        (&func!())();
    }

3. call func directly instead of through a function pointer:

    void main() @nogc
    {
        func!()();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant