Skip to content

Fix wasm-gc externref trait-object cast path#6

Open
bikallem wants to merge 1 commit intomoonbitlang:mainfrom
bikallem:pr/issue1123-externref-trait-cast
Open

Fix wasm-gc externref trait-object cast path#6
bikallem wants to merge 1 commit intomoonbitlang:mainfrom
bikallem:pr/issue1123-externref-trait-cast

Conversation

@bikallem
Copy link

Summary

Fixes the wasm-gc cast path for trait objects when #external (externref) values flow across supertrait chains.

What changed

  • clam_of_core: add explicit Pidentity lowering and tighten Pcast lowering so casts are emitted only when source/target ltypes differ.
  • wasm_of_clam_gc: handle Lcast between Ref_extern and Ref_any with dedicated ops (Extern_convert_any / Any_convert_extern) instead of generic ref_cast in those cases.

Why

Issue report shows invalid wasm validation/instantiation due to mismatch in casted ref kinds for #external -> trait object conversion.

Fixes moonbitlang/moonbit-docs#1123.

Copilot AI review requested due to automatic review settings February 11, 2026 20:30
Fixes moonbitlang/moonbit-docs#1123.

The wasm-gc backend was emitting invalid cast sequences when trait-object conversion involved #external values across supertrait chains.

This change adds a Pidentity lowering path in clam_of_core and tightens Pcast lowering to avoid redundant casts and preserve source/target type intent.

In wasm_of_clam_gc, Lcast now handles Ref_extern<->Ref_any with the dedicated conversion ops (Extern_convert_any / Any_convert_extern) instead of generic ref_cast where needed.
@bikallem bikallem force-pushed the pr/issue1123-externref-trait-cast branch from f2811ac to 9dbea76 Compare February 11, 2026 20:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes wasm-gc reference casting for trait-object paths when #external (externref) values cross supertrait chains, avoiding invalid Wasm validation due to mismatched ref kinds.

Changes:

  • Emit extern.convert_any / any.convert_extern for Lcast between Ref_extern and Ref_any instead of using generic ref.cast.
  • Lower %identity and tighten %cast lowering so Lcast is only emitted when source/target ltypes differ (and ensure casts operate on vars via bind).
  • Adjust stub type validation to allow function-typed stub parameters under the Wasm_gc target.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/wasm_of_clam_gc.ml Special-cases Lcast between externref/anyref using dedicated Wasm GC conversion ops.
src/clam_of_core.ml Adds explicit %identity lowering and avoids emitting redundant Lcast when ltypes are equal.
src/typeutil.ml Changes stub type checking behavior for function types when targeting Wasm_gc.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wasm-gc: trait object from #external type causes validation error

1 participant