]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: dont override declared struct type
authorkkHAIKE <kkhaike@gmail.com>
Wed, 11 May 2022 02:27:21 +0000 (02:27 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 22 Jun 2022 16:21:49 +0000 (16:21 +0000)
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 <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dexter Ouyang <kkhaike@gmail.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Alex Rakoczy <alex@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

misc/cgo/test/testdata/issue52611.go [new file with mode: 0644]
misc/cgo/test/testdata/issue52611a/a.go [new file with mode: 0644]
misc/cgo/test/testdata/issue52611a/b.go [new file with mode: 0644]
misc/cgo/test/testdata/issue52611b/a.go [new file with mode: 0644]
misc/cgo/test/testdata/issue52611b/b.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

diff --git a/misc/cgo/test/testdata/issue52611.go b/misc/cgo/test/testdata/issue52611.go
new file mode 100644 (file)
index 0000000..32d2240
--- /dev/null
@@ -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 (file)
index 0000000..0764688
--- /dev/null
@@ -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 (file)
index 0000000..74a50c5
--- /dev/null
@@ -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 (file)
index 0000000..730b52f
--- /dev/null
@@ -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 (file)
index 0000000..d304175
--- /dev/null
@@ -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)
+}
index 4dff5e2b1c7abaea94dc27068d97b3c128170cd8..3cb01ba382f255f72ede91f6aafbd6b149b0ffbe 100644 (file)
@@ -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.