Skip to content

Commit cf59f9a

Browse files
ntreldkorpel
andauthored
Improve return ref examples (#3630)
* Improve return ref examples Make some examples runnable. Mention @safe needed to check returning address of `return ref` parameter. Note -dip1000 is *still* needed to return the address of a `return ref` parameter. The example compiler is currently 2.102, so one example can't be made runnable yet. (The mars function errors with it but pluto always errors anyway so _FAIL is OK). * Apply suggestions from code review Co-authored-by: Dennis <[email protected]> --------- Co-authored-by: Dennis <[email protected]>
1 parent bbda2a2 commit cf59f9a

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

spec/function.dd

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,21 +1707,22 @@ $(H3 $(LNAME2 return-ref-parameters, Return Ref Parameters))
17071707
returned reference will not outlive the matching argument's lifetime.
17081708
)
17091709

1710+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
17101711
---
17111712
ref int identity(return ref int x) {
1712-
return x; // pass-through function that does nothing
1713+
return x; // pass-through function that does nothing
17131714
}
17141715

17151716
ref int fun() {
1716-
int x;
1717-
return identity(x); // Error: escaping reference to local variable x
1717+
int x;
1718+
return identity(x); // Error: escaping reference to local variable x
17181719
}
17191720

17201721
ref int gun(return ref int x) {
1721-
return identity(x); // OK
1722+
return identity(x); // OK
17221723
}
17231724
---
1724-
1725+
)
17251726
$(P Struct non-static methods marked with the `return` attribute ensure the returned
17261727
reference will not outlive the struct instance.
17271728
)
@@ -1740,26 +1741,49 @@ ref int escape()
17401741
}
17411742
---
17421743

1743-
$(P Returning the address of a `ref` variable is also checked.)
1744+
$(P Returning the address of a `ref` variable is also checked in `@safe` code.)
17441745

1746+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
17451747
---
1746-
int* pluto(ref int i)
1748+
int* pluto(ref int i) @safe
17471749
{
17481750
return &i; // error: returning &i escapes a reference to parameter i
17491751
}
17501752

1751-
int* mars(return ref int i)
1753+
int* mars(return ref int i) @safe
17521754
{
1753-
return &i; // ok
1755+
return &i; // OK with -preview=dip1000
17541756
}
17551757
---
1758+
)
17561759

1757-
$(P If the function returns `void`, and the first parameter is `ref` or `out`, then
1760+
$(P If a function returns `void`, and the first parameter is `ref` or `out`, then
17581761
all subsequent `return ref` parameters are considered as being assigned to
17591762
the first parameter for lifetime checking.
17601763
The `this` reference parameter to a struct non-static member function is
17611764
considered the first parameter.)
17621765

1766+
---
1767+
struct S
1768+
{
1769+
private int* p;
1770+
1771+
void f(return ref int i) scope @safe
1772+
{
1773+
p = &i;
1774+
}
1775+
}
1776+
1777+
void main() @safe
1778+
{
1779+
int i;
1780+
S s;
1781+
s.f(i); // OK with -preview=dip1000, lifetime of `s` is shorter than `i`
1782+
*s.p = 2;
1783+
assert(i == 2);
1784+
}
1785+
---
1786+
17631787
$(P If there are multiple `return ref` parameters, the lifetime of the return
17641788
value is the smallest lifetime of the corresponding arguments.)
17651789

@@ -1778,7 +1802,10 @@ int mercury(return ref int i)
17781802
$(P Template functions, auto functions, nested functions and $(DDSUBLINK spec/expression, function_literals, lambdas)
17791803
can deduce the `return` attribute.)
17801804

1805+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
17811806
---
1807+
@safe:
1808+
17821809
ref int templateFunction()(ref int i)
17831810
{
17841811
return i; // ok
@@ -1795,17 +1822,14 @@ void uranus()
17951822
{
17961823
return i; // ok
17971824
}
1798-
}
1799-
1800-
void venus()
1801-
{
18021825
auto lambdaFunction =
18031826
(ref int i)
18041827
{
18051828
return &i; // ok
18061829
};
18071830
}
18081831
---
1832+
)
18091833

18101834
$(H3 $(LNAME2 scope-parameters, Scope Parameters))
18111835

0 commit comments

Comments
 (0)