~sircmpwn/hare-users

1

casting enum to non-integer causes harec to abort

Details
Message ID
<d65c8dccf8f704e9fa5bf41cbca9e3dc@disroot.org>
DKIM signature
pass
Download raw message
The following program causes harec to abort:

     type badenum = enum {
          FOO, BAR,
     };

     export fn main() void = {
          const bar = badenum::FOO: bool;
     };

With this output:

     $ hare run test.ha
     0/4 tasks completed (0%)
     harec for /home/sewn/src/test/test.ha exited with signal SIGABRT
Details
Message ID
<D46D73297XFZ.2NF8JGBLI23YL@disroot.org>
In-Reply-To
<d65c8dccf8f704e9fa5bf41cbca9e3dc@disroot.org> (view parent)
DKIM signature
pass
Download raw message
On Sat Sep 14, 2024 at 1:06 PM EDT, sewn wrote:
> The following program causes harec to abort:
>
>      type badenum = enum {
>           FOO, BAR,
>      };
>
>      export fn main() void = {
>           const bar = badenum::FOO: bool;
>      };
>
> With this output:
>
>      $ hare run test.ha
>      0/4 tasks completed (0%)
>      harec for /home/sewn/src/test/test.ha exited with signal SIGABRT

I think casting an enum to a bool is disallowed by the language but the
compiler doesn't give an error because of a bug. Casting something like
an int to a bool is caught by the compiler and gives you an error.

Instead, you can do something like this:

  type enum_type = enum {
  	FOO, BAR,
  };

  export fn main() void = {
	const enum_value = enum_type::FOO;
  	const bar = (enum_value != enum_type::FOO);
  };

That also has the added benefit of making it more clear in the code what
you're trying to do.
Reply to thread Export thread (mbox)