aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@google.com>2026-03-31 15:38:36 -0700
committerRobert Griesemer <gri@google.com>2026-04-01 16:39:05 -0700
commit238d7bddeba8436ff6eea8f9216978afb7d6e7cc (patch)
tree0c63dbf6ba5151e25893f0ea2c6e6230e459db56
parent9301a3eab246d3ddb49415cfd892c00382189264 (diff)
downloadgo-238d7bddeba8436ff6eea8f9216978afb7d6e7cc.tar.xz
go/types, types2: in range-over-func, the yield function cannot be variadic
Fixes #78483. For #78314. Change-Id: If83983c0bf79840aa02dc0d2fa8945f5e8b4e969 Reviewed-on: https://go-review.googlesource.com/c/go/+/761682 Auto-Submit: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jakub Ciolek <jakub@ciolek.dev> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com>
-rw-r--r--src/cmd/compile/internal/types2/range.go3
-rw-r--r--src/go/types/range.go3
-rw-r--r--src/internal/types/testdata/fixedbugs/issue78483.go15
-rw-r--r--test/fixedbugs/issue78314.go4
4 files changed, 23 insertions, 2 deletions
diff --git a/src/cmd/compile/internal/types2/range.go b/src/cmd/compile/internal/types2/range.go
index ae6524d0e4..f7ecbb4c76 100644
--- a/src/cmd/compile/internal/types2/range.go
+++ b/src/cmd/compile/internal/types2/range.go
@@ -240,6 +240,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
return bad("requires go1.23 or later")
}
// check iterator arity
+ // TODO(gri) error messages could be less verbose (consider rangeStmt error and cause returned here)
switch {
case typ.Params().Len() != 1:
return bad("func must be func(yield func(...) bool): wrong argument count")
@@ -266,6 +267,8 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
} else {
return bad("func must be func(yield func(...) bool): yield func does not return bool")
}
+ case cb.Variadic():
+ return bad(check.sprintf("yield func of type %s cannot be variadic", cb))
}
assert(cb.Recv() == nil)
// determine key and value types, if any
diff --git a/src/go/types/range.go b/src/go/types/range.go
index 208da64ba2..8eceb4d787 100644
--- a/src/go/types/range.go
+++ b/src/go/types/range.go
@@ -243,6 +243,7 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
return bad("requires go1.23 or later")
}
// check iterator arity
+ // TODO(gri) error messages could be less verbose (consider rangeStmt error and cause returned here)
switch {
case typ.Params().Len() != 1:
return bad("func must be func(yield func(...) bool): wrong argument count")
@@ -269,6 +270,8 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
} else {
return bad("func must be func(yield func(...) bool): yield func does not return bool")
}
+ case cb.Variadic():
+ return bad(check.sprintf("yield func of type %s cannot be variadic", cb))
}
assert(cb.Recv() == nil)
// determine key and value types, if any
diff --git a/src/internal/types/testdata/fixedbugs/issue78483.go b/src/internal/types/testdata/fixedbugs/issue78483.go
new file mode 100644
index 0000000000..7f4bc01e22
--- /dev/null
+++ b/src/internal/types/testdata/fixedbugs/issue78483.go
@@ -0,0 +1,15 @@
+// Copyright 2026 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
+
+func _() {
+ for range f1 /* ERROR "cannot range over f1 (value of type func(func(...int) bool)): yield func of type func(...int) bool cannot be variadic" */ {
+ }
+ for range f2 /* ERROR "cannot range over f2 (value of type func(func(int, ...int) bool)): yield func of type func(int, ...int) bool cannot be variadic" */ {
+ }
+}
+
+func f1(func(...int) bool) {}
+func f2(func(int, ...int) bool) {}
diff --git a/test/fixedbugs/issue78314.go b/test/fixedbugs/issue78314.go
index 18deba930f..f643938400 100644
--- a/test/fixedbugs/issue78314.go
+++ b/test/fixedbugs/issue78314.go
@@ -1,4 +1,4 @@
-// compile
+// errorcheck
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
@@ -7,7 +7,7 @@
package main
func F() {
- for range (func(func(int, ...string) bool))(nil) {
+ for range (func(func(int, ...string) bool))(nil) { // ERROR "cannot be variadic"
}
}