From: Ian Lance Taylor Date: Fri, 31 May 2024 05:29:35 +0000 (-0700) Subject: cmd/cgo: fail on v, err := C.fn when fn is a builtin function X-Git-Tag: go1.23rc2~2^2~61 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d73a8a206a;p=gostls13.git cmd/cgo: fail on v, err := C.fn when fn is a builtin function 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 Reviewed-by: Damien Neil Reviewed-by: Cherry Mui LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index b596477b0a..6c23e59adf 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -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. diff --git a/src/cmd/cgo/internal/testerrors/errors_test.go b/src/cmd/cgo/internal/testerrors/errors_test.go index 07556007a8..eddfb6583b 100644 --- a/src/cmd/cgo/internal/testerrors/errors_test.go +++ b/src/cmd/cgo/internal/testerrors/errors_test.go @@ -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 index 0000000000..4f80de165e --- /dev/null +++ b/src/cmd/cgo/internal/testerrors/testdata/issue67707.go @@ -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 +}