roc icon indicating copy to clipboard operation
roc copied to clipboard

Compiler error when destructuring

Open ghigt opened this issue 2 years ago • 1 comments

I cannot understand why this produces an error:

{ card, num } = { card: [], num: 1 }
List.map card \n ->
    when n is
        Not v if v == num -> Found v
        _ -> n
thread 'main' panicked at 'Error in alias analysis: error in module ModName("UserApp"), function definition FuncName("\x00\x00\x00\x00\x0f\x00\x00\x00\x1b3\xaaX\xc0\x0bk\x7f"), definition of value binding ValueId(11): expected type '((heap_cell, bag<union { ((),), ((),) }>), ())', found type '((heap_cell, bag<()>), ())'', crates/compiler/gen_llvm/src/llvm/build.rs:4504:23

while this don't (keep tags, no destructuring):

card = []
num = 1
List.map card \n ->
    when n is
        Not v if v == num -> Found v
        _ -> n

neither this (removing tags, still destructuring):

{ card, num } = { card: [], num: 1 }
List.map card \n ->
    when n is
        v if v == num -> v
        _ -> n

ghigt avatar Oct 04 '22 21:10 ghigt

This is another case of polymorphic expressions not being compiled correctly. A smaller reproducer is

{ tags } = { tags: [A] }
List.map tags \t ->
  when t is
    A -> A
    B -> B

Right now we don't raise the polymorphic demand that tags : [A, B] correctly, whereas we do for direct assignments.

Unfortunately destructures are a bit more complicated than direct assignments because we must multiplex over compilation of each polymorphic destructure case (that is, in the OP, if card and num were both used polymorphically with 2 different types each, we'd need to compile 4 destructuring cases if we did it naively; done intelligently, we can arrange a plan that compiles only 2).

All that is to say that it's known how to fix this, but we should take care before doing so. The optimal solution will require us to build a plan of what destructuring layouts to compile, so let's design that. There have been a few of these polymorphic expression compilation holes, so might be good to go at them again in the near future.

ayazhafiz avatar Oct 05 '22 16:10 ayazhafiz

No longer reproduces

JRI98 avatar Dec 14 '23 14:12 JRI98