roc icon indicating copy to clipboard operation
roc copied to clipboard

Nested question mark causes compiler crash

Open isaacvando opened this issue 1 year ago • 2 comments

This code causes the compiler to crash with the following error on roc check or roc test.

module []

isBomb : List Str, I64, I64 -> Result Bool [OutOfBounds]
isBomb = \rows, nx, ny ->
    rows
    |> List.get? (Num.toU64Checked? ny)
    |> Str.toUtf8
    |> List.get? (Num.toU64Checked? nx)
    |> Bool.isEq '*'
    |> Ok
$ roc check .meta/Example.roc
An internal compiler expectation was broken.
This is definitely a compiler bug.
Please file an issue here: <https://github.com/roc-lang/roc/issues/new/choose>
a Expr::TrySuffix expression was not completely removed in desugar_value_def_suffixed
Location: crates/compiler/can/src/expr.rs:1125:40

Extracting the nested ? expressions prevents the crash:

isBomb : List Str, I64, I64 -> Result Bool [OutOfBounds]
isBomb = \rows, nx, ny ->
    y = Num.toU64Checked? ny
    x = Num.toU64Checked? nx
    rows
    |> List.get? y
    |> Str.toUtf8
    |> List.get? x
    |> Bool.isEq '*'
    |> Ok

isaacvando avatar Sep 02 '24 04:09 isaacvando

I think the compiler panics when there's more than one ? in an expression.

ageron avatar Sep 02 '24 08:09 ageron

It's weird because assigning then returning does work:

isBomb : List Str, I64, I64 -> Result Bool [OutOfBounds]
isBomb = \rows, nx, ny ->
    result = rows
        |> List.get? (Num.toU64Checked? ny)
        |> Str.toUtf8
        |> List.get? (Num.toU64Checked? nx)
        |> Bool.isEq '*'
        |> Ok

    result

kuglee avatar Oct 15 '24 08:10 kuglee