@@ -1455,6 +1455,74 @@ inout(int)* neptune(inout ref int i)
1455
1455
}
1456
1456
---
1457
1457
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
+
1458
1526
1459
1527
$(H3 $(LNAME2 udas-parameters, User-Defined Attributes for Parameters))
1460
1528
0 commit comments