From: Ian Lance Taylor Date: Wed, 17 Dec 2025 00:00:36 +0000 (-0800) Subject: cmd/cgo: don't emit C local if it is not used X-Git-Tag: go1.26rc2~7^2~67 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8c28ab936a;p=gostls13.git cmd/cgo: don't emit C local if it is not used Fixes #76861 Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8 Reviewed-on: https://go-review.googlesource.com/c/go/+/730600 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Joe Richey Auto-Submit: Ian Lance Taylor --- diff --git a/src/cmd/cgo/internal/test/issue76861.go b/src/cmd/cgo/internal/test/issue76861.go new file mode 100644 index 0000000000..225e2acc3f --- /dev/null +++ b/src/cmd/cgo/internal/test/issue76861.go @@ -0,0 +1,12 @@ +// Copyright 2025 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. + +//go:build cgo + +package cgotest + +// Issue 43639: No runtime test needed, make sure package +// cmd/cgo/internal/test/issue76861 compiles without error. + +import _ "cmd/cgo/internal/test/issue76861" diff --git a/src/cmd/cgo/internal/test/issue76861/a.go b/src/cmd/cgo/internal/test/issue76861/a.go new file mode 100644 index 0000000000..18a7bda490 --- /dev/null +++ b/src/cmd/cgo/internal/test/issue76861/a.go @@ -0,0 +1,13 @@ +// Copyright 2025 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 issue76861 + +// #cgo CFLAGS: -Wall -Werror +// void issue76861(void) {} +import "C" + +func Issue76861() { + C.issue76861() +} diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index dc1e5b29e5..ac2ce8fd0d 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -783,13 +783,13 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { // We're trying to write a gcc struct that matches gc's layout. // Use packed attribute to force no padding in this struct in case // gcc has different packing requirements. - fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute()) - if n.FuncType.Result != nil { - // Save the stack top for use below. - fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n") - } tr := n.FuncType.Result + if (n.Kind != "macro" && len(n.FuncType.Params) > 0) || tr != nil { + fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute()) + } if tr != nil { + // Save the stack top for use below. + fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n") fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n") } fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n") @@ -819,7 +819,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) { fmt.Fprintf(fgcc, "\t_cgo_errno = errno;\n") } fmt.Fprintf(fgcc, "\t_cgo_tsan_release();\n") - if n.FuncType.Result != nil { + if tr != nil { // The cgo call may have caused a stack copy (via a callback). // Adjust the return value pointer appropriately. fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")