@@ -1362,8 +1362,8 @@ ref int gun(return ref int x) {
1362
1362
}
1363
1363
---
1364
1364
1365
- $(P Ref methods marked with the `return` attribute ensure the returned
1366
- reference will not outlive the respective aggregate instance.
1365
+ $(P Struct non-static methods marked with the `return` attribute ensure the returned
1366
+ reference will not outlive the struct instance.
1367
1367
)
1368
1368
1369
1369
---
@@ -1380,10 +1380,75 @@ ref int escape()
1380
1380
}
1381
1381
---
1382
1382
1383
- $(P Template functions and lambdas can deduce the `return` attribute.)
1383
+ $(P Returning the address of a `ref` variable is also checked.)
1384
+
1385
+ ---
1386
+ int* pluto(ref int i)
1387
+ {
1388
+ return &i; // error: returning &i escapes a reference to parameter i
1389
+ }
1390
+
1391
+ int* mars(return ref int i)
1392
+ {
1393
+ return &i; // ok
1394
+ }
1395
+ ---
1396
+
1397
+ $(P If there are multiple `return ref` parameters, the lifetime of the return
1398
+ value is the smallest lifetime of the corresponding arguments.)
1399
+
1400
+ $(P Neither the type of the `return ref` parameter(s) nor the type of the return
1401
+ value is considered when determining the lifetime of the return value.)
1402
+
1403
+ $(P It is not an error if the return type does not contain any indirections.)
1404
+
1405
+ ---
1406
+ int mercury(return ref int i)
1407
+ {
1408
+ return i; // ok
1409
+ }
1410
+ ---
1411
+
1412
+ $(P Template functions, auto functions, nested functions and lambdas can deduce the `return` attribute.)
1413
+
1414
+ ---
1415
+ ref int templateFunction()(ref int i)
1416
+ {
1417
+ return i; // ok
1418
+ }
1419
+
1420
+ ref auto autoFunction(ref int i)
1421
+ {
1422
+ return i; // ok
1423
+ }
1424
+
1425
+ void uranus()
1426
+ {
1427
+ ref int nestedFunction(ref int i)
1428
+ {
1429
+ return i; // ok
1430
+ }
1431
+ }
1432
+
1433
+ void venus()
1434
+ {
1435
+ auto lambdaFunction =
1436
+ (ref int i)
1437
+ {
1438
+ return &i; // ok
1439
+ };
1440
+ }
1441
+ ---
1384
1442
1385
1443
$(P `inout ref` parameters imply the `return` attribute.)
1386
1444
1445
+ ---
1446
+ inout(int)* neptune(inout ref int i)
1447
+ {
1448
+ return &i; // ok
1449
+ }
1450
+ ---
1451
+
1387
1452
1388
1453
$(H3 $(LNAME2 udas-parameters, User-Defined Attributes for Parameters))
1389
1454
0 commit comments