Bug 22015 – sumtype Error: e2ir: cannot cast `...` of type `noreturn` to type `string`

Status
RESOLVED
Resolution
DUPLICATE
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-06-10T23:30:32Z
Last change time
2021-06-21T20:38:24Z
Keywords
backend
Assigned to
No Owner
Creator
mipri

Comments

Comment #0 by mipri — 2021-06-10T23:30:32Z
The following complete program: import std.sumtype; alias Result = SumType!(string, int); string unwrap(Result r) { return r.match!( (string s) => s, (int _) => assert(0), ); } fails to compile with this internal(?) error: /home/jfondren/dlang/dmd2/linux/bin64/../../src/phobos/std/sumtype.d-mixin-1989(1989): Error: e2ir: cannot cast `(*function (int _) pure nothrow @nogc @safe => assert(0))(_param_0.get())` of type `noreturn` to type `string` under DMD64 D Compiler v2.097.0 A workaround: import std.sumtype; import std.exception : enforce; import std.stdio : writeln; alias Result = SumType!(string, int); string unwrap(Result r) { string result; bool failed; r.match!( (string s) { result = s; }, (int _) { failed = true; }, ); if (failed) assert(0); return result; } void main() { Result("ok").unwrap.writeln; Result(0).unwrap.writeln; // assertion failure }
Comment #1 by jlourenco5691 — 2021-06-11T01:30:42Z
A better workaround is: ```d string unwrap(Result r) { return r.match!( (string s) => s, function string (int) { assert(0); }, ); } ```
Comment #2 by moonlightsentinel — 2021-06-11T05:48:10Z
Probably fixed in stable by https://github.com/dlang/dmd/pull/12633
Comment #3 by moonlightsentinel — 2021-06-21T20:38:24Z
*** This issue has been marked as a duplicate of issue 21955 ***