Skip to content
Snippets Groups Projects
Commit 0fa31cb6 authored by qiulaidongfeng's avatar qiulaidongfeng Committed by Gopher Robot
Browse files

[release-branch.go1.23] reflect: let Value.Seq return the iteration value correct type

Fixes #71915
For #71905

Change-Id: I50a418f8552e071c6e5011af5b9accc7d41548d0
Reviewed-on: https://go-review.googlesource.com/c/go/+/651855


Reviewed-by: default avatarIan Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: default avatarMichael Pratt <mpratt@google.com>
(cherry picked from commit 194696f1)
Reviewed-on: https://go-review.googlesource.com/c/go/+/652875


Reviewed-by: default avatarMichael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
parent d89fda21
No related branches found
No related tags found
No related merge requests found
...@@ -4,15 +4,24 @@ ...@@ -4,15 +4,24 @@
package reflect package reflect
import "iter" import (
"iter"
)
func rangeNum[T int8 | int16 | int32 | int64 | int | func rangeNum[T int8 | int16 | int32 | int64 | int |
uint8 | uint16 | uint32 | uint64 | uint | uint8 | uint16 | uint32 | uint64 | uint |
uintptr, N int64 | uint64](v N) iter.Seq[Value] { uintptr, N int64 | uint64](num N, t Type) iter.Seq[Value] {
return func(yield func(v Value) bool) { return func(yield func(v Value) bool) {
convert := t.PkgPath() != ""
// cannot use range T(v) because no core type. // cannot use range T(v) because no core type.
for i := T(0); i < T(v); i++ { for i := T(0); i < T(num); i++ {
if !yield(ValueOf(i)) { tmp := ValueOf(i)
// if the iteration value type is define by
// type T built-in type.
if convert {
tmp = tmp.Convert(t)
}
if !yield(tmp) {
return return
} }
} }
...@@ -35,29 +44,29 @@ func (v Value) Seq() iter.Seq[Value] { ...@@ -35,29 +44,29 @@ func (v Value) Seq() iter.Seq[Value] {
v.Call([]Value{rf}) v.Call([]Value{rf})
} }
} }
switch v.Kind() { switch v.kind() {
case Int: case Int:
return rangeNum[int](v.Int()) return rangeNum[int](v.Int(), v.Type())
case Int8: case Int8:
return rangeNum[int8](v.Int()) return rangeNum[int8](v.Int(), v.Type())
case Int16: case Int16:
return rangeNum[int16](v.Int()) return rangeNum[int16](v.Int(), v.Type())
case Int32: case Int32:
return rangeNum[int32](v.Int()) return rangeNum[int32](v.Int(), v.Type())
case Int64: case Int64:
return rangeNum[int64](v.Int()) return rangeNum[int64](v.Int(), v.Type())
case Uint: case Uint:
return rangeNum[uint](v.Uint()) return rangeNum[uint](v.Uint(), v.Type())
case Uint8: case Uint8:
return rangeNum[uint8](v.Uint()) return rangeNum[uint8](v.Uint(), v.Type())
case Uint16: case Uint16:
return rangeNum[uint16](v.Uint()) return rangeNum[uint16](v.Uint(), v.Type())
case Uint32: case Uint32:
return rangeNum[uint32](v.Uint()) return rangeNum[uint32](v.Uint(), v.Type())
case Uint64: case Uint64:
return rangeNum[uint64](v.Uint()) return rangeNum[uint64](v.Uint(), v.Type())
case Uintptr: case Uintptr:
return rangeNum[uintptr](v.Uint()) return rangeNum[uintptr](v.Uint(), v.Type())
case Pointer: case Pointer:
if v.Elem().kind() != Array { if v.Elem().kind() != Array {
break break
......
...@@ -7,10 +7,13 @@ package reflect_test ...@@ -7,10 +7,13 @@ package reflect_test
import ( import (
"iter" "iter"
"maps" "maps"
"reflect"
. "reflect" . "reflect"
"testing" "testing"
) )
type N int8
func TestValueSeq(t *testing.T) { func TestValueSeq(t *testing.T) {
m := map[string]int{ m := map[string]int{
"1": 1, "1": 1,
...@@ -187,6 +190,21 @@ func TestValueSeq(t *testing.T) { ...@@ -187,6 +190,21 @@ func TestValueSeq(t *testing.T) {
t.Fatalf("should loop four times") t.Fatalf("should loop four times")
} }
}}, }},
{"type N int8", ValueOf(N(4)), func(t *testing.T, s iter.Seq[Value]) {
i := N(0)
for v := range s {
if v.Int() != int64(i) {
t.Fatalf("got %d, want %d", v.Int(), i)
}
i++
if v.Type() != reflect.TypeOf(i) {
t.Fatalf("got %s, want %s", v.Type(), reflect.TypeOf(i))
}
}
if i != 4 {
t.Fatalf("should loop four times")
}
}},
} }
for _, tc := range tests { for _, tc := range tests {
seq := tc.val.Seq() seq := tc.val.Seq()
...@@ -323,6 +341,42 @@ func TestValueSeq2(t *testing.T) { ...@@ -323,6 +341,42 @@ func TestValueSeq2(t *testing.T) {
t.Fatalf("should loop four times") t.Fatalf("should loop four times")
} }
}}, }},
{"[4]N", ValueOf([4]N{0, 1, 2, 3}), func(t *testing.T, s iter.Seq2[Value, Value]) {
i := N(0)
for v1, v2 := range s {
if v1.Int() != int64(i) {
t.Fatalf("got %d, want %d", v1.Int(), i)
}
if v2.Int() != int64(i) {
t.Fatalf("got %d, want %d", v2.Int(), i)
}
i++
if v2.Type() != reflect.TypeOf(i) {
t.Fatalf("got %s, want %s", v2.Type(), reflect.TypeOf(i))
}
}
if i != 4 {
t.Fatalf("should loop four times")
}
}},
{"[]N", ValueOf([]N{1, 2, 3, 4}), func(t *testing.T, s iter.Seq2[Value, Value]) {
i := N(0)
for v1, v2 := range s {
if v1.Int() != int64(i) {
t.Fatalf("got %d, want %d", v1.Int(), i)
}
i++
if v2.Int() != int64(i) {
t.Fatalf("got %d, want %d", v2.Int(), i)
}
if v2.Type() != reflect.TypeOf(i) {
t.Fatalf("got %s, want %s", v2.Type(), reflect.TypeOf(i))
}
}
if i != 4 {
t.Fatalf("should loop four times")
}
}},
} }
for _, tc := range tests { for _, tc := range tests {
seq := tc.val.Seq2() seq := tc.val.Seq2()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment