-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/cgo: reject attempts to declare methods on C types
This change causes cgo to emit an error (with the same message as the compiler) when it encounters a declaration of a method whose receiver type is C.T or *C.T. Conceptually, C is another package, but unfortunately the desugaring of C.T is a type within the same package, causing the previous behavior to accept invalid input. It is likely that at least some users are intentionally exploiting this behavior, so this may break their build. We should mention it in the release notes. Fixes #57926 Change-Id: I513cffb7e13bc93d08a07b7e61301ac1762fd42d Reviewed-on: https://go-review.googlesource.com/c/go/+/490819 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
- Loading branch information
Showing
2 changed files
with
79 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[short] skip | ||
[!cgo] skip | ||
|
||
# Test that cgo rejects attempts to declare methods | ||
# on the types C.T or *C.T; see issue #57926. | ||
|
||
! go build | ||
stderr 'cannot define new methods on non-local type C.T' | ||
stderr 'cannot define new methods on non-local type \*C.T' | ||
! stderr 'Alias' | ||
|
||
-- go.mod -- | ||
module example.com | ||
go 1.12 | ||
|
||
-- a.go -- | ||
package a | ||
|
||
/* | ||
typedef int T; | ||
*/ | ||
import "C" | ||
|
||
func (C.T) f() {} | ||
func (recv *C.T) g() {} | ||
|
||
// The check is more education than enforcement, | ||
// and is easily defeated using a type alias. | ||
type Alias = C.T | ||
func (Alias) h() {} | ||
func (*Alias) i() {} |