Skip to content

Commit 6465111

Browse files
ydnardeadprogram
authored andcommitted
reflect: Value.Seq iteration value types should match
Implementation of golang/go#71905 194696f1d1f6e5609f96d0fb0192595e7e0f5b90
1 parent b6c3d14 commit 6465111

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/reflect/iter.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@
66

77
package reflect
88

9-
import "iter"
9+
import (
10+
"iter"
11+
)
1012

1113
func rangeNum[T int8 | int16 | int32 | int64 | int |
1214
uint8 | uint16 | uint32 | uint64 | uint |
13-
uintptr, N int64 | uint64](v N) iter.Seq[Value] {
15+
uintptr, N int64 | uint64](num N, t Type) iter.Seq[Value] {
1416
return func(yield func(v Value) bool) {
17+
convert := t.PkgPath() != ""
1518
// cannot use range T(v) because no core type.
16-
for i := T(0); i < T(v); i++ {
17-
if !yield(ValueOf(i)) {
19+
for i := T(0); i < T(num); i++ {
20+
tmp := ValueOf(i)
21+
// if the iteration value type is define by
22+
// type T built-in type.
23+
if convert {
24+
tmp = tmp.Convert(t)
25+
}
26+
if !yield(tmp) {
1827
return
1928
}
2029
}
@@ -40,27 +49,27 @@ func (v Value) Seq() iter.Seq[Value] {
4049
// }
4150
switch v.Kind() {
4251
case Int:
43-
return rangeNum[int](v.Int())
52+
return rangeNum[int](v.Int(), v.Type())
4453
case Int8:
45-
return rangeNum[int8](v.Int())
54+
return rangeNum[int8](v.Int(), v.Type())
4655
case Int16:
47-
return rangeNum[int16](v.Int())
56+
return rangeNum[int16](v.Int(), v.Type())
4857
case Int32:
49-
return rangeNum[int32](v.Int())
58+
return rangeNum[int32](v.Int(), v.Type())
5059
case Int64:
51-
return rangeNum[int64](v.Int())
60+
return rangeNum[int64](v.Int(), v.Type())
5261
case Uint:
53-
return rangeNum[uint](v.Uint())
62+
return rangeNum[uint](v.Uint(), v.Type())
5463
case Uint8:
55-
return rangeNum[uint8](v.Uint())
64+
return rangeNum[uint8](v.Uint(), v.Type())
5665
case Uint16:
57-
return rangeNum[uint16](v.Uint())
66+
return rangeNum[uint16](v.Uint(), v.Type())
5867
case Uint32:
59-
return rangeNum[uint32](v.Uint())
68+
return rangeNum[uint32](v.Uint(), v.Type())
6069
case Uint64:
61-
return rangeNum[uint64](v.Uint())
70+
return rangeNum[uint64](v.Uint(), v.Type())
6271
case Uintptr:
63-
return rangeNum[uintptr](v.Uint())
72+
return rangeNum[uintptr](v.Uint(), v.Type())
6473
case Pointer:
6574
if v.Elem().Kind() != Array {
6675
break

src/reflect/iter_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,11 @@ func TestValueSeq(t *testing.T) {
197197
t.Fatalf("got %d, want %d", v.Int(), i)
198198
}
199199
i++
200-
// TODO: iteration should produce the same type
201-
// if v.Type() != reflect.TypeOf(i) {
202-
// t.Fatalf("got %s, want %s", v.Type(), reflect.TypeOf(i))
203-
// }
200+
if v.Type() != reflect.TypeOf(i) {
201+
j := ValueOf(i)
202+
t.Logf("ValueOf(j): %s", j.Type())
203+
t.Fatalf("got %s, want %s", v.Type(), reflect.TypeOf(i))
204+
}
204205
}
205206
if i != 4 {
206207
t.Fatalf("should loop four times")

0 commit comments

Comments
 (0)