File tree Expand file tree Collapse file tree 1 file changed +34
-3
lines changed Expand file tree Collapse file tree 1 file changed +34
-3
lines changed Original file line number Diff line number Diff line change @@ -3086,8 +3086,38 @@ void main()
3086
3086
)
3087
3087
3088
3088
$(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
+ )
3091
3121
3092
3122
$(NOTE Returning addresses of stack variables, however, is not
3093
3123
a closure and is an error.
@@ -3550,7 +3580,8 @@ $(H2 $(LNAME2 nogc-functions, No-GC Functions))
3550
3580
void bar() { }
3551
3581
---
3552
3582
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).
3554
3585
)
3555
3586
3556
3587
---
You can’t perform that action at this time.
0 commit comments