Skip to content

Commit 4f84311

Browse files
committed
[spec] Add scope parameter caveats
Mention unique allocation elision.
1 parent 9c3e74d commit 4f84311

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

spec/function.dd

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,9 +1242,18 @@ int foo(in int x, out int y, ref int z, int q);
12421242
$(TROW $(D out), parameter is passed by reference and initialized upon function entry with the default value
12431243
for its type)
12441244

1245-
$(TROW $(D scope), references in the parameter
1246-
cannot be escaped (e.g. assigned to a global variable).
1247-
Ignored for parameters with no references)
1245+
$(TROW $(D scope), $(ARGS
1246+
The parameter does not escape the function call
1247+
(e.g. by being assigned to a global variable).
1248+
Ignored for any parameter that is not a reference type.
1249+
Escape analysis is only done for `@safe` functions, for other functions `scope` semantics must
1250+
be manually enforced.
1251+
As the parameter must not escape, the compiler can avoid heap-allocation of a unique argument
1252+
to a `scope` parameter. Due to this, passing e.g. a `new T` argument may not violate `@nogc`.
1253+
A typical example of this is passing a delegate literal to a `scope` parameter.
1254+
$(B Note): `scope` escape analysis is currently only done by `dmd`
1255+
when the `-dip1000` switch is passed.
1256+
))
12481257
$(TROW $(D return), $(ARGS Parameter may be returned or copied to the first parameter,
12491258
but otherwise does not escape from the function.
12501259
Such copies are required not to outlive the argument(s) they were derived from.
@@ -1422,10 +1431,6 @@ pure void f()
14221431

14231432
$(H3 $(LNAME2 return-ref-parameters, Return Ref Parameters))
14241433

1425-
$(P Note: The `return` attribute is currently only enforced by `dmd`
1426-
when the `-dip25` switch is passed.
1427-
)
1428-
14291434
$(P Return ref parameters are used with
14301435
$(RELATIVE_LINK2 ref-functions, ref functions) to ensure that the
14311436
returned reference will not outlive the matching argument's lifetime.

0 commit comments

Comments
 (0)