Skip to content

Commit 16bd757

Browse files
ntreldlang-bot
authored andcommitted
[spec/function] Add example for ref return void free function
Group paragraphs in a panel. Tweak comments of struct method example.
1 parent d5355ad commit 16bd757

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

spec/function.dd

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,11 +1758,32 @@ int* mars(return ref int i) @safe
17581758
---
17591759
)
17601760

1761-
$(P If a function returns `void`, and the first parameter is `ref` or `out`, then
1761+
$(PANEL
1762+
If a function returns `void`, and the first parameter is `ref` or `out`, then
17621763
all subsequent `return ref` parameters are considered as being assigned to
17631764
the first parameter for lifetime checking.
1765+
1766+
---
1767+
void f(ref scope int* p, return ref int i) @safe
1768+
{
1769+
p = &i; // OK with -preview=dip1000
1770+
}
1771+
1772+
void main() @safe
1773+
{
1774+
int i;
1775+
int* p;
1776+
f(p, i); // OK, lifetime of p is shorter than i
1777+
*p = 5;
1778+
assert(i == 5);
1779+
1780+
int j;
1781+
//f(p, j); // error, lifetime of p is longer than j
1782+
}
1783+
---
1784+
17641785
The `this` reference parameter to a struct non-static member function is
1765-
considered the first parameter.)
1786+
considered the first parameter.
17661787

17671788
---
17681789
struct S
@@ -1771,19 +1792,20 @@ struct S
17711792

17721793
void f(return ref int i) scope @safe
17731794
{
1774-
p = &i;
1795+
p = &i; // OK with -preview=dip1000
17751796
}
17761797
}
17771798

17781799
void main() @safe
17791800
{
17801801
int i;
17811802
S s;
1782-
s.f(i); // OK with -preview=dip1000, lifetime of `s` is shorter than `i`
1803+
s.f(i); // OK, lifetime of `s` is shorter than `i`
17831804
*s.p = 2;
17841805
assert(i == 2);
17851806
}
17861807
---
1808+
)
17871809

17881810
$(P If there are multiple `return ref` parameters, the lifetime of the return
17891811
value is the smallest lifetime of the corresponding arguments.)

0 commit comments

Comments
 (0)