Skip to content

Commit 3b497da

Browse files
authored
Merge pull request #2534 from WalterBright/more-return-ref
add missing information to return-ref documentation
2 parents 84e0711 + 4a72e73 commit 3b497da

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

spec/function.dd

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,8 +1362,8 @@ ref int gun(return ref int x) {
13621362
}
13631363
---
13641364

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.
13671367
)
13681368

13691369
---
@@ -1380,10 +1380,75 @@ ref int escape()
13801380
}
13811381
---
13821382

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+
---
13841442

13851443
$(P `inout ref` parameters imply the `return` attribute.)
13861444

1445+
---
1446+
inout(int)* neptune(inout ref int i)
1447+
{
1448+
return &i; // ok
1449+
}
1450+
---
1451+
13871452

13881453
$(H3 $(LNAME2 udas-parameters, User-Defined Attributes for Parameters))
13891454

0 commit comments

Comments
 (0)