Skip to content

Commit 46fd17f

Browse files
committed
sumtype: fix canMatch for non-copyable ref return
Handlers that return a non-copyable value by reference are now capable of successfully matching. Fixes #10647
1 parent 3a8f31a commit 46fd17f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

std/sumtype.d

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,7 @@ class MatchException : Exception
18301830
template canMatch(alias handler, Ts...)
18311831
if (Ts.length > 0)
18321832
{
1833-
enum canMatch = is(typeof((ref Ts args) => handler(args)));
1833+
enum canMatch = is(typeof(auto ref (ref Ts args) => handler(args)));
18341834
}
18351835

18361836
///
@@ -1855,6 +1855,21 @@ if (Ts.length > 0)
18551855
assert(canMatch!(OverloadSet.fun, double));
18561856
}
18571857

1858+
// Allows returning non-copyable types by ref
1859+
// https://github.com/dlang/phobos/issues/10647
1860+
@safe unittest
1861+
{
1862+
static struct NoCopy
1863+
{
1864+
@disable this(this);
1865+
}
1866+
1867+
static NoCopy lvalue;
1868+
static ref handler(int _) => lvalue;
1869+
1870+
assert(canMatch!(handler, int));
1871+
}
1872+
18581873
// Like aliasSeqOf!(iota(n)), but works in BetterC
18591874
private template Iota(size_t n)
18601875
{

0 commit comments

Comments
 (0)