]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: fail on v, err := C.fn when fn is a builtin function
authorIan Lance Taylor <iant@golang.org>
Fri, 31 May 2024 05:29:35 +0000 (22:29 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 21 Jun 2024 19:12:53 +0000 (19:12 +0000)
We were already checking for _CMalloc, but in fact none of the
builtin functions support returning an error.

Fixes #67707

Change-Id: I0ee432a9f13ace472c3f36f641efc7d18eda0631
Reviewed-on: https://go-review.googlesource.com/c/go/+/589575
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/cgo/gcc.go
src/cmd/cgo/internal/testerrors/errors_test.go
src/cmd/cgo/internal/testerrors/testdata/issue67707.go [new file with mode: 0644]

index b596477b0a65f1054d31dc4beb81ca8a5ebe53fe..6c23e59adf19eb6e49d789de6b6ef722747187f7 100644 (file)
@@ -1601,8 +1601,8 @@ func (p *Package) rewriteName(f *File, r *Ref, addPosition bool) ast.Expr {
                        break
                }
                if r.Context == ctxCall2 {
-                       if r.Name.Go == "_CMalloc" {
-                               error_(r.Pos(), "no two-result form for C.malloc")
+                       if builtinDefs[r.Name.Go] != "" {
+                               error_(r.Pos(), "no two-result form for C.%s", r.Name.Go)
                                break
                        }
                        // Invent new Name for the two-result function.
index 07556007a84fe88b101bc84f7edd546db5f1ecb2..eddfb6583b9e7715fb4a236af587b4f4b0a4f60d 100644 (file)
@@ -127,6 +127,7 @@ func TestReportsTypeErrors(t *testing.T) {
                "issue33061.go",
                "issue50710.go",
                "issue67517.go",
+               "issue67707.go",
        } {
                check(t, file)
        }
diff --git a/src/cmd/cgo/internal/testerrors/testdata/issue67707.go b/src/cmd/cgo/internal/testerrors/testdata/issue67707.go
new file mode 100644 (file)
index 0000000..4f80de1
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2024 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 p
+
+import "C"
+
+func F() *C.char {
+       s, err := C.CString("hi") // ERROR HERE: no two-result form
+       if err != nil {
+               println(err)
+       }
+       return s
+}