Skip to content

core: make abi::cast and abi::flat_types public#1597

Merged
pchickey merged 1 commit intobytecodealliance:mainfrom
ejrgilbert:task/pub-cast-and-flatten
Apr 20, 2026
Merged

core: make abi::cast and abi::flat_types public#1597
pchickey merged 1 commit intobytecodealliance:mainfrom
ejrgilbert:task/pub-cast-and-flatten

Conversation

@ejrgilbert
Copy link
Copy Markdown
Contributor

First, thanks so much for this crate, it's really simplified the implementation of my tool! I think you kept me from aging about 20 years over the course of a month :)

Motivation

The Generator emits Instruction::Bitcasts automatically during lowering of heterogeneous-arm variants (lower_variant_arms), but never during lifting. Rather, read_variant_arms_from_memory reads each arm in its natural flat shape, then fires VariantLift without any widening in between.

That asymmetry is fine for language backends whose Operand is source text (since the variant-arm widening is deferred until the value is next lowered or pattern-matched, and the source-level type system handles it).

However, it's a problem for Bindgen implementations that emit wasm directly. For any variant whose arms join to a wider type (e.g. result<string, u64>, where [Pointer, Length] and [I64] unify to [PointerOrI64, Length]), the wasm stack after each arm block has to converge on the joined-flat signature before the block ends...otherwise the module fails validation with "expected i64, found i32". The backend must emit the casts itself.

The Fix

Enabling the backend to emit the casts just requires access to two things the upstream owns:

  • cast(WasmType, WasmType) -> Bitcast: the canonical-ABI bitcast-selection table.
  • flat_types(&Resolve, &Type, Option<usize>) -> Option<Vec<WasmType>>: the joined flat shape to target.

We can simply flip both to pub (they currently reference only already-public types: WasmType, Bitcast, Resolve, Type). No behavioral change; existing private callers are unaffected.

Discovering Use Case

I'm currently leveraging this crate to offload the canonical ABI lifting/lowering in my splicer tool that performs interface-agnostic component interposition by generating component adapters in pure Wasm. Right now, I have verbatim copies of these two functions. This PR would let me delete those copies and depend on your upstream implementation.

@pchickey
Copy link
Copy Markdown
Contributor

Seems reasonable to make these part of wit-bindgen's public API, thanks

@pchickey pchickey added this pull request to the merge queue Apr 20, 2026
Merged via the queue into bytecodealliance:main with commit 5f20865 Apr 20, 2026
29 checks passed
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.

2 participants