1
0
mirror of https://github.com/golang/go synced 2024-11-17 19:04:47 -07:00
go/test/range3.go
Cuong Manh Le fbfe62bc80 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>
2023-12-01 17:20:08 +00:00

91 lines
1.3 KiB
Go

// run
// Copyright 2023 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.
// Test the 'for range' construct ranging over integers.
package main
func testint1() {
bad := false
j := 0
for i := range int(4) {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint1")
}
}
func testint2() {
bad := false
j := 0
for i := range 4 {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint2")
}
}
func testint3() {
bad := false
type MyInt int
j := MyInt(0)
for i := range MyInt(4) {
if i != j {
println("range var", i, "want", j)
bad = true
}
j++
}
if j != 4 {
println("wrong count ranging over 4:", j)
bad = true
}
if bad {
panic("testint3")
}
}
// Issue #63378.
func testint4() {
for i := range -1 {
_ = i
panic("must not be executed")
}
}
// Issue #64471.
func testint5() {
for i := range 'a' {
var _ *rune = &i // ensure i has type rune
}
}
func main() {
testint1()
testint2()
testint3()
testint4()
testint5()
}