1
0
mirror of https://github.com/golang/go synced 2024-07-05 09:50:19 +00:00

cmd/cgo: dont override declared struct type

Fixes #52611

Change-Id: I835df8d6a98a37482446ec00af768c96fd8ee4fe
GitHub-Last-Rev: ea54dd69ee
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>
This commit is contained in:
kkHAIKE 2022-05-11 02:27:21 +00:00 committed by Gopher Robot
parent 92c9b81447
commit f571518139
6 changed files with 72 additions and 0 deletions

13
misc/cgo/test/testdata/issue52611.go vendored Normal file
View File

@ -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"
)

16
misc/cgo/test/testdata/issue52611a/a.go vendored Normal file
View File

@ -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)
}

11
misc/cgo/test/testdata/issue52611a/b.go vendored Normal file
View File

@ -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)
}

11
misc/cgo/test/testdata/issue52611b/a.go vendored Normal file
View File

@ -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)
}

16
misc/cgo/test/testdata/issue52611b/b.go vendored Normal file
View File

@ -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)
}

View 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.