Skip to content

Commit 16df53d

Browse files
committed
Move scope parameter row to its own section
Also split return scope example. Reparent `return scope` section headings. Remove note about -dip1000 in return scope section as it is now mentioned in the (earlier) scope parameter section.
1 parent 90b2561 commit 16df53d

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

spec/function.dd

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,14 +1246,6 @@ int foo(in int x, out int y, ref int z, int q);
12461246
The parameter must not escape the function call
12471247
(e.g. by being assigned to a global variable).
12481248
Ignored for any parameter that is not a reference type.
1249-
`scope` escape analysis is only done for `@safe` functions. For other functions `scope`
1250-
semantics must be manually enforced.)
1251-
$(P $(B Note): `scope` escape analysis is currently only done by `dmd`
1252-
when the `-dip1000` switch is passed.)
1253-
$(P As the parameter must not escape, the compiler can potentially avoid heap-allocation of a
1254-
unique argument to a `scope` parameter. Due to this, passing an array literal, delegate
1255-
literal or a $(GLINK2 expression, NewExpression) to a scope parameter may be used in a
1256-
`@nogc` context, depending on the compiler implementation.)
12571249
))
12581250
$(TROW $(D return), $(ARGS Parameter may be returned or copied to the first parameter,
12591251
but otherwise does not escape from the function.
@@ -1545,35 +1537,60 @@ inout(int)* neptune(inout ref int i)
15451537
}
15461538
---
15471539

1548-
$(H3 $(LNAME2 return-scope-parameters, Return Scope Parameters))
1540+
$(H3 $(LNAME2 scope-parameters, Scope Parameters))
15491541

1550-
$(P Parameters marked as `return scope` that contain indirections
1551-
can only escape those indirections via the function's return value.)
1542+
$(P A `scope` parameter of reference type must not escape the function call
1543+
(e.g. by being assigned to a global variable). It has no effect for non-reference types.
1544+
`scope` escape analysis is only done for `@safe` functions. For other functions `scope`
1545+
semantics must be manually enforced.)
1546+
$(P $(B Note): `scope` escape analysis is currently only done by `dmd`
1547+
when the `-dip1000` switch is passed.)
15521548

15531549
---
15541550
@safe:
15551551

15561552
int* gp;
15571553
void thorin(scope int*);
15581554
void gloin(int*);
1559-
int* balin(return scope int* p, scope int* q, int* r)
1555+
int* balin(scope int* q, int* r)
15601556
{
1561-
gp = p; // error, p escapes to global gp
15621557
gp = q; // error, q escapes to global gp
15631558
gp = r; // ok
15641559

1565-
thorin(p); // ok, p does not escape thorin()
1566-
thorin(q); // ok
1560+
thorin(q); // ok, q does not escape thorin()
15671561
thorin(r); // ok
15681562

1569-
gloin(p); // error, gloin() escapes p
15701563
gloin(q); // error, gloin() escapes q
15711564
gloin(r); // ok that gloin() escapes r
15721565

1573-
return p; // ok
15741566
return q; // error, cannot return 'scope' q
15751567
return r; // ok
15761568
}
1569+
---
1570+
1571+
$(P As a `scope` parameter must not escape, the compiler can potentially avoid heap-allocating a
1572+
unique argument to a `scope` parameter. Due to this, passing an array literal, delegate
1573+
literal or a $(GLINK2 expression, NewExpression) to a scope parameter may be allowed in a
1574+
`@nogc` context, depending on the compiler implementation.)
1575+
1576+
$(H4 $(LNAME2 return-scope-parameters, Return Scope Parameters))
1577+
1578+
$(P Parameters marked as `return scope` that contain indirections
1579+
can only escape those indirections via the function's return value.)
1580+
1581+
---
1582+
@safe:
1583+
1584+
int* gp;
1585+
void thorin(scope int*);
1586+
void gloin(int*);
1587+
int* balin(return scope int* p)
1588+
{
1589+
gp = p; // error, p escapes to global gp
1590+
thorin(p); // ok, p does not escape thorin()
1591+
gloin(p); // error, gloin() escapes p
1592+
return p; // ok
1593+
}
15771594
---
15781595

15791596
$(P Class references are considered pointers that are subject to `scope`.)
@@ -1617,10 +1634,7 @@ class C
16171634
$(P Template functions, auto functions, nested functions and lambdas can deduce
16181635
the `return scope` attribute.)
16191636

1620-
$(P $(B Note:) Checks for `scope` parameters are currently enabled
1621-
only for $(D @safe) functions when compiled with the $(D -dip1000) flag.)
1622-
1623-
$(H3 $(LNAME2 ref-return-scope-parameters, Ref Return Scope Parameters))
1637+
$(H4 $(LNAME2 ref-return-scope-parameters, Ref Return Scope Parameters))
16241638

16251639
$(P Parameters marked as `ref return scope` come in two forms:)
16261640

0 commit comments

Comments
 (0)