roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.2k stars 304 forks source link

"Error in alias analysis" panic when mapping a list with folded function compositions #6191

Open Wollantine opened 10 months ago

Wollantine commented 10 months ago

Hi,

This is probably related with #851.

Setup

This is a minimal repro:

identity = \a -> a

compose = \fnA, fnB ->
    \a -> fnB (fnA a)

chain = \fns ->
    List.walk fns identity compose

main =
    values = List.map [1,2,3] (chain [(\a -> a * 2), (\b -> b + 1)])
    Stdout.line "hi"

Using compose instead of chain avoids the error. Not using it on a List.map avoids the error.

Result

thread 'main' panicked at 'Error in alias analysis: error in module ModName("UserApp"), function definition FuncName("\x07\x00\x00\x00\x05\x00\x00\x00G\xba\x93XR\xc7\xa9C"), definition of value binding ValueId(14): expected type '((),)', found type '((), "UserApp"::"Z\x8d\xb3\xec5\xdf\xb7\xf9")'', crates/compiler/gen_llvm/src/llvm/build.rs:5624:19

In REPL

When using the repl, a smaller reproduction also gives a different error:

compose = \fnA, fnB ->
    \a -> fnB (fnA a)

List.map [1,2,3] (compose (\a -> a * 2) (\b -> b + 1))

Gives:

panicked at 'called Option::unwrap() on a None value', crates/compiler/gen_wasm/src/backend.rs:2131:14


Edit:

Calling the function explicitly also solves it, so it might be related to anonymous functions. Here is a minimal fix on the original:

-    values = List.map [1,2,3] (chain [(\a -> a * 2), (\b -> b + 1)])
+    values = List.map [1,2,3] \x -> (chain [(\a -> a * 2), (\b -> b + 1)]) x
Anton-4 commented 9 months ago

For whomever looks at this in the future; this debugging strategy may be helpful.