mirror of
https://github.com/golang/go.git
synced 2025-05-07 16:43:03 +00:00
go/types, types2: require iterator yield to return bool (work-around)
The original implementation of the type checkers accepted any boolean result type for yield, but the compiler's front-end had a problem with it (#71131). As a temporary fix (for 1.24), adjust the type checkers to insist on the spec's literal wording and avoid the compiler panic. Fixes #71131. For #71164. Change-Id: Ie25f9a892e58b5e489d399b0bce2d0af55dc3c48 Reviewed-on: https://go-review.googlesource.com/c/go/+/640599 Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
parent
54693a81fd
commit
c9afcbade7
@ -1057,9 +1057,14 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
|
|||||||
return bad("func must be func(yield func(...) bool): argument is not func")
|
return bad("func must be func(yield func(...) bool): argument is not func")
|
||||||
case cb.Params().Len() > 2:
|
case cb.Params().Len() > 2:
|
||||||
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
|
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
|
||||||
case cb.Results().Len() != 1 || !isBoolean(cb.Results().At(0).Type()):
|
case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
|
||||||
|
// see go.dev/issues/71131, go.dev/issues/71164
|
||||||
|
if cb.Results().Len() == 1 && isBoolean(cb.Results().At(0).Type()) {
|
||||||
|
return bad("func must be func(yield func(...) bool): yield func returns user-defined boolean, not bool")
|
||||||
|
} else {
|
||||||
return bad("func must be func(yield func(...) bool): yield func does not return bool")
|
return bad("func must be func(yield func(...) bool): yield func does not return bool")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
assert(cb.Recv() == nil)
|
assert(cb.Recv() == nil)
|
||||||
// determine key and value types, if any
|
// determine key and value types, if any
|
||||||
if cb.Params().Len() >= 1 {
|
if cb.Params().Len() >= 1 {
|
||||||
|
@ -21,6 +21,7 @@ var Unsafe *Package
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
universeIota Object
|
universeIota Object
|
||||||
|
universeBool Type
|
||||||
universeByte Type // uint8 alias, but has name "byte"
|
universeByte Type // uint8 alias, but has name "byte"
|
||||||
universeRune Type // int32 alias, but has name "rune"
|
universeRune Type // int32 alias, but has name "rune"
|
||||||
universeAnyNoAlias *TypeName
|
universeAnyNoAlias *TypeName
|
||||||
@ -275,6 +276,7 @@ func init() {
|
|||||||
defPredeclaredFuncs()
|
defPredeclaredFuncs()
|
||||||
|
|
||||||
universeIota = Universe.Lookup("iota")
|
universeIota = Universe.Lookup("iota")
|
||||||
|
universeBool = Universe.Lookup("bool").Type()
|
||||||
universeByte = Universe.Lookup("byte").Type()
|
universeByte = Universe.Lookup("byte").Type()
|
||||||
universeRune = Universe.Lookup("rune").Type()
|
universeRune = Universe.Lookup("rune").Type()
|
||||||
universeError = Universe.Lookup("error").Type()
|
universeError = Universe.Lookup("error").Type()
|
||||||
|
@ -1075,9 +1075,14 @@ func rangeKeyVal(typ Type, allowVersion func(goVersion) bool) (key, val Type, ca
|
|||||||
return bad("func must be func(yield func(...) bool): argument is not func")
|
return bad("func must be func(yield func(...) bool): argument is not func")
|
||||||
case cb.Params().Len() > 2:
|
case cb.Params().Len() > 2:
|
||||||
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
|
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
|
||||||
case cb.Results().Len() != 1 || !isBoolean(cb.Results().At(0).Type()):
|
case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
|
||||||
|
// see go.dev/issues/71131, go.dev/issues/71164
|
||||||
|
if cb.Results().Len() == 1 && isBoolean(cb.Results().At(0).Type()) {
|
||||||
|
return bad("func must be func(yield func(...) bool): yield func returns user-defined boolean, not bool")
|
||||||
|
} else {
|
||||||
return bad("func must be func(yield func(...) bool): yield func does not return bool")
|
return bad("func must be func(yield func(...) bool): yield func does not return bool")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
assert(cb.Recv() == nil)
|
assert(cb.Recv() == nil)
|
||||||
// determine key and value types, if any
|
// determine key and value types, if any
|
||||||
if cb.Params().Len() >= 1 {
|
if cb.Params().Len() >= 1 {
|
||||||
|
@ -24,6 +24,7 @@ var Unsafe *Package
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
universeIota Object
|
universeIota Object
|
||||||
|
universeBool Type
|
||||||
universeByte Type // uint8 alias, but has name "byte"
|
universeByte Type // uint8 alias, but has name "byte"
|
||||||
universeRune Type // int32 alias, but has name "rune"
|
universeRune Type // int32 alias, but has name "rune"
|
||||||
universeAnyNoAlias *TypeName
|
universeAnyNoAlias *TypeName
|
||||||
@ -278,6 +279,7 @@ func init() {
|
|||||||
defPredeclaredFuncs()
|
defPredeclaredFuncs()
|
||||||
|
|
||||||
universeIota = Universe.Lookup("iota")
|
universeIota = Universe.Lookup("iota")
|
||||||
|
universeBool = Universe.Lookup("bool").Type()
|
||||||
universeByte = Universe.Lookup("byte").Type()
|
universeByte = Universe.Lookup("byte").Type()
|
||||||
universeRune = Universe.Lookup("rune").Type()
|
universeRune = Universe.Lookup("rune").Type()
|
||||||
universeError = Universe.Lookup("error").Type()
|
universeError = Universe.Lookup("error").Type()
|
||||||
|
15
src/internal/types/testdata/fixedbugs/issue71131.go
vendored
Normal file
15
src/internal/types/testdata/fixedbugs/issue71131.go
vendored
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// 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 p
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
type Bool bool
|
||||||
|
for range func /* ERROR "yield func returns user-defined boolean, not bool" */ (func() Bool) {} {
|
||||||
|
}
|
||||||
|
for range func /* ERROR "yield func returns user-defined boolean, not bool" */ (func(int) Bool) {} {
|
||||||
|
}
|
||||||
|
for range func /* ERROR "yield func returns user-defined boolean, not bool" */ (func(int, string) Bool) {} {
|
||||||
|
}
|
||||||
|
}
|
2
src/internal/types/testdata/spec/range.go
vendored
2
src/internal/types/testdata/spec/range.go
vendored
@ -5,7 +5,7 @@
|
|||||||
package p
|
package p
|
||||||
|
|
||||||
type MyInt int32
|
type MyInt int32
|
||||||
type MyBool bool
|
type MyBool = bool // TODO(gri) remove alias declaration - see go.dev/issues/71131, go.dev/issues/71164
|
||||||
type MyString string
|
type MyString string
|
||||||
type MyFunc1 func(func(int) bool)
|
type MyFunc1 func(func(int) bool)
|
||||||
type MyFunc2 func(int) bool
|
type MyFunc2 func(int) bool
|
||||||
|
Loading…
x
Reference in New Issue
Block a user