Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6963 from JohanEngelen/closurenonstatic
Fix Issue 17590 - Unnecessary GC alloc on returning static local struct merged-on-behalf-of: Sebastian Wilzbach <sebi.wilzbach@gmail.com>
- Loading branch information
Showing
2 changed files
with
44 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// REQUIRED_ARGS: -o- | ||
|
||
void lazyfun(scope lazy int a) @nogc; | ||
|
||
// Test that returning a local _static_ struct does not lead to allocation of a closure. | ||
auto foo_static(int a, bool b) @nogc { | ||
static struct SInside {} | ||
|
||
SInside res; | ||
|
||
lazyfun(a); | ||
|
||
return res; | ||
} | ||
|
||
// Test that returning a local _non-static_ struct that does not reference any local variable does not lead to allocation of a closure. | ||
auto foo_nonstatic(int a, bool b) @nogc { | ||
struct SInside {} | ||
|
||
SInside res; | ||
|
||
lazyfun(a); | ||
|
||
return res; | ||
} | ||
|
||
// Test that returning a local non-static struct that references a local variable does lead to allocation of a closure. | ||
static assert(!__traits(compiles, () @nogc => goo(1))); | ||
static assert(__traits(compiles, () => goo(1))); | ||
auto goo(T)(T a) { | ||
struct SInside { | ||
T foo() { return a; } | ||
} | ||
|
||
SInside res; | ||
|
||
lazyfun(a); | ||
|
||
return res; | ||
} |