]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: remove another coreType call in type checking range clause
authorRobert Griesemer <gri@golang.org>
Mon, 24 Feb 2025 22:47:30 +0000 (14:47 -0800)
committerGopher Robot <gobot@golang.org>
Tue, 25 Feb 2025 14:23:44 +0000 (06:23 -0800)
For #70128.

Change-Id: I5949bccbfaaebc435ae8ac7c70580d9740de6f00
Reviewed-on: https://go-review.googlesource.com/c/go/+/652136
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/stmt.go
src/go/types/stmt.go
src/internal/types/testdata/spec/range.go

index 3f5412fbddea04bbc4ca70211777fb5ca754f864..3cd29fbb4cddbcb491a4fe7662cc635f1d924f0c 100644 (file)
@@ -1040,10 +1040,15 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
                }
                assert(typ.Recv() == nil)
                // check iterator argument type
-               cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
+               var cause2 string
+               cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
                switch {
                case cb == nil:
-                       return bad("func must be func(yield func(...) bool): argument is not func")
+                       if cause2 != "" {
+                               return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", cause2))
+                       } else {
+                               return bad("func must be func(yield func(...) bool): argument is not func")
+                       }
                case cb.Params().Len() > 2:
                        return bad("func must be func(yield func(...) bool): yield func has too many parameters")
                case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
index 5426c5e719d799ceeecf3733c46918205ab06370..983467e36acef7517104779ca93c8ab7022b1c49 100644 (file)
@@ -1058,10 +1058,15 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
                }
                assert(typ.Recv() == nil)
                // check iterator argument type
-               cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
+               var cause2 string
+               cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
                switch {
                case cb == nil:
-                       return bad("func must be func(yield func(...) bool): argument is not func")
+                       if cause2 != "" {
+                               return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", cause2))
+                       } else {
+                               return bad("func must be func(yield func(...) bool): argument is not func")
+                       }
                case cb.Params().Len() > 2:
                        return bad("func must be func(yield func(...) bool): yield func has too many parameters")
                case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):
index 26406fca8adb2aca2ec2ab024a55bd8b1e25da20..d77511ece0575474446dabc837fa5e3718c262a1 100644 (file)
@@ -164,6 +164,11 @@ func _[T ~func(func(int) bool)](x T) {
        }
 }
 
+func _[T func() bool | func(int) bool]() {
+       for range func /* ERROR "func must be func(yield func(...) bool): in yield type, func() bool and func(int) bool have different underlying types" */ (T) {} {
+       }
+}
+
 // go.dev/issue/65236
 
 func seq0(func() bool) {}