From 40daf618514120be780cd54891be5c44dd1a155f Mon Sep 17 00:00:00 2001 From: Paul Backus Date: Sun, 23 Feb 2025 17:12:00 -0500 Subject: [PATCH] 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 --- std/sumtype.d | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/std/sumtype.d b/std/sumtype.d index 84b8362fc..5068da6de 100644 --- a/std/sumtype.d +++ b/std/sumtype.d @@ -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) {