Skip to content

Commit b8a8a94

Browse files
ntreldlang-bot
authored andcommitted
[spec] Fix closure allocation docs
1 parent d8429c2 commit b8a8a94

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

spec/function.dd

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,8 +3086,38 @@ void main()
30863086
)
30873087

30883088
$(P Those referenced stack variables that make up the closure
3089-
are allocated on the GC heap. Closures are not allowed for
3090-
`@nogc` functions.)
3089+
are allocated on the GC heap, unless:)
3090+
3091+
* The closure is passed to a `scope` parameter.
3092+
* The closure is an initializer for a `scope` variable.
3093+
* The closure is assigned to a `scope` variable.
3094+
3095+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
3096+
---
3097+
@nogc:
3098+
void f(scope int delegate());
3099+
void g(int delegate());
3100+
3101+
void main()
3102+
{
3103+
int i;
3104+
int h() { return i; }
3105+
h(); // OK
3106+
scope x = &h; // OK
3107+
x(); // OK
3108+
//auto y = &h; // error, can't allocate closure in @nogc function
3109+
f(&h); // OK
3110+
//g(&h); // error
3111+
3112+
// delegate literals
3113+
f(() => i); // OK
3114+
scope d = () => i; // OK
3115+
d = () => i + 1; // OK
3116+
f(d);
3117+
//g(() => i); // error, can't allocate closure in @nogc function
3118+
}
3119+
---
3120+
)
30913121

30923122
$(NOTE Returning addresses of stack variables, however, is not
30933123
a closure and is an error.
@@ -3550,7 +3580,8 @@ $(H2 $(LNAME2 nogc-functions, No-GC Functions))
35503580
void bar() { }
35513581
---
35523582

3553-
$(P No-GC functions cannot be closures.
3583+
$(P No-GC functions can only use a closure if it is `scope` -
3584+
see $(RELATIVE_LINK2 closures, Delegates & Closures).
35543585
)
35553586

35563587
---

0 commit comments

Comments
 (0)