1
0
mirror of https://github.com/golang/go synced 2024-11-22 02:44:39 -07:00

cmd/compile: fix typecheck range over rune literal

With range over int, the rune literal in range expression will be left
as untyped rune, but idealType is not handling this case, causing ICE.

Fixing this by setting the concrete type for untyped rune expresison.

Fixes #64471

Change-Id: I07a151c54ea1d9e1b92e4d96cdfb6e73dca13862
Reviewed-on: https://go-review.googlesource.com/c/go/+/546296
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Cuong Manh Le 2023-12-01 00:16:24 +07:00 committed by Gopher Robot
parent 446a5dcf5a
commit fbfe62bc80
2 changed files with 10 additions and 0 deletions

View File

@ -99,6 +99,8 @@ func idealType(tv syntax.TypeAndValue) types2.Type {
typ = types2.Typ[types2.Bool] // expression in "if" or "for" condition
case types2.UntypedString:
typ = types2.Typ[types2.String] // argument to "append" or "copy" calls
case types2.UntypedRune:
typ = types2.Typ[types2.Int32] // range over rune
default:
return nil
}

View File

@ -74,9 +74,17 @@ func testint4() {
}
}
// Issue #64471.
func testint5() {
for i := range 'a' {
var _ *rune = &i // ensure i has type rune
}
}
func main() {
testint1()
testint2()
testint3()
testint4()
testint5()
}