From 82b02847060aa01a42c559a4db3849563a0237cb Mon Sep 17 00:00:00 2001 From: Spotandjake Date: Fri, 3 Apr 2026 00:40:17 -0400 Subject: [PATCH] feat(compiler): Stub panic with unreachable to prevent bad codegen This pr stubs panic with unreachable when we are compiling in `@runtimeMode`. The reason for this change is that when working in `@runtimeMode` we don't have the panic module available however if you want to use something like `arr[x]` you are out of luck as it has a dependency on panic, this results in bad wasm codegen. My solution to this is we just fail with `unreachable` the downside is we don't get a nice error but if someone is working in the runtime I feel like thats a reasonable sacrifice, to be able to write the higher level code in the first place. Closes: #2379 fix: skip test in js mode --- compiler/src/codegen/compcore.re | 28 ++++++++++++++++------------ compiler/test/suites/runtime.re | 12 ++++++++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/compiler/src/codegen/compcore.re b/compiler/src/codegen/compcore.re index d2cef90bca..1fe1f2ed4c 100644 --- a/compiler/src/codegen/compcore.re +++ b/compiler/src/codegen/compcore.re @@ -149,20 +149,24 @@ let get_metadata_ptr = wasm_mod => let get_grain_imported_name = (mod_, name) => Ident.unique_name(name); let call_panic_handler = (wasm_mod, env, args) => { - let args = [ - Expression.Global_get.make( + switch (env.compilation_mode) { + | Runtime => Expression.Unreachable.make(wasm_mod) + | _ => + let args = [ + Expression.Global_get.make( + wasm_mod, + resolve_global(~env, panic_with_exception_name), + ref_any(), + ), + ...args, + ]; + Expression.Call.make( wasm_mod, - resolve_global(~env, panic_with_exception_name), + resolve_func(~env, panic_with_exception_name), + args, ref_any(), - ), - ...args, - ]; - Expression.Call.make( - wasm_mod, - resolve_func(~env, panic_with_exception_name), - args, - ref_any(), - ); + ); + }; }; let call_equal = (wasm_mod, env, args) => diff --git a/compiler/test/suites/runtime.re b/compiler/test/suites/runtime.re index 35cc3ec8ce..ed7c060c0e 100644 --- a/compiler/test/suites/runtime.re +++ b/compiler/test/suites/runtime.re @@ -5,7 +5,19 @@ describe("runtime", ({test, testSkip}) => { let test_or_skip = Sys.backend_type == Other("js_of_ocaml") ? testSkip : test; + let assertRunError = makeErrorRunner(test_or_skip); let assertRuntime = makeRuntimeRunner(test_or_skip); + + assertRunError( + ~config_fn=() => {Grain_utils.Config.compilation_mode := Runtime}, + "runtime_mode_panics", + {| + let arr = [> 1, 2, 3] + provide let x = arr[10] + |}, + "RuntimeError: unreachable", + ); + assertRuntime("numbers.test"); assertRuntime("unsafe/wasmf32.test");