Skip to content

Commit 7194caf

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: remove another coreType call in type checking range clause
For #70128. Change-Id: I5949bccbfaaebc435ae8ac7c70580d9740de6f00 Reviewed-on: https://go-review.googlesource.com/c/go/+/652136 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 549a88f commit 7194caf

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/cmd/compile/internal/types2/stmt.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1040,10 +1040,15 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10401040
}
10411041
assert(typ.Recv() == nil)
10421042
// check iterator argument type
1043-
cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
1043+
var cause2 string
1044+
cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
10441045
switch {
10451046
case cb == nil:
1046-
return bad("func must be func(yield func(...) bool): argument is not func")
1047+
if cause2 != "" {
1048+
return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", cause2))
1049+
} else {
1050+
return bad("func must be func(yield func(...) bool): argument is not func")
1051+
}
10471052
case cb.Params().Len() > 2:
10481053
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
10491054
case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):

src/go/types/stmt.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1058,10 +1058,15 @@ func rangeKeyVal(check *Checker, orig Type, allowVersion func(goVersion) bool) (
10581058
}
10591059
assert(typ.Recv() == nil)
10601060
// check iterator argument type
1061-
cb, _ := coreType(typ.Params().At(0).Type()).(*Signature)
1061+
var cause2 string
1062+
cb, _ := sharedUnder(check, typ.Params().At(0).Type(), &cause2).(*Signature)
10621063
switch {
10631064
case cb == nil:
1064-
return bad("func must be func(yield func(...) bool): argument is not func")
1065+
if cause2 != "" {
1066+
return bad(check.sprintf("func must be func(yield func(...) bool): in yield type, %s", cause2))
1067+
} else {
1068+
return bad("func must be func(yield func(...) bool): argument is not func")
1069+
}
10651070
case cb.Params().Len() > 2:
10661071
return bad("func must be func(yield func(...) bool): yield func has too many parameters")
10671072
case cb.Results().Len() != 1 || !Identical(cb.Results().At(0).Type(), universeBool):

src/internal/types/testdata/spec/range.go

+5
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ func _[T ~func(func(int) bool)](x T) {
164164
}
165165
}
166166

167+
func _[T func() bool | func(int) bool]() {
168+
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) {} {
169+
}
170+
}
171+
167172
// go.dev/issue/65236
168173

169174
func seq0(func() bool) {}

0 commit comments

Comments
 (0)