www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22015] New: sumtype Error: e2ir: cannot cast `...` of type

https://issues.dlang.org/show_bug.cgi?id=22015

          Issue ID: 22015
           Summary: sumtype Error: e2ir: cannot cast `...` of type
                    `noreturn` to type `string`
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: mipri minimaltype.com

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
  }

--
Jun 10