@@ -1707,21 +1707,22 @@ $(H3 $(LNAME2 return-ref-parameters, Return Ref Parameters))
1707
1707
returned reference will not outlive the matching argument's lifetime.
1708
1708
)
1709
1709
1710
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1710
1711
---
1711
1712
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
1713
1714
}
1714
1715
1715
1716
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
1718
1719
}
1719
1720
1720
1721
ref int gun(return ref int x) {
1721
- return identity(x); // OK
1722
+ return identity(x); // OK
1722
1723
}
1723
1724
---
1724
-
1725
+ )
1725
1726
$(P Struct non-static methods marked with the `return` attribute ensure the returned
1726
1727
reference will not outlive the struct instance.
1727
1728
)
@@ -1740,26 +1741,49 @@ ref int escape()
1740
1741
}
1741
1742
---
1742
1743
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 .)
1744
1745
1746
+ $(SPEC_RUNNABLE_EXAMPLE_FAIL
1745
1747
---
1746
- int* pluto(ref int i)
1748
+ int* pluto(ref int i) @safe
1747
1749
{
1748
1750
return &i; // error: returning &i escapes a reference to parameter i
1749
1751
}
1750
1752
1751
- int* mars(return ref int i)
1753
+ int* mars(return ref int i) @safe
1752
1754
{
1753
- return &i; // ok
1755
+ return &i; // OK with -preview=dip1000
1754
1756
}
1755
1757
---
1758
+ )
1756
1759
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
1758
1761
all subsequent `return ref` parameters are considered as being assigned to
1759
1762
the first parameter for lifetime checking.
1760
1763
The `this` reference parameter to a struct non-static member function is
1761
1764
considered the first parameter.)
1762
1765
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
+
1763
1787
$(P If there are multiple `return ref` parameters, the lifetime of the return
1764
1788
value is the smallest lifetime of the corresponding arguments.)
1765
1789
@@ -1778,7 +1802,10 @@ int mercury(return ref int i)
1778
1802
$(P Template functions, auto functions, nested functions and $(DDSUBLINK spec/expression, function_literals, lambdas)
1779
1803
can deduce the `return` attribute.)
1780
1804
1805
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
1781
1806
---
1807
+ @safe:
1808
+
1782
1809
ref int templateFunction()(ref int i)
1783
1810
{
1784
1811
return i; // ok
@@ -1795,17 +1822,14 @@ void uranus()
1795
1822
{
1796
1823
return i; // ok
1797
1824
}
1798
- }
1799
-
1800
- void venus()
1801
- {
1802
1825
auto lambdaFunction =
1803
1826
(ref int i)
1804
1827
{
1805
1828
return &i; // ok
1806
1829
};
1807
1830
}
1808
1831
---
1832
+ )
1809
1833
1810
1834
$(H3 $(LNAME2 scope-parameters, Scope Parameters))
1811
1835
0 commit comments