roc-lang / roc

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

Roc crashes with `Expected symbol to be in the map` #5909

Open lukewilliamboswell opened 1 year ago

lukewilliamboswell commented 1 year ago

This happens when unwrapping the NeedsColors opaque type. (@NeedsColors {colors}, { foo, bar, baz }) = ... looks to be the cause, as (_, { foo, bar, baz }) = ... or (state, { foo, bar, baz }) = ... are ok.

% roc test Experiments/Count.roc
thread 'main' panicked at 'Expected symbol to be in the map', crates/compiler/mono/src/inc_dec.rs:392:26
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Crashes

interface Count
    exposes []
    imports []

NeedsColors := {
    colors : List Color,
}

Color : [
    Red,
    Green,
    Blue,
]

Count a := (NeedsColors, a)

from : NeedsColors, a -> Count a
from = \value, a ->
    @Count (value, a)

applyColor : Color -> (Count (Nat -> a) -> Count a)
applyColor = \c -> \@Count (@NeedsColors {colors}, advance) ->

    id = List.len colors

    @Count (@NeedsColors {colors: List.append colors c}, advance id)

done : Count a -> (NeedsColors, a)
done = \@Count (state, final) -> 
    (state, final)

expect 

    initial : NeedsColors
    initial = @NeedsColors {colors: []}

    (@NeedsColors {colors}, { foo, bar, baz }) = 
        from initial {
            foo: <- applyColor Red,
            bar: <- applyColor Green,
            baz: <- applyColor Blue,
        } |> done

    len = List.len colors

    foo == 0 && bar == 1 && baz == 2 && len == 3

Works

interface Count
    exposes []
    imports []

NeedsColors := {
    colors : List Color,
}

Color : [
    Red,
    Green,
    Blue,
]

Count a := (NeedsColors, a)

from : NeedsColors, a -> Count a
from = \value, a ->
    @Count (value, a)

applyColor : Color -> (Count (Nat -> a) -> Count a)
applyColor = \c -> \@Count (@NeedsColors {colors}, advance) ->

    id = List.len colors

    @Count (@NeedsColors {colors: List.append colors c}, advance id)

done : Count a -> (NeedsColors, a)
done = \@Count (state, final) -> 
    (state, final)

expect 

    initial : NeedsColors
    initial = @NeedsColors {colors: []}

    (_, { foo, bar, baz }) = 
        from initial {
            foo: <- applyColor Red,
            bar: <- applyColor Green,
            baz: <- applyColor Blue,
        } |> done

    foo == 0 && bar == 1 && baz == 2 
jwoudenberg commented 3 months ago

This might be related. I ran into something similar when trying to unpack an opaque type containing a lambda:

module [Foo]

Foo := { fn : {} -> Str }

expect
    x = @Foo { fn: \_ -> "Hi" }
    (@Foo res) = x
    res.fn {} == "Hi"

roc test will fail with:

thread 'main' panicked at crates/compiler/mono/src/inc_dec.rs:400:26:
Expected symbol to be in the map
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

roc check is fine.

Roc commit 70fa3ecdc2c76a8bde74bed2048beb8d02bdbd92

skyqrose commented 2 weeks ago

Probably the same as https://github.com/roc-lang/roc/issues/5513 , in which case it was fixed by https://github.com/roc-lang/roc/pull/6493

JRI98 commented 2 weeks ago

Still reproduces on Apple silicon:

module [Foo]

Foo := { fn : {} -> Str }

expect
    x = @Foo { fn: \_ -> "Hi" }
    (@Foo res) = x
    res.fn {} == "Hi"
jwoudenberg commented 2 weeks ago

Thanks for cleaning up issues!

I can unfortunately still reproduce the error too on x86_64 linux.