Skip to content

Commit 815e0f9

Browse files
[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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]> (cherry picked from commit a371bc2) Reviewed-on: https://go-review.googlesource.com/128155
1 parent 4c77cb3 commit 815e0f9

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

misc/cgo/test/issue26430.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Issue 26430: incomplete typedef leads to inconsistent typedefs error.
6+
// No runtime test; just make sure it compiles.
7+
8+
package cgotest
9+
10+
import _ "./issue26430"

misc/cgo/test/issue26430/a.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
// typedef struct S ST;
8+
// static ST* F() { return 0; }
9+
import "C"
10+
11+
func F1() {
12+
C.F()
13+
}

misc/cgo/test/issue26430/b.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
// typedef struct S ST;
8+
// struct S { int f; };
9+
import "C"
10+
11+
func F2(p *C.ST) {
12+
p.f = 1
13+
}

src/cmd/cgo/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ func (p *Package) Record(f *File) {
388388
for k, v := range f.Name {
389389
if p.Name[k] == nil {
390390
p.Name[k] = v
391+
} else if p.incompleteTypedef(p.Name[k].Type) {
392+
p.Name[k] = v
393+
} else if p.incompleteTypedef(v.Type) {
394+
// Nothing to do.
391395
} else if !reflect.DeepEqual(p.Name[k], v) {
392396
error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
393397
}
@@ -400,3 +404,9 @@ func (p *Package) Record(f *File) {
400404
}
401405
p.Decl = append(p.Decl, f.AST.Decls...)
402406
}
407+
408+
// incompleteTypedef reports whether t appears to be an incomplete
409+
// typedef definition.
410+
func (p *Package) incompleteTypedef(t *Type) bool {
411+
return t == nil || (t.Size == 0 && t.Align == -1)
412+
}

0 commit comments

Comments
 (0)