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

cmd/cgo: types regression for anonymous structs #38408

Closed
yalegko opened this issue Apr 13, 2020 · 5 comments
Closed

cmd/cgo: types regression for anonymous structs #38408

yalegko opened this issue Apr 13, 2020 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@yalegko
Copy link

yalegko commented Apr 13, 2020

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

$ go version
go version go1.14.2 linux/amd64

Does this issue reproduce with the latest release?

The issue is reproduced on any go1.14+ and not reproduced on go1.13.10 and below

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

go env Output
$ go env

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/yalegko/.cache/go-build"
GOENV="/home/yalegko/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/yalegko/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build064772607=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Use a typedef on anonymous struct in cgo:

package main

/*
	typedef struct {
		int i;
	} A, *PA;
*/
import "C"
import "fmt"

func main() {
	var a C.PA = &C.A{
		i: 1,
	}
	fmt.Printf("%#v\n", a)
}

Then just build it:

go build -a .

What did you expect to see?

Nothing and successfully compiled binary

What did you see instead?

# _/tmp/tst
./tst.go:12:6: cannot use &_Ctype_struct___0 literal (type *_Ctype_struct___0) as type _Ctype_PA in assignment

Additional info

According to _cgo_gotypes.go it looks like compiler can't understand that *A and PA is the same type and generates 2 identically structures for them.

The issue is not reproduced neither for named strucutres, e.g.

typedef struct a {
	int i;
} A, *PA;

nor for 2 separate typedefs:

typedef struct a {
	int i;
} A;
typedef A *PA;
@tklauser
Copy link
Member

Looks like another case caused by https://golang.org/cl/181857, also see #37479 and #37621

/cc @ianlancetaylor

@tklauser tklauser added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 13, 2020
@ianlancetaylor
Copy link
Contributor

@gopherbot Please add issue for backport to Go 1.14.

This is an incompatible change to how cgo handles anonymous structs. There is no workaround for people using C headers that use the construct that changed incompatibly. We should fix this in the 1.14 branch.

@gopherbot
Copy link

Backport issue(s) opened: #38426 (for 1.14).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.

@gopherbot
Copy link

Change https://golang.org/cl/228102 mentions this issue: cmd/cgo: use consistent tag for a particular struct

@gopherbot
Copy link

Change https://golang.org/cl/228107 mentions this issue: [release-branch.go1.14] cmd/cgo: use consistent tag for a particular struct

gopherbot pushed a commit that referenced this issue Apr 14, 2020
…struct

For #31891
For #38408
Fixes #38426

Change-Id: Ie7498c2cab728ae798e66e7168425e16b063520e
Reviewed-on: https://go-review.googlesource.com/c/go/+/228102
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
(cherry picked from commit 33ff63d)
Reviewed-on: https://go-review.googlesource.com/c/go/+/228107
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Apr 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants