[go] cmd/cgo: don't emit C local if it is not used

0 views
Skip to first unread message

Ian Lance Taylor (Gerrit)

unread,
Dec 16, 2025, 7:01:27 PM (7 hours ago) Dec 16
to David Chase, goph...@pubsubhelper.golang.org, Ian Lance Taylor, golang-co...@googlegroups.com
Attention needed from David Chase

Ian Lance Taylor has uploaded the change for review

Ian Lance Taylor would like David Chase to review this change.

Commit message

cmd/cgo: don't emit C local if it is not used

Fixes #76861
Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8

Change diff

diff --git a/src/cmd/cgo/internal/test/issue76861.go b/src/cmd/cgo/internal/test/issue76861.go
new file mode 100644
index 0000000..225e2ac
--- /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 0000000..18a7bda
--- /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 dc1e5b2..ac2ce8f 100644
--- a/src/cmd/cgo/out.go
+++ b/src/cmd/cgo/out.go
@@ -783,13 +783,13 @@
// 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 {
+ 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")
- }
- tr := n.FuncType.Result
- if tr != nil {
fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
}
fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
@@ -819,7 +819,7 @@
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")

Change information

Files:
  • A src/cmd/cgo/internal/test/issue76861.go
  • A src/cmd/cgo/internal/test/issue76861/a.go
  • M src/cmd/cgo/out.go
Change size: S
Delta: 3 files changed, 31 insertions(+), 6 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • David Chase
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Gerrit-Change-Number: 730600
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Attention: David Chase <drc...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Joe Richey (Gerrit)

unread,
Dec 16, 2025, 7:17:22 PM (7 hours ago) Dec 16
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Go LUCI, David Chase, golang-co...@googlegroups.com
Attention needed from David Chase and Ian Lance Taylor

Joe Richey voted and added 1 comment

Votes added by Joe Richey

Code-Review+1

1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Joe Richey . resolved

LGTM. The change and tests look good.

Thanks Ian for the quick fix!

Open in Gerrit

Related details

Attention is currently required from:
  • David Chase
  • Ian Lance Taylor
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Gerrit-Change-Number: 730600
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Joe Richey <joer...@google.com>
Gerrit-Attention: David Chase <drc...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Wed, 17 Dec 2025 00:17:18 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

David Chase (Gerrit)

unread,
Dec 16, 2025, 9:41:49 PM (5 hours ago) Dec 16
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, Go LUCI, Joe Richey, golang-co...@googlegroups.com
Attention needed from Ian Lance Taylor

David Chase voted Code-Review+2

Code-Review+2
Open in Gerrit

Related details

Attention is currently required from:
  • Ian Lance Taylor
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Gerrit-Change-Number: 730600
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Joe Richey <joer...@google.com>
Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
Gerrit-Comment-Date: Wed, 17 Dec 2025 02:41:45 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
open
diffy

Ian Lance Taylor (Gerrit)

unread,
12:26 AM (2 hours ago) 12:26 AM
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, David Chase, Go LUCI, Joe Richey, golang-co...@googlegroups.com

Ian Lance Taylor voted Auto-Submit+1

Auto-Submit+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Gerrit-Change-Number: 730600
Gerrit-PatchSet: 1
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Joe Richey <joer...@google.com>
Gerrit-Comment-Date: Wed, 17 Dec 2025 05:26:24 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
12:28 AM (2 hours ago) 12:28 AM
to Ian Lance Taylor, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, David Chase, Go LUCI, Joe Richey, golang-co...@googlegroups.com

Gopher Robot submitted the change

Change information

Commit message:
cmd/cgo: don't emit C local if it is not used

Fixes #76861
Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Reviewed-by: David Chase <drc...@google.com>
Reviewed-by: Joe Richey <joer...@google.com>
Auto-Submit: Ian Lance Taylor <ia...@golang.org>
Files:
  • A src/cmd/cgo/internal/test/issue76861.go
  • A src/cmd/cgo/internal/test/issue76861/a.go
  • M src/cmd/cgo/out.go
Change size: S
Delta: 3 files changed, 31 insertions(+), 6 deletions(-)
Branch: refs/heads/master
Submit Requirements:
  • requirement satisfiedCode-Review: +2 by David Chase, +1 by Joe Richey
  • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
Open in Gerrit
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: merged
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Icc8452e48ed736e8240f8afea18637c33b8e3ef8
Gerrit-Change-Number: 730600
Gerrit-PatchSet: 2
Gerrit-Owner: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages