Stream: beginners

Topic: panic on recursive functions


view this post on Zulip Ghislain (Jun 18 2024 at 20:16):

Hi,
I can't understand why my program is panicking, could you help me?

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

displayBlock = \{ widget ? None } ->
    when widget is
        None -> "{}"
        w -> "{ $(displayWidget w) }"

displayGroup = \group ->
    "[$(group.widgets |> List.map displayWidget |> Str.joinWith ", ")]"

displayWidget = \widget ->
    when widget is
        Block block -> "block $(displayBlock block)"
        Group group -> "group $(displayGroup group)"
        None -> ""

main =
    # works
    Stdout.line! (displayWidget (Block {}))
    Stdout.line! (displayWidget (Group { widgets: [] }))
    Stdout.line! (displayWidget (Block { widget: Group { widgets: [] } }))
    # fails with panic
    Stdout.line! (displayWidget (Block { widget: Block {} }))
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("\x11\x00\x00\x00\x11\x00\x00\x00\xb4e[-\xa6\x8d\x12P"), definition of value binding ValueId(4): expected type '("UserApp"::"\xbf\xb4\xe0\xb0\xed\x84F\x9d",)', found type '((),)'
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:48):

I think the compiler is confused trying to figure out the types and you have a bug with type definitions

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:49):

I'm pretty sure that Block { widget: Block {} } is an impossible type*.

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:50):

Cause a block takes a record that contains a widget, but the inner block is taking an empty record

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:51):

That said, the types in this program are extra strange. I think that the compiler is generating two different versions of Block, but they can't be used together.

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:54):

I would try to explictly write out the types, I think that will reveal the issues.

My current guess as to the types is:

Widget : [
  Block {widget: Widget},
  Group (List Widget),
  None
]

Note, that type does not include Block {}.

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:56):

I'm honestly a bit surprised these two lines compile together:

    Stdout.line! (displayWidget (Block {}))
    Stdout.line! (displayWidget (Block { widget: Group { widgets: [] } }))

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:58):

Looking at the ir, this compiles cause roc is generating two distinct versions of displayWidget that accept different types.

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 20:59):

One version accepts the Widget type I specified above.

The other accepts the version with Block {} as a variant

view this post on Zulip Ghislain (Jun 18 2024 at 21:05):

Thank you for your reply, I tried with your type (a bit modified to have a record) and I get compiler issue this time:

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

Widget : [
    Block { widget : Widget },
    Group { widgets : List Widget },
    None,
]

displayBlock = \{ widget } ->
    when widget is
        None -> "{}"
        w -> "{ $(displayWidget w) }"

displayGroup = \group ->
    "[$(group.widgets |> List.map displayWidget |> Str.joinWith ", ")]"

displayWidget : Widget -> Str
displayWidget = \widget ->
    when widget is
        Block block -> "block $(displayBlock block)"
        Group group -> "group $(displayGroup group)"
        None -> "unknown"

main =
    Stdout.line! (displayWidget (Block { widget: None }))
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
thread '<unnamed>' panicked at crates/compiler/mono/src/layout.rs:1589:17:
no lambda set found for (`17.IdentId(4)`, []): LambdaSet {
    set: [],
    args: [
        InLayout(
            91,
        ),
    ],
    ret: InLayout(STR),
    representation: InLayout(VOID),
    full_layout: InLayout(
        103,
    ),
}
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 21:16):

Hmm....definitely a compiler bug. I'm really not sure the cause.

view this post on Zulip Brendan Hansknecht (Jun 18 2024 at 21:16):

Some reason commenting out the type is a workaround


Last updated: Jul 26 2025 at 12:14 UTC