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

Compilation failure (cannot use struct [...] as struct [...]) #785

Closed
klauspost opened this issue Aug 11, 2023 · 4 comments · Fixed by #808
Closed

Compilation failure (cannot use struct [...] as struct [...]) #785

klauspost opened this issue Aug 11, 2023 · 4 comments · Fixed by #808

Comments

@klauspost
Copy link

What version of Garble and Go are you using?

λ garble version
mvdan.cc/garble v0.10.1

Build settings:
      -buildmode exe
       -compiler gc
  DefaultGODEBUG panicnil=1
     CGO_ENABLED 1
          GOARCH amd64
            GOOS windows
         GOAMD64 v1

λ go version
go version go1.21.0 windows/amd64

What environment are you running Garble on?

go env Output
λ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=e:\temp\gocache
set GOENV=C:\Users\klaus\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=e:\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=e:\gopath
set GOPRIVATE=
set GOPROXY=https://goproxy.dev,direct
set GOROOT=C:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=e:\gopath\src\github.com\klauspost\compress\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=e:\temp\wintemp\go-build3567242259=/tmp/go-build -gno-record-gcc-switches

What did you do?

Compile this code:

package x

import "encoding/json"

func JSON() []byte {
	x := struct {
		TotalUncompressed int64 `json:"total_uncompressed"`
		TotalCompressed   int64 `json:"total_compressed"`
		Offsets           []struct {
			CompressedOffset   int64 `json:"compressed"`
			UncompressedOffset int64 `json:"uncompressed"`
		} `json:"offsets"`
		EstBlockUncomp int64 `json:"est_block_uncompressed"`
	}{
		TotalUncompressed: 1,
		TotalCompressed:   2,
		EstBlockUncomp:    3,
	}
	for _, v := range []int64{1, 2, 3} {
		x.Offsets = append(x.Offsets, struct {
			CompressedOffset   int64 `json:"compressed"`
			UncompressedOffset int64 `json:"uncompressed"`
		}{CompressedOffset: v, UncompressedOffset: v})
	}
	b, _ := json.MarshalIndent(x, "", "  ")
	return b
}

What did you expect to see?

λ go build repro.go

λ 

What did you see instead?

λ garble build repro.go
# command-line-arguments
HfGnX1.go:1: cannot use struct{MTsXn_rKxas int64 `json:"compressed"`; VJ6N6dh8 int64 `json:"uncompressed"`}{…} (value of type struct{MTsXn_rKxas int64 "json:\"compressed\""; VJ6N6dh8 int64 "json:\"uncompressed\""}) as struct{CompressedOffset int64 "json:\"compressed\""; UncompressedOffset int64 "json:\"uncompressed\""} value in argument to append
exit status 2
exit status 1

λ 

Seems like only right side is garbled.

@mvdan
Copy link
Member

mvdan commented Aug 13, 2023

We have a few of these bugs, like #685, and I'm trying to figure them out. Thanks for minifying a reproducer :)

@klauspost
Copy link
Author

For anyone else, the solution is to just extract the type:

	type offset struct {
		CompressedOffset   int64 `json:"compressed"`
		UncompressedOffset int64 `json:"uncompressed"`
	}

This will get you building until the compatibility issue is resolved.

@pagran
Copy link
Member

pagran commented Aug 26, 2023

I think this bug has two triggers:

Anon structs:

package main

import "encoding/json"

func JSON() []byte {
	x := struct {
		A []struct {
			A int64
		}
		B struct {
			B int64
		}
	}{}

	x.A = append(x.A, struct{ A int64 }{A: int64(0)})
	x.B = struct{ B int64 }{B: int64(0)}
	b, _ := json.MarshalIndent(x, "", "  ")
	return b
}
package main

import "encoding/json"

type ASTR struct {
	A int64
}

type BSTR struct {
	B int64
}

func JSON() []byte {
	x := struct {
		A []ASTR `json:"offsets"`
		B BSTR
	}{}

	x.A = append(x.A, struct{ A int64 }{A: int64(0)}) // [*ssa.ChangeType     ] t8 = changetype ASTR <- struct{A int64} (t5)
	x.B = struct{ B int64 }{B: int64(0)} // [*ssa.ChangeType     ] t16 = changetype BSTR <- struct{B int64} (t14)
	b, _ := json.MarshalIndent(x, "", "  ")
	return b
}

I think the second case can be fixed by analyzing ssa.ChangeType instruction

@mvdan
Copy link
Member

mvdan commented Aug 27, 2023

I think the second case can be fixed by analyzing ssa.ChangeType instruction

Indeed that's where I'm going with a couple of local patches I've got in progress. I only got stuck since, for some reason, the behavior seems to be somewhat random. I also sent a PR to update to Go 1.21 in the meantime, and got an unexpected timeout that I can't reproduce locally 😵‍💫

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

Successfully merging a pull request may close this issue.

3 participants