@@ -1242,9 +1242,11 @@ int foo(in int x, out int y, ref int z, int q);
1242
1242
$(TROW $(D out), parameter is passed by reference and initialized upon function entry with the default value
1243
1243
for its type)
1244
1244
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 must 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
+ ))
1248
1250
$(TROW $(D return), $(ARGS Parameter may be returned or copied to the first parameter,
1249
1251
but otherwise does not escape from the function.
1250
1252
Such copies are required not to outlive the argument(s) they were derived from.
@@ -1422,10 +1424,6 @@ pure void f()
1422
1424
1423
1425
$(H3 $(LNAME2 return-ref-parameters, Return Ref Parameters))
1424
1426
1425
- $(P Note: The `return` attribute is currently only enforced by `dmd`
1426
- when the `-dip25` switch is passed.
1427
- )
1428
-
1429
1427
$(P Return ref parameters are used with
1430
1428
$(RELATIVE_LINK2 ref-functions, ref functions) to ensure that the
1431
1429
returned reference will not outlive the matching argument's lifetime.
@@ -1539,35 +1537,60 @@ inout(int)* neptune(inout ref int i)
1539
1537
}
1540
1538
---
1541
1539
1542
- $(H3 $(LNAME2 return- scope-parameters, Return Scope Parameters))
1540
+ $(H3 $(LNAME2 scope-parameters, Scope Parameters))
1543
1541
1544
- $(P Parameters marked as `return scope` that contain indirections
1545
- 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.)
1546
1548
1547
1549
---
1548
1550
@safe:
1549
1551
1550
1552
int* gp;
1551
1553
void thorin(scope int*);
1552
1554
void gloin(int*);
1553
- int* balin(return scope int* p, scope int* q, int* r)
1555
+ int* balin(scope int* q, int* r)
1554
1556
{
1555
- gp = p; // error, p escapes to global gp
1556
1557
gp = q; // error, q escapes to global gp
1557
1558
gp = r; // ok
1558
1559
1559
- thorin(p); // ok, p does not escape thorin()
1560
- thorin(q); // ok
1560
+ thorin(q); // ok, q does not escape thorin()
1561
1561
thorin(r); // ok
1562
1562
1563
- gloin(p); // error, gloin() escapes p
1564
1563
gloin(q); // error, gloin() escapes q
1565
1564
gloin(r); // ok that gloin() escapes r
1566
1565
1567
- return p; // ok
1568
1566
return q; // error, cannot return 'scope' q
1569
1567
return r; // ok
1570
1568
}
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
+ }
1571
1594
---
1572
1595
1573
1596
$(P Class references are considered pointers that are subject to `scope`.)
@@ -1611,10 +1634,7 @@ class C
1611
1634
$(P Template functions, auto functions, nested functions and lambdas can deduce
1612
1635
the `return scope` attribute.)
1613
1636
1614
- $(P $(B Note:) Checks for `scope` parameters are currently enabled
1615
- only for $(D @safe) functions when compiled with the $(D -dip1000) flag.)
1616
-
1617
- $(H3 $(LNAME2 ref-return-scope-parameters, Ref Return Scope Parameters))
1637
+ $(H4 $(LNAME2 ref-return-scope-parameters, Ref Return Scope Parameters))
1618
1638
1619
1639
$(P Parameters marked as `ref return scope` come in two forms:)
1620
1640
0 commit comments