@@ -1758,11 +1758,32 @@ int* mars(return ref int i) @safe
1758
1758
---
1759
1759
)
1760
1760
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
1762
1763
all subsequent `return ref` parameters are considered as being assigned to
1763
1764
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
+
1764
1785
The `this` reference parameter to a struct non-static member function is
1765
- considered the first parameter.)
1786
+ considered the first parameter.
1766
1787
1767
1788
---
1768
1789
struct S
@@ -1771,19 +1792,20 @@ struct S
1771
1792
1772
1793
void f(return ref int i) scope @safe
1773
1794
{
1774
- p = &i;
1795
+ p = &i; // OK with -preview=dip1000
1775
1796
}
1776
1797
}
1777
1798
1778
1799
void main() @safe
1779
1800
{
1780
1801
int i;
1781
1802
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`
1783
1804
*s.p = 2;
1784
1805
assert(i == 2);
1785
1806
}
1786
1807
---
1808
+ )
1787
1809
1788
1810
$(P If there are multiple `return ref` parameters, the lifetime of the return
1789
1811
value is the smallest lifetime of the corresponding arguments.)
0 commit comments