diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-07-17 17:36:15 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-08-07 05:18:07 +0000 |
commit | 815e0f9755e6e2a0db9c36db1af6550c1f750d8e (patch) | |
tree | c48dd70875bb2b09b26de65010a2191294095d1a | |
parent | 4c77cb3087363f2ebab565bd2b46935749ca7dd2 (diff) | |
download | go-git-815e0f9755e6e2a0db9c36db1af6550c1f750d8e.tar.gz |
[release-branch.go1.10] cmd/cgo: don't report inconsistency error for incomplete typedef
In CLs 122575 and 123177 the cgo tool started explicitly looking up
typedefs. When there are two Go files using import "C", and the first
one has an incomplete typedef and the second one has a complete
version of the same typedef, then we will now record a version of the
first typedef which will not match the recorded version of the second
typedef, producing an "inconsistent definitions" error. Fix this by
silently merging incomplete typedefs with complete ones.
Fixes #26430
Change-Id: I9e629228783b866dd29b5c3a31acd48f6e410a2d
Reviewed-on: https://go-review.googlesource.com/124575
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
(cherry picked from commit a371bc2dfdf1fe4166c83be1177fbecb03d8da53)
Reviewed-on: https://go-review.googlesource.com/128155
-rw-r--r-- | misc/cgo/test/issue26430.go | 10 | ||||
-rw-r--r-- | misc/cgo/test/issue26430/a.go | 13 | ||||
-rw-r--r-- | misc/cgo/test/issue26430/b.go | 13 | ||||
-rw-r--r-- | src/cmd/cgo/main.go | 10 |
4 files changed, 46 insertions, 0 deletions
diff --git a/misc/cgo/test/issue26430.go b/misc/cgo/test/issue26430.go new file mode 100644 index 0000000000..3ad5420989 --- /dev/null +++ b/misc/cgo/test/issue26430.go @@ -0,0 +1,10 @@ +// Copyright 2018 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 26430: incomplete typedef leads to inconsistent typedefs error. +// No runtime test; just make sure it compiles. + +package cgotest + +import _ "./issue26430" diff --git a/misc/cgo/test/issue26430/a.go b/misc/cgo/test/issue26430/a.go new file mode 100644 index 0000000000..fbaa46b1e8 --- /dev/null +++ b/misc/cgo/test/issue26430/a.go @@ -0,0 +1,13 @@ +// Copyright 2018 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 a + +// typedef struct S ST; +// static ST* F() { return 0; } +import "C" + +func F1() { + C.F() +} diff --git a/misc/cgo/test/issue26430/b.go b/misc/cgo/test/issue26430/b.go new file mode 100644 index 0000000000..a7c527cde3 --- /dev/null +++ b/misc/cgo/test/issue26430/b.go @@ -0,0 +1,13 @@ +// Copyright 2018 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 a + +// typedef struct S ST; +// struct S { int f; }; +import "C" + +func F2(p *C.ST) { + p.f = 1 +} diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 0c1c863a7a..80502262a5 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -388,6 +388,10 @@ func (p *Package) Record(f *File) { for k, v := range f.Name { if p.Name[k] == nil { p.Name[k] = v + } else if p.incompleteTypedef(p.Name[k].Type) { + p.Name[k] = v + } else if p.incompleteTypedef(v.Type) { + // Nothing to do. } else if !reflect.DeepEqual(p.Name[k], v) { error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k)) } @@ -400,3 +404,9 @@ func (p *Package) Record(f *File) { } p.Decl = append(p.Decl, f.AST.Decls...) } + +// incompleteTypedef reports whether t appears to be an incomplete +// typedef definition. +func (p *Package) incompleteTypedef(t *Type) bool { + return t == nil || (t.Size == 0 && t.Align == -1) +} |