Skip to content

feat(compiler): Stub panic with unreachable to prevent bad codegen#2382

Open
spotandjake wants to merge 1 commit into
grain-lang:oscar/gc-rebasedfrom
spotandjake:spotandjake/panic-stub
Open

feat(compiler): Stub panic with unreachable to prevent bad codegen#2382
spotandjake wants to merge 1 commit into
grain-lang:oscar/gc-rebasedfrom
spotandjake:spotandjake/panic-stub

Conversation

@spotandjake

Copy link
Copy Markdown
Member

This pr stubs panic with unreachable when we are compiling in @runtimeMode.

Because we use @runtimeMode itself to compile the panic module, we can't allow a circular dependency, so we don't import panic when compiling in runtime mode.

This had the side effect that if someone tried to use a higher-level expression such as arr[x], we would generate invalid WASM. In order to avoid this, I figure we can just fail with unreachable instead of properly panicking. The downside is we don't get a nice error, but I figure that is a perfectly reasonable tradeoff in the context of the runtime.

Closes: #2379

Comment thread compiler/src/codegen/compcore.re Outdated
resolve_global(~env, panic_with_exception_name),
gensym_label("runtime_panic_stub"),
List.fold_left(
(acc, arg) => [Expression.Drop.make(wasm_mod, arg), ...acc],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little weird to me. This is only the exception being passed to the panic handler, so there's no sense in keeping it around. We should be able to just emit unreachable and have it be a little cleaner.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair. I made the change.

The reason I chose to drop the arguments initially was suppose that any of the arguments had side effects like local.tee, we might be setting a value and using it later, in which case not emitting the wasm would cause issues. Reviewing the implementation of compile_imm, this won't happen right now.

@spotandjake
spotandjake force-pushed the spotandjake/panic-stub branch from dfa54d6 to 82b0284 Compare May 6, 2026 00:11
@spotandjake
spotandjake changed the base branch from main to oscar/gc-rebased May 6, 2026 00:11

@ospencer ospencer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very next instruction is unreachable, so side effects like a local tee wouldn't matter. The thing to look out here for would be if we didn't do SSA, since an argument could be something like a function call. But not something for us to worry about!

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: grain-lang#2379
fix: skip test in js mode
@spotandjake
spotandjake force-pushed the spotandjake/panic-stub branch from 82b0284 to ae37229 Compare May 10, 2026 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compiler: Have call_panic_handler produce unreachable in @runtimeMode

2 participants