life icon indicating copy to clipboard operation
life copied to clipboard

Incorrect switch statement execution in go

Open icexin opened this issue 7 years ago • 4 comments

package main

func main() {
	s := "f64"
	switch s {
	case "i32":
	case "i64":
	case "f32":
	case "f64":
	default:
		panic(s)
	}
}

go version go1.11.1 darwin/amd64

The default branch is triggered, then panic. It's ok in browser.

icexin avatar Nov 04 '18 12:11 icexin

A runtime is needed to run WebAssembly modules compiled from Go. What runtime are you using? Are you sure that the panic originates from the default branch but not somewhere else? And what message did you get exactly when the program panics?

losfair avatar Nov 04 '18 14:11 losfair

I Wrote a simple version wasm go runtime.

https://gist.github.com/icexin/3123b51d2ae97c21aade3ea2975ecea2

The output is

Resolve func: go debug
Resolve func: go runtime.wasmExit
Resolve func: go runtime.wasmWrite
Resolve func: go runtime.nanotime
Resolve func: go runtime.walltime
Resolve func: go runtime.scheduleCallback
Resolve func: go runtime.clearScheduledCallback
Resolve func: go runtime.getRandomData
panic: f64

goroutine 1 [running]:
main.main()
	/Users/icexin/test/wasm/switch.go:11 +0x16
return value = 0, duration = 20.195955ms

icexin avatar Nov 04 '18 15:11 icexin

@losfair Any progress?

icexin avatar Nov 12 '18 04:11 icexin

I've run into this same issue. Can reproduce. Although for me it seems to be related to how many switch cases are available. This runs the default branch:

net := "tcp"
switch net {
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6":
	fmt.Println("Should match")
case "ip", "ip4", "ip6":
	fmt.Println("shouldn't match")
default:
	log.Fatal("broken")
}

Altering this line to just three options runs successfully.

case "tcp", "tcp4", "tcp6":

maxmcd avatar Dec 04 '18 16:12 maxmcd