xie cui has uploaded this change for review.
cmd/go: compiler accepts invalid declaration of methods on aliases to C types
Fixes #60725
Change-Id: Iae49d04e4c432f28f6c6a011237a1333cfab6c8b
---
M src/cmd/go/internal/work/gc.go
M src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
A src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
3 files changed, 46 insertions(+), 7 deletions(-)
diff --git a/src/cmd/go/internal/work/gc.go b/src/cmd/go/internal/work/gc.go
index 6043ad5..119321d 100644
--- a/src/cmd/go/internal/work/gc.go
+++ b/src/cmd/go/internal/work/gc.go
@@ -177,6 +177,11 @@
}
args := []any{cfg.BuildToolexec, base.Tool("compile"), "-o", ofile, "-trimpath", a.trimpath(), defaultGcFlags, gcflags}
+
+ if len(p.CgoFiles) > 0 {
+ cgoIsLocalFile := "cgo.islocal"
+ args = append(args, "-islocaljsonfile="+objdir+cgoIsLocalFile)
+ }
if p.Internal.LocalPrefix == "" {
args = append(args, "-nolocalimports")
} else {
diff --git a/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt b/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
index 81ef850..b6abff0 100644
--- a/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
+++ b/src/cmd/go/testdata/script/cgo_badmethod_issue57926.txt
@@ -7,7 +7,6 @@
! 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
@@ -23,9 +22,3 @@
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() {}
diff --git a/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt b/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
new file mode 100644
index 0000000..5c77315
--- /dev/null
+++ b/src/cmd/go/testdata/script/cgo_badmethod_issue60725.txt
@@ -0,0 +1,41 @@
+[short] skip
+[!cgo] skip
+
+# Test that cgo rejects attempts to declare methods
+
+! go build ./a
+stderr 'cannot define new methods on non-local type Alias'
+stderr 'cannot define new methods on non-local type \*Alias'
+
+
+! go build ./b
+stderr 'cannot define new methods on non-local type foo'
+-- go.mod --
+module example.com
+go 1.12
+
+-- a/a.go --
+package a
+
+/*
+typedef int T;
+*/
+import "C"
+
+type Alias = C.T
+func (Alias) h() {}
+func (*Alias) i() {}
+
+-- b/b1.go --
+package b
+
+/*
+typedef int T;
+*/
+import "C"
+type foo = C.T
+
+-- b/b2.go --
+package b
+
+func (foo) h() {}
\ No newline at end of file
To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Bryan Mills, Michael Matloob.
1 comment:
Patchset:
this cl is going to be abandon. please refer to https://go-review.googlesource.com/c/go/+/503596.
To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.
xie cui restored this change.
To view, visit change 503397. To unsubscribe, or for help writing mail filters, visit settings.