roc icon indicating copy to clipboard operation
roc copied to clipboard

Compiler panic when scrutinizing non-`Bool` in `if ... then ... else ...`

Open evanrelf opened this issue 1 year ago • 4 comments

I built Roc from source with Nix at db94b555ab1946ad6bfdb5314a11ba67d9b1d1da.

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
}

import pf.Stdout
import pf.Task exposing [Task]

main : Task {} _
main =
    if "non bool thing here" then
        Stdout.line! "True"
    else
        Stdout.line! "False"
$ roc build main.roc

── TYPE MISMATCH in main.roc ───────────────────────────────────────────────────

This if condition needs to be a Bool:

10│      if "non bool thing here" then
            ^^^^^^^^^^^^^^^^^^^^^

Right now it’s a string of type:

    Str

But I need every if condition to evaluate to a Bool—either Bool.true
or Bool.false.

────────────────────────────────────────────────────────────────────────────────

thread 'main' panicked at crates/compiler/gen_llvm/src/llvm/build.rs:5764:19:
Error in alias analysis: error in module ModName("UserApp"), function definition FuncName("mainForHost"), definition of value binding ValueId(4): could not find func in module ModName("UserApp") with name FuncName("#\x00\x00\x00\x00\x00\x00\x00z\x87\xceV\x11\xaaYs")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

evanrelf avatar Jun 11 '24 02:06 evanrelf

It looks like expected behavior. Roc doesn't cast anything implicitly, so Str is an invalid type for the if predicate, which expects only Bool.

Maybe there’s another problem I missed?

kdziamura avatar Jun 26 '24 16:06 kdziamura

The friendly human error is great, but it's followed by a compiler panic:

thread 'main' panicked at crates/compiler/gen_llvm/src/llvm/build.rs:5764:19:
Error in alias analysis: error in module ModName("UserApp"), function definition FuncName("mainForHost"), definition of value binding ValueId(4): could not find func in module ModName("UserApp") with name FuncName("#\x00\x00\x00\x00\x00\x00\x00z\x87\xceV\x11\xaaYs")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

You might have missed it in the second code block; I think GitHub has it below the fold, so you have to scroll.

evanrelf avatar Jun 26 '24 17:06 evanrelf

Thank you for the report and clarification!

kdziamura avatar Jun 26 '24 18:06 kdziamura

Minimal repro:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
}

main = 42

Zulip thread

similar issues:

https://github.com/roc-lang/roc/issues/5156

kdziamura avatar Jun 27 '24 08:06 kdziamura