roc-lang / roc

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

panic on json encode with optional Frac * #6860

Open adomurad opened 3 weeks ago

adomurad commented 3 weeks ago

When trying to run this example I get a panic from the compiler:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.11.0/SY4WWMhWQ9NvQgvIthcv15AUeA7rAIJHAHgiaSHGhdY.tar.br",
    json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.10.0/KbIfTNbxShRX1A1FgXei1SpO5Jn8sgP6HP6PXbi-xyA.tar.br",
}

import pf.Task
import json.Json

main =
    pdf = printPdf {}
    Task.ok {}

# printPdf : { page ? F32 } -> List U8
printPdf = \{ page ? 21.4 } ->
    obj = {
        page,
    }
    Encode.toBytes obj Json.utf8

Result:

thread '<unnamed>' panicked at crates/compiler/mono/src/ir.rs:6143:56:
called `Option::unwrap()` on a `None` value
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

When I uncomment the type annotation # printPdf : { page ? F32 } -> List U8 (Frac * becomes F32) then I can run the example on Linux using --linker=legacy .

I have tried older compiler versions, older basic-cli platforms, and older json packages - all the same.

The example works with ints (e.g. I32, Num *) and with Str.

lukewilliamboswell commented 2 weeks ago

The following work for me on macOS

printPdf : { page ? F32 } -> List U8
printPdf = \{ page ? 21.4 } ->
printPdf : { page ? Dec } -> List U8
printPdf = \{ page ? 21.4 } ->
#printPdf : { page ? Dec } -> List U8
printPdf = \{ page ? 21.4dec } ->
#printPdf : { page ? F32 } -> List U8
printPdf = \{ page ? 21.4f32 } ->

I think the issue here is in resolving the types -- lambda set related, so the compiler crashes, but ideally should provide a nice error message.

@adomurad if you provide a type for the default value it seems to be working ok. I'm not sure if that entirely resolves this issue, but just wanted to add this context for future.

We do have many issues related to lambda set bugs -- this could be a nice repro.

adomurad commented 2 weeks ago

When I use F32:

printPdf : { page ? F32 } -> Str
printPdf = \{ page ? 21.4 } ->

Then I get:

The surgical linker currently has issue #3609 and would fail linking your app.
Please use `--linker=legacy` to avoid the issue for now.

When using the "legacy" linker, it works fine - but this seems like a different issue. I'm testing on linux.