reflect: Value.Seq iteration value types should match

Implementation of https://github.com/golang/go/issues/71905

194696f1d1f6e5609f96d0fb0192595e7e0f5b90
This commit is contained in:
Randy Reddig
2025-03-09 09:04:47 -07:00
committed by Ron Evans
parent b6c3d142db
commit 64651115c2
2 changed files with 29 additions and 19 deletions
+24 -15
View File
@@ -6,15 +6,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
} }
} }
@@ -40,27 +49,27 @@ func (v Value) Seq() iter.Seq[Value] {
// } // }
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
+5 -4
View File
@@ -197,10 +197,11 @@ func TestValueSeq(t *testing.T) {
t.Fatalf("got %d, want %d", v.Int(), i) t.Fatalf("got %d, want %d", v.Int(), i)
} }
i++ i++
// TODO: iteration should produce the same type if v.Type() != reflect.TypeOf(i) {
// if v.Type() != reflect.TypeOf(i) { j := ValueOf(i)
// t.Fatalf("got %s, want %s", v.Type(), reflect.TypeOf(i)) t.Logf("ValueOf(j): %s", j.Type())
// } t.Fatalf("got %s, want %s", v.Type(), reflect.TypeOf(i))
}
} }
if i != 4 { if i != 4 {
t.Fatalf("should loop four times") t.Fatalf("should loop four times")