Skip to content

wit/bindgen, cm: wrong value of type result<bool, s8> #284

@ethanstanley3

Description

@ethanstanley3

Description

echo "`wit-bindgen-go version` | `tinygo version` | `wasmtime --version` | `uname -m`"
wit-bindgen-go version v0.5.0 | tinygo version 0.35.0 darwin/arm64 (using go version go1.23.4 and LLVM version 18.1.2) | wasmtime 28.0.1 (1bdf2c2b5 2025-01-14) | arm64

wit-bindgen-go represents the WIT definition type alias = result<bool, s8> with the following type definition: type Alias cm.Result[bool, bool, int8]. Initializing and retrieving a data value of type Alias produces unexpected behavior.

Component Model bindings are generated using wit-bindgen-go for the following WIT world definition:

world foo {
  import wasi:io/streams@0.2.0;
  import wasi:cli/stdout@0.2.0;
  import wasi:random/random@0.2.0;

  type alias = result<bool, s8>;

  export wasi:cli/run@0.2.0;
}

The following Go source program targets the world foo. It initializes a variable of type Alias and writes its value to standard output:

func main() {
	var val foo.Alias = cm.Err[foo.Alias](5)

	if ok_val, err_val, is_err := val.Result(); is_err {
		println("Err(", err_val, ")")
	} else {
		println("OK(", ok_val, ")")
	}
}

When the program above is compiled to a Wasm component using tinygo and executed using Wasmtime, an incorrect value is printed.

Steps to reproduce

Here is a zipped directory that reproduces the bug:
wrong-result-value.zip

  1. Unzip wrong-result-value.zip
  2. Build and run with ./verify.sh

Expected behavior

The printed value should be:

Err( 5 )

Actual behavior

I get the following output:

Err( 1 )

It seems that the Err value of type int8 is being treated as a bool i.e. odd numbered values result in 1 being printed while even values result in 0. The correct value is printed if the shape of the result is changed to int8.

Additional context

The Go source program was derived from programs generated by a differential testing framework for wit-bindgen. The wit definitions are derived from a test case produced by wit-smith.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions