diff options
| author | Robert Griesemer <gri@google.com> | 2026-03-31 15:38:36 -0700 |
|---|---|---|
| committer | Robert Griesemer <gri@google.com> | 2026-04-01 16:39:05 -0700 |
| commit | 238d7bddeba8436ff6eea8f9216978afb7d6e7cc (patch) | |
| tree | 0c63dbf6ba5151e25893f0ea2c6e6230e459db56 /src | |
| parent | 9301a3eab246d3ddb49415cfd892c00382189264 (diff) | |
| download | go-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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/compile/internal/types2/range.go | 3 | ||||
| -rw-r--r-- | src/go/types/range.go | 3 | ||||
| -rw-r--r-- | src/internal/types/testdata/fixedbugs/issue78483.go | 15 |
3 files changed, 21 insertions, 0 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) {} |
