Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime: type-assertion on Safari/WASM sometimes generates "types from different scopes" #46895

Open
Inkeliz opened this issue Jun 23, 2021 · 0 comments

Comments

@Inkeliz
Copy link

@Inkeliz Inkeliz commented Jun 23, 2021

What version of Go are you using (go version)?

go version go1.16.5 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lucas\AppData\Local\go-build
set GOENV=C:\Users\lucas\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=Z:\GOPATH\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=Z:\GOPATH
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\lucas\AppData\Local\Temp\go-build1684762088=/tmp/go-build -gno-record-gcc-switches

What did you do?

It's dificult to reproduce the issue, I tried to create many structs and all of them works. In my program I use the following construction:

if resp, ok,_ := sp.Request.Result(); ok {
    if resp, ok := resp.(*common.OrderCreateResponse); ok {
            //...
       }
    }

The sp.Request.Result() returns a interface{} which contains the decoded body from http request. For every response, I use the type-assertion. So, for Login page it may have something like *common.LoginResponse and on SignUp it will use *common.SignUpResponse, for instance.

On all platforms, the code works, except on Safari/WebAssembly. The code works on many cases (say *common.OrderCreateResponse works, but *common.SignUpResponse doesn't). That is why is so difficult to create a test or to provide a code to reproduce the issue.

What did you expect to see?

The same behavior of all other OSes (including the WebAssembly on Chrome/Windows). On all other OSes (Windows, Linux, Android) and also on WebAssembly runing on Windows and Android: the ok from the type-assertion is true.

What did you see instead?

The ok on type-assertion returns false on Safari/WebAssembly. However, the resp of the same type-assertion is a non-nil value, which contains the data/struct correctly.


More tests and weird results:

  1. Removing the ok: (fail)
resp := resp.(*common.OrderCreateResponse)

It works on all OSes, except on Safari, which gives:

panic: interface conversion: interface {} is *common.OrderCreateResponse, not *common.OrderCreateResponse (types from different scopes)
  1. Ignoring the ok: (works)
if resp, _ := resp.(*common.OrderCreateResponse); resp != nil {
fmt.Println("ok", resp)
}

It works everywhere, including Safari. The resp is a non-nil! Notice that I'm using resp, _ := instead of resp :=.

  1. Switch type: (works)
switch resp := resp.(type) {
    case *common.OrderCreateResponse:
    fmt.Println("ok", resp)
}

It works everywhere, including Safari.

  1. Using reflect before the type-assertion (works):
if resp, ok,_ := sp.Request.Result(); ok {
    fmt.Println("resp",  reflect.TypeOf(resp).String())                                                       // Prints: *common.OrderCreateResponse
    fmt.Println("base",  reflect.TypeOf(new(common.OrderCreateResponse)).String())  // Prints: *common.OrderCreateResponse

    if resp, ok := resp.(*common.OrderCreateResponse); ok {
        fmt.Println("ok", resp) // It works now!
    }
}

For some random reason, that works on Safari! When I insert the prints, the resp.(*common.OrderCreateResponse) returns ok=true, for some unknown reason.

@toothrot toothrot changed the title type-assertion on Safari/WASM sometimes generates "types from different scopes" runtime: type-assertion on Safari/WASM sometimes generates "types from different scopes" Jun 24, 2021
@toothrot toothrot added this to the Backlog milestone Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants