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 <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>
diff --git a/misc/cgo/test/testdata/issue52611.go b/misc/cgo/test/testdata/issue52611.go
new file mode 100644
index 0000000..32d2240
--- /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 0000000..0764688
--- /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 0000000..74a50c5
--- /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 0000000..730b52f5
--- /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 0000000..d304175
--- /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 4dff5e2..3cb01ba 100644
--- a/src/cmd/cgo/gcc.go
+++ b/src/cmd/cgo/gcc.go
@@ -2551,6 +2551,11 @@
 		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.