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: C Structs with the same field definitions are used interchangeably in generated prototypes #31891

Open
kesmit13 opened this issue May 7, 2019 · 0 comments

Comments

Projects
None yet
2 participants
@kesmit13
Copy link

commented May 7, 2019

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

$ go version
go version go1.12.1 darwin/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/nd/fslycqgs2m54077zvlwx158h000464/T/go-build014851518=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

If I use the following struct definitions in functions exported by cgo, the
struct names get mixed up in the generated h file.

Input h File

typedef struct {
    char *objtype;
    unsigned long long obj;
} Connection;

typedef struct {
    char *objtype;
    unsigned long long obj;
} Error;

typedef struct {
    char *objtype;
    unsigned long long obj;
} CASValueList;

Exported Function Definitions

func casConnectionDestroy(c *C.CASConnection) { ... }
func casErrorDestroy(e *C.CASError) { ... }

What did you expect to see?

I expected the generated prototypes to look like the following.

extern void casConnectionDestroy(CASConnection* p0);
extern void casErrorDestroy(CASError* p0);

What did you see instead?

There are the prototypes that were generated instead.

extern void casConnectionDestroy(CASValueList* p0);
extern void casErrorDestroy(CASValueList* p0);

Observation

I suspected that some sort of hashing was being used to alias structs that
had the same fields, so I added a unique field to each of the structs in the
input header file and that did solve the problem.

@ianlancetaylor ianlancetaylor changed the title C Structs with the same field definitions are used interchangeably in generated prototypes cmd/cgo: C Structs with the same field definitions are used interchangeably in generated prototypes May 7, 2019

@ianlancetaylor ianlancetaylor added this to the Go1.13 milestone May 7, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.