]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix typecheck range over rune literal
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 30 Nov 2023 17:16:24 +0000 (00:16 +0700)
committerGopher Robot <gobot@golang.org>
Fri, 1 Dec 2023 17:20:08 +0000 (17:20 +0000)
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>

src/cmd/compile/internal/noder/helpers.go
test/range3.go

index 1f7b497599dcd8423ccdee3d6ffaf6fadf375bc1..f9e3838fd955e980dc11d847d81935a3d8f1a1b1 100644 (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
                }
index 4f770a2b7005ce03cd51d53a1297990ff177ea0d..f58a398f94c16c95623217ad3e06e8b12d4184a6 100644 (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()
 }