roc-lang / roc

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

Debugging help for compiler crash #4369

Closed bhansconnect closed 1 year ago

bhansconnect commented 2 years ago

So I am trying to resurrect an old project and get it compiling again. I updated the app to check again, but now it is hitting a compiler bug that I am quite uncertain of. Looking for help in debugging/working around this. The project is here: https://github.com/bhansconnect/functional-mos6502-web-performance

I am trying to get roc build --target=wasm32 implementations/roc/emulator.roc to work. I partially minimized the example on the minimized branch. Specifically I get the error when the main `when statement at the bottom of the file has at least 2 matches. If you comment out this line the app will compile.

@ayazhafiz, I would assume you would have the best guess here. Any ideas?

Error:

thread 'main' panicked at 'internal error: entered unreachable code: symbol/layout `17.IdentId(11)` ProcLayout {
    arguments: [
        Struct {
            field_order_hash: FieldOrderHash(
                0,
            ),
            field_layouts: [],
        },
        LambdaSet(
            LambdaSet {
                set: [
                    ( 17.11, [LambdaSet(LambdaSet { set: [( 17.11, [LambdaSet(LambdaSet { set: [( 17.32, [Builtin(Int(U16))])], representation: Interned(2, PhantomData) }), LambdaSet(LambdaSet { set: [( 16.55, [Struct { field_order_hash: FieldOrderHash(1371262882315734191), field_layouts: [Builtin(Int(U16)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8))] }])], representation: Interned(3, PhantomData) })])], representation: Interned(4, PhantomData) }), LambdaSet(LambdaSet { set: [( 16.73, [])], representation: Interned(0, PhantomData) })]),
                    ( 17.11, [LambdaSet(LambdaSet { set: [( 17.32, [Builtin(Int(U16))])], representation: Interned(2, PhantomData) }), LambdaSet(LambdaSet { set: [( 16.55, [Struct { field_order_hash: FieldOrderHash(1371262882315734191), field_layouts: [Builtin(Int(U16)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8))] }])], representation: Interned(3, PhantomData) })]),
                ],
                representation: Interned(
                    8,
                    PhantomData,
                ),
            },
        ),
    ],
    result: Struct {
        field_order_hash: FieldOrderHash(
            11857316205579443912,
        ),
        field_layouts: [
            Struct {
                field_order_hash: FieldOrderHash(
                    1371262882315734191,
                ),
                field_layouts: [
                    Builtin(
                        Int(
                            U16,
                        ),
                    ),
                    Builtin(
                        Int(
                            U8,
                        ),
                    ),
                    Builtin(
                        Int(
                            U8,
                        ),
                    ),
                    Builtin(
                        Int(
                            U8,
                        ),
                    ),
                    Builtin(
                        Int(
                            U8,
                        ),
                    ),
                    Builtin(
                        Int(
                            U8,
                        ),
                    ),
                ],
            },
            Builtin(
                Int(
                    U8,
                ),
            ),
        ],
    },
    captures_niche: CapturesNiche(
        [
            LambdaSet(
                LambdaSet {
                    set: [
                        ( 17.11, [LambdaSet(LambdaSet { set: [( 17.32, [Builtin(Int(U16))])], representation: Interned(2, PhantomData) }), LambdaSet(LambdaSet { set: [( 16.55, [Struct { field_order_hash: FieldOrderHash(1371262882315734191), field_layouts: [Builtin(Int(U16)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8)), Builtin(Int(U8))] }])], representation: Interned(3, PhantomData) })]),
                    ],
                    representation: Interned(
                        4,
                        PhantomData,
                    ),
                },
            ),
            LambdaSet(
                LambdaSet {
                    set: [
                        ( 16.73, []),
                    ],
                    representation: Interned(
                        0,
                        PhantomData,
                    ),
                },
            ),
        ],
    ),
} combo must be in DeclarationToIndex', crates/compiler/mono/src/borrow.rs:165:9
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: roc_mono::borrow::ParamMap::get_param_offset
   3: roc_mono::borrow::BorrowInfState::collect_expr
   4: roc_mono::borrow::BorrowInfState::collect_stmt
   5: roc_mono::borrow::BorrowInfState::collect_stmt
   6: roc_mono::borrow::BorrowInfState::collect_stmt
   7: roc_mono::borrow::BorrowInfState::collect_stmt
   8: roc_mono::borrow::infer_borrow
   9: roc_mono::ir::Proc::insert_refcount_operations
  10: roc_load_internal::file::update
  11: roc_load_internal::file::state_thread_step
  12: roc_load_internal::file::load
  13: roc_load::load_and_monomorphize
  14: roc_cli::build::build_file
  15: roc_cli::build
  16: roc::main
ayazhafiz commented 1 year ago

@bhansconnect wow, I'm sorry to have missed this - I believe #4745 fixes this! I no longer get this mono panic, though I do get some zig-related errors that I think are my own local problem. Could you check on #4745?

bhansconnect commented 1 year ago

That seems to have changed the error, but their still is one:

thread '<unnamed>' panicked at 'index out of bounds: the len is 3719 but the index is 3719', crates/compiler/types/src/unification_table.rs:295:49
stack backtrace:
   0: rust_begin_unwind
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:142:14
   2: core::panicking::panic_bounds_check
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/panicking.rs:84:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/slice/index.rs:242:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/slice/index.rs:18:9
   5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/alloc/src/vec/mod.rs:2627:9
   6: roc_types::unification_table::UnificationTable::root_key_without_compacting
             at /Users/bren077s/Projects/roc/crates/compiler/types/src/unification_table.rs:295:49
   7: roc_types::unification_table::UnificationTable::root_key
             at /Users/bren077s/Projects/roc/crates/compiler/types/src/unification_table.rs:283:20
   8: roc_types::unification_table::UnificationTable::set_rank
             at /Users/bren077s/Projects/roc/crates/compiler/types/src/unification_table.rs:257:24
   9: roc_types::subs::Subs::set_rank
             at /Users/bren077s/Projects/roc/crates/compiler/types/src/subs.rs:1893:9
  10: roc_solve::solve::introduce
             at /Users/bren077s/Projects/roc/crates/compiler/solve/src/solve.rs:3881:9
  11: roc_solve::solve::solve
             at /Users/bren077s/Projects/roc/crates/compiler/solve/src/solve.rs:937:25
  12: roc_solve::solve::run_in_place
             at /Users/bren077s/Projects/roc/crates/compiler/solve/src/solve.rs:604:17
  13: roc_solve::solve::run
             at /Users/bren077s/Projects/roc/crates/compiler/solve/src/solve.rs:540:15
  14: roc_solve::module::run_solve
             at /Users/bren077s/Projects/roc/crates/compiler/solve/src/module.rs:87:37
  15: roc_load_internal::file::run_solve_solve
             at /Users/bren077s/Projects/roc/crates/compiler/load_internal/src/file.rs:4695:68
  16: roc_load_internal::file::run_solve
             at /Users/bren077s/Projects/roc/crates/compiler/load_internal/src/file.rs:4804:13
  17: roc_load_internal::file::run_task
             at /Users/bren077s/Projects/roc/crates/compiler/load_internal/src/file.rs:5863:17
  18: roc_load_internal::file::worker_task
             at /Users/bren077s/Projects/roc/crates/compiler/load_internal/src/file.rs:2117:34
  19: roc_load_internal::file::load_multi_threaded::{{closure}}::{{closure}}
             at /Users/bren077s/Projects/roc/crates/compiler/load_internal/src/file.rs:1942:25
  20: crossbeam_utils::thread::ScopedThreadBuilder::spawn::{{closure}}
             at /Users/bren077s/.cargo/registry/src/github.com-1ecc6299db9ec823/crossbeam-utils-0.8.10/src/thread.rs:438:31
  21: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5
  22: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/alloc/src/boxed.rs:1935:9
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
ayazhafiz commented 1 year ago

can you try with a release build of roc? I only saw that in debug build

bhansconnect commented 1 year ago

I hit it in both debug and release on the i4712 branch.

ayazhafiz commented 1 year ago

🤨 alright we need a small reproducer

ayazhafiz commented 1 year ago

🤦🏼 I had a silly bug. @bhansconnect can you try repelling i4712 and trying again

ayazhafiz commented 1 year ago

Seeing this from the roc repo

$ target/debug/roc build --target=wasm32 ../functional-mos6502-web-performance/implementations/roc/emulator.roc
🔨 Rebuilding platform...
0 errors and 0 warnings found in 1345 ms while successfully building:

    ../functional-mos6502-web-performance/implementations/roc/emulator.wasm
bhansconnect commented 1 year ago

Awesome. Yeah, that fixes this.

Note: the full application at roc_impl branch will only build with release build of the roc compiler. In debug, it hits:

thread '<unnamed>' panicked at 'assertion failed: `(left == right)`
  left: `true`,
 right: `false`', crates/compiler/mono/src/ir.rs:1180:33