D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20050 - pure function should be able to return function pointer to impure static nested function
Summary: pure function should be able to return function pointer to impure static nest...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2019-07-14 11:26 UTC by ag0aep6g
Modified: 2019-07-15 13:14 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 ag0aep6g 2019-07-14 11:26:42 UTC
Found while looking into issue 20047.

Should compile:

----
int g = 0;
void impure_function_1() { ++g; }
void function() get_impure_function() pure
{
    static void impure_function_2()
    {
        ++g; /* Error: pure function test.get_impure_function cannot access mutable static data g */
        impure_function_1(); /* Error: pure function test.get_impure_function cannot call impure function test.impure_function_1 */
    }
    return &impure_function_2;
}
void main()
{
    auto f = get_impure_function();
    f();
    assert(g == 2);
}
----

Just because `get_impure_function` is pure doesn't mean that `impure_function_2` also has to be pure.
Comment 1 Dlang Bot 2019-07-14 12:41:53 UTC
@aG0aep6G created dlang/dmd pull request #10172 "fix handling of static impure functions nested in pure functions (issues 20047 and 20050)" fixing this issue:

- fix issue 20050 - pure function should be able to return function pointer to impure static nested function

https://github.com/dlang/dmd/pull/10172
Comment 2 Dlang Bot 2019-07-15 13:14:58 UTC
dlang/dmd pull request #10172 "fix handling of static impure functions nested in pure functions (issues 20047 and 20050)" was merged into master:

- e50097d7ca361a1d6ee5841459781aaf26f77466 by aG0aep6G:
  fix issue 20050 - pure function should be able to return function pointer to impure static nested function

https://github.com/dlang/dmd/pull/10172