sumtype: fix canMatch for non-copyable ref return (#10648)

Handlers that return a non-copyable value by reference are now capable
of successfully matching.

Fixes #10647
This commit is contained in:
Paul Backus 2025-02-23 17:12:00 -05:00 committed by GitHub
parent 3a8f31acbd
commit 40daf61851
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1830,7 +1830,7 @@ class MatchException : Exception
template canMatch(alias handler, Ts...)
if (Ts.length > 0)
{
enum canMatch = is(typeof((ref Ts args) => handler(args)));
enum canMatch = is(typeof(auto ref (ref Ts args) => handler(args)));
}
///
@ -1855,6 +1855,21 @@ if (Ts.length > 0)
assert(canMatch!(OverloadSet.fun, double));
}
// Allows returning non-copyable types by ref
// https://github.com/dlang/phobos/issues/10647
@safe unittest
{
static struct NoCopy
{
@disable this(this);
}
static NoCopy lvalue;
static ref handler(int _) => lvalue;
assert(canMatch!(handler, int));
}
// Like aliasSeqOf!(iota(n)), but works in BetterC
private template Iota(size_t n)
{