From f5715181392c68d928c3152f8cf90fa9d4ee9e4e Mon Sep 17 00:00:00 2001 From: kkHAIKE Date: Wed, 11 May 2022 02:27:21 +0000 Subject: [PATCH] cmd/cgo: dont override declared struct type Fixes #52611 Change-Id: I835df8d6a98a37482446ec00af768c96fd8ee4fe GitHub-Last-Rev: ea54dd69eef90eaf1641889039344fff70158ece GitHub-Pull-Request: golang/go#52733 Reviewed-on: https://go-review.googlesource.com/c/go/+/404497 Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Reviewed-by: Dexter Ouyang Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Alex Rakoczy TryBot-Result: Gopher Robot --- misc/cgo/test/testdata/issue52611.go | 13 +++++++++++++ misc/cgo/test/testdata/issue52611a/a.go | 16 ++++++++++++++++ misc/cgo/test/testdata/issue52611a/b.go | 11 +++++++++++ misc/cgo/test/testdata/issue52611b/a.go | 11 +++++++++++ misc/cgo/test/testdata/issue52611b/b.go | 16 ++++++++++++++++ src/cmd/cgo/gcc.go | 5 +++++ 6 files changed, 72 insertions(+) create mode 100644 misc/cgo/test/testdata/issue52611.go create mode 100644 misc/cgo/test/testdata/issue52611a/a.go create mode 100644 misc/cgo/test/testdata/issue52611a/b.go create mode 100644 misc/cgo/test/testdata/issue52611b/a.go create mode 100644 misc/cgo/test/testdata/issue52611b/b.go diff --git a/misc/cgo/test/testdata/issue52611.go b/misc/cgo/test/testdata/issue52611.go new file mode 100644 index 0000000000..32d22403ab --- /dev/null +++ b/misc/cgo/test/testdata/issue52611.go @@ -0,0 +1,13 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 52611: inconsistent compiler behaviour when compiling a C.struct. +// No runtime test; just make sure it compiles. + +package cgotest + +import ( + _ "cgotest/issue52611a" + _ "cgotest/issue52611b" +) diff --git a/misc/cgo/test/testdata/issue52611a/a.go b/misc/cgo/test/testdata/issue52611a/a.go new file mode 100644 index 0000000000..0764688ec4 --- /dev/null +++ b/misc/cgo/test/testdata/issue52611a/a.go @@ -0,0 +1,16 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue52611a + +/* +typedef struct Foo { + int X; +} Foo; +*/ +import "C" + +func GetX1(foo *C.struct_Foo) int32 { + return int32(foo.X) +} diff --git a/misc/cgo/test/testdata/issue52611a/b.go b/misc/cgo/test/testdata/issue52611a/b.go new file mode 100644 index 0000000000..74a50c5dea --- /dev/null +++ b/misc/cgo/test/testdata/issue52611a/b.go @@ -0,0 +1,11 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue52611a + +import "C" + +func GetX2(foo *C.struct_Foo) int32 { + return int32(foo.X) +} diff --git a/misc/cgo/test/testdata/issue52611b/a.go b/misc/cgo/test/testdata/issue52611b/a.go new file mode 100644 index 0000000000..730b52f5e9 --- /dev/null +++ b/misc/cgo/test/testdata/issue52611b/a.go @@ -0,0 +1,11 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue52611b + +import "C" + +func GetX1(bar *C.struct_Bar) int32 { + return int32(bar.X) +} diff --git a/misc/cgo/test/testdata/issue52611b/b.go b/misc/cgo/test/testdata/issue52611b/b.go new file mode 100644 index 0000000000..d304175395 --- /dev/null +++ b/misc/cgo/test/testdata/issue52611b/b.go @@ -0,0 +1,16 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package issue52611b + +/* +typedef struct Bar { + int X; +} Bar; +*/ +import "C" + +func GetX2(bar *C.struct_Bar) int32 { + return int32(bar.X) +} diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 4dff5e2b1c..3cb01ba382 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -2551,6 +2551,11 @@ func (c *typeConv) loadType(dtype dwarf.Type, pos token.Pos, parent string) *Typ t.Go = name // publish before recursive calls goIdent[name.Name] = name if dt.ByteSize < 0 { + // Don't override old type + if _, ok := typedef[name.Name]; ok { + break + } + // Size calculation in c.Struct/c.Opaque will die with size=-1 (unknown), // so execute the basic things that the struct case would do // other than try to determine a Go representation.