Skip to content

Commit 28dff1b

Browse files
authored
Merge pull request #2536 from WalterBright/return-scope
add Return Scope Parameters section
2 parents 1b608bf + 11d3f89 commit 28dff1b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

spec/function.dd

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,6 +1455,74 @@ inout(int)* neptune(inout ref int i)
14551455
}
14561456
---
14571457

1458+
$(H3 $(LNAME2 return-scope-parameters, Return Scope Parameters))
1459+
1460+
$(P Parameters marked as `return scope` that contain indirections
1461+
can only escape those indirections via the function's return value.)
1462+
1463+
---
1464+
int* gp;
1465+
void thorin(scope int*);
1466+
void gloin(int*);
1467+
int* balin(return scope int* p, scope int* q, int* r)
1468+
{
1469+
gp = p; // error, p escapes to global gp
1470+
gp = q; // error, q escapes to global gp
1471+
gp = r; // ok
1472+
1473+
thorin(p); // ok, p does not escape thorin()
1474+
thorin(q); // ok
1475+
thorin(r); // ok
1476+
1477+
gloin(p); // error, gloin() escapes p
1478+
gloin(q); // error, gloin() escapes q
1479+
gloin(r); // ok that gloin() escapes r
1480+
1481+
return p; // ok
1482+
return q; // error, cannot return 'scope' q
1483+
return r; // ok
1484+
}
1485+
---
1486+
1487+
$(P Class references are considered pointers that are subject to `scope`.)
1488+
1489+
---
1490+
class C { }
1491+
C gp;
1492+
void thorin(scope C);
1493+
void gloin(C);
1494+
C balin(return scope C p, scope C q, C r)
1495+
{
1496+
gp = p; // error, p escapes to global gp
1497+
gp = q; // error, q escapes to global gp
1498+
gp = r; // ok
1499+
1500+
thorin(p); // ok, p does not escape thorin()
1501+
thorin(q); // ok
1502+
thorin(r); // ok
1503+
1504+
gloin(p); // error, gloin() escapes p
1505+
gloin(q); // error, gloin() escapes q
1506+
gloin(r); // ok that gloin() escapes r
1507+
1508+
return p; // ok
1509+
return q; // error, cannot return 'scope' q
1510+
return r; // ok
1511+
}
1512+
---
1513+
1514+
$(P `return scope` can be applied to the `this` of class and interface member functions.)
1515+
1516+
---
1517+
class C
1518+
{
1519+
C bofur() return scope { return this; }
1520+
}
1521+
---
1522+
1523+
$(P Template functions, auto functions, nested functions and lambdas can deduce
1524+
the `return scope` attribute.)
1525+
14581526

14591527
$(H3 $(LNAME2 udas-parameters, User-Defined Attributes for Parameters))
14601528

0 commit comments

Comments
 (0)