Bug 22384 – castSwitch confused by noreturn handlers
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-10-13T15:28:25Z
Last change time
2021-10-14T08:49:34Z
Keywords
pull
Assigned to
No Owner
Creator
moonlightsentinel
Comments
Comment #0 by moonlightsentinel — 2021-10-13T15:28:25Z
castSwitch allows handlers that neither return a value nor throw an exception - as long as no other handler returns a value. This breaks when passing noreturn handlers, causing castSwitch to throw an exception for the void handlers that don't trow an exception and hence being inferred as noreturn.
Example
void main()
{ static void objectSkip(Object) {}
static void defaultSkip() {}
static noreturn objectError(Object) { assert(false); }
static noreturn defaultError() { assert(false); }
{
alias test = castSwitch!(objectSkip, defaultError);
static assert(is(ReturnType!test == void)); // fails, noreturn
}{
alias test = castSwitch!(objectError, defaultSkip);
static assert(is(ReturnType!test == void)); // fails, noreturn
}{
alias test = castSwitch!(objectError, defaultError);
static assert(is(ReturnType!test == noreturn)); // valid
}
}
Comment #1 by dlang-bot — 2021-10-13T15:35:48Z
@MoonlightSentinel created dlang/phobos pull request #8270 "Fix 22384 - Treat noreturn handlers like void in castSwitch" fixing this issue:
- Fix 22384 - Treat noreturn handlers like void in castSwitch
The previous check assumed that non-`void` return implies a returned
value - which obviously does not apply for `noreturn`.
Further code then assumed that `void` handlers should throw an exception
and hence threw an error when it didn't throw.
https://github.com/dlang/phobos/pull/8270
Comment #2 by dlang-bot — 2021-10-14T08:49:34Z
dlang/phobos pull request #8270 "Fix 22384 - Treat noreturn handlers like void in castSwitch" was merged into master:
- 3e70aac3ae35a26ec837ce5362711ed3bfece2ae by MoonlightSentinel:
Fix 22384 - Treat noreturn handlers like void in castSwitch
The previous check assumed that non-`void` return implies a returned
value - which obviously does not apply for `noreturn`.
Further code then assumed that `void` handlers should throw an exception
and hence threw an error when it didn't throw.
https://github.com/dlang/phobos/pull/8270