ponylang / ponyc

Pony is an open-source, actor-model, capabilities-secure, high performance programming language
http://www.ponylang.io
BSD 2-Clause "Simplified" License
5.71k stars 415 forks source link

"Code generation failed" when function contains iftype and returns an union type #3770

Open ergl opened 3 years ago

ergl commented 3 years ago

The following code:

actor Main
  new create(env: Env) =>
    test[U8]()

  fun test[T: U8](): (U8 | None) =>
    iftype T <: U8 then
      0
    end

Fails with

Error:
main.pony:5:3: internal failure: code generation failed for method Main_ref_test_U8_val_o
  fun test[T: U8](): (U8 | None) =>
  ^

The actual code generation failure is here:

https://github.com/ponylang/ponyc/blob/a37b32d03b23bb7200a1145cee70eedb3edde9ed/src/libponyc/codegen/genbox.c#L20-L21

In the code above, it is assuming that l_type is a pointer to i8.

ergl commented 3 years ago

Changing the iftype to be iftype T <: None (or any other type different from the one in the constraint) makes the code work.

ergl commented 3 years ago

We discussed this on sync today. @jemc thinks that is probably due to a missing call to gen_assign_cast when generating the iftype. Probably the issue is that the body of the iftype is not boxing 0 correctly, so trying to cast it to (U8 | None) fails because of that.