Update to go1.24.1

This commit is contained in:
Vorapol Rinsatitnon
2025-03-07 10:53:05 +07:00
parent bf266cebe6
commit 6304737d59
53 changed files with 21449 additions and 157 deletions

View File

@@ -4,15 +4,24 @@
package reflect
import "iter"
import (
"iter"
)
func rangeNum[T int8 | int16 | int32 | int64 | int |
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) {
convert := t.PkgPath() != ""
// cannot use range T(v) because no core type.
for i := T(0); i < T(v); i++ {
if !yield(ValueOf(i)) {
for i := T(0); i < T(num); 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
}
}
@@ -27,7 +36,7 @@ func rangeNum[T int8 | int16 | int32 | int64 | int |
// Uint, Uint8, Uint16, Uint32, Uint64, Uintptr,
// Array, Chan, Map, Slice, or String.
func (v Value) Seq() iter.Seq[Value] {
if canRangeFunc(v.typ()) {
if canRangeFunc(v.abiType()) {
return func(yield func(Value) bool) {
rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
return []Value{ValueOf(yield(in[0]))}
@@ -35,29 +44,29 @@ func (v Value) Seq() iter.Seq[Value] {
v.Call([]Value{rf})
}
}
switch v.Kind() {
switch v.kind() {
case Int:
return rangeNum[int](v.Int())
return rangeNum[int](v.Int(), v.Type())
case Int8:
return rangeNum[int8](v.Int())
return rangeNum[int8](v.Int(), v.Type())
case Int16:
return rangeNum[int16](v.Int())
return rangeNum[int16](v.Int(), v.Type())
case Int32:
return rangeNum[int32](v.Int())
return rangeNum[int32](v.Int(), v.Type())
case Int64:
return rangeNum[int64](v.Int())
return rangeNum[int64](v.Int(), v.Type())
case Uint:
return rangeNum[uint](v.Uint())
return rangeNum[uint](v.Uint(), v.Type())
case Uint8:
return rangeNum[uint8](v.Uint())
return rangeNum[uint8](v.Uint(), v.Type())
case Uint16:
return rangeNum[uint16](v.Uint())
return rangeNum[uint16](v.Uint(), v.Type())
case Uint32:
return rangeNum[uint32](v.Uint())
return rangeNum[uint32](v.Uint(), v.Type())
case Uint64:
return rangeNum[uint64](v.Uint())
return rangeNum[uint64](v.Uint(), v.Type())
case Uintptr:
return rangeNum[uintptr](v.Uint())
return rangeNum[uintptr](v.Uint(), v.Type())
case Pointer:
if v.Elem().kind() != Array {
break
@@ -113,7 +122,7 @@ func (v Value) Seq() iter.Seq[Value] {
// If v's kind is Pointer, the pointer element type must have kind Array.
// Otherwise v's kind must be Array, Map, Slice, or String.
func (v Value) Seq2() iter.Seq2[Value, Value] {
if canRangeFunc2(v.typ()) {
if canRangeFunc2(v.abiType()) {
return func(yield func(Value, Value) bool) {
rf := MakeFunc(v.Type().In(0), func(in []Value) []Value {
return []Value{ValueOf(yield(in[0], in[1]))}

View File

@@ -7,10 +7,13 @@ package reflect_test
import (
"iter"
"maps"
"reflect"
. "reflect"
"testing"
)
type N int8
func TestValueSeq(t *testing.T) {
m := map[string]int{
"1": 1,
@@ -173,6 +176,33 @@ func TestValueSeq(t *testing.T) {
t.Fatalf("should loop four times")
}
}},
{"method", ValueOf(methodIter{}).Method(0), func(t *testing.T, s iter.Seq[Value]) {
i := int64(0)
for v := range s {
if v.Int() != i {
t.Fatalf("got %d, want %d", v.Int(), i)
}
i++
}
if i != 4 {
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 {
seq := tc.val.Seq()
@@ -293,9 +323,84 @@ func TestValueSeq2(t *testing.T) {
t.Fatalf("should loop four times")
}
}},
{"method", ValueOf(methodIter2{}).Method(0), func(t *testing.T, s iter.Seq2[Value, Value]) {
i := int64(0)
for v1, v2 := range s {
if v1.Int() != i {
t.Fatalf("got %d, want %d", v1.Int(), i)
}
i++
if v2.Int() != i {
t.Fatalf("got %d, want %d", v2.Int(), i)
}
}
if i != 4 {
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 {
seq := tc.val.Seq2()
tc.check(t, seq)
}
}
// methodIter is a type from which we can derive a method
// value that is an iter.Seq.
type methodIter struct{}
func (methodIter) Seq(yield func(int) bool) {
for i := range 4 {
if !yield(i) {
return
}
}
}
// methodIter2 is a type from which we can derive a method
// value that is an iter.Seq2.
type methodIter2 struct{}
func (methodIter2) Seq2(yield func(int, int) bool) {
for i := range 4 {
if !yield(i, i+1) {
return
}
}
}

View File

@@ -93,6 +93,9 @@ func (f flag) ro() flag {
return 0
}
// typ returns the *abi.Type stored in the Value. This method is fast,
// but it doesn't always return the correct type for the Value.
// See abiType and Type, which do return the correct type.
func (v Value) typ() *abi.Type {
// Types are either static (for compiler-created types) or
// heap-allocated but always reachable (for reflection-created
@@ -2380,14 +2383,26 @@ func (v Value) Type() Type {
return v.typeSlow()
}
//go:noinline
func (v Value) typeSlow() Type {
return toRType(v.abiTypeSlow())
}
func (v Value) abiType() *abi.Type {
if v.flag != 0 && v.flag&flagMethod == 0 {
return v.typ()
}
return v.abiTypeSlow()
}
func (v Value) abiTypeSlow() *abi.Type {
if v.flag == 0 {
panic(&ValueError{"reflect.Value.Type", Invalid})
}
typ := v.typ()
if v.flag&flagMethod == 0 {
return toRType(v.typ())
return v.typ()
}
// Method value.
@@ -2400,7 +2415,7 @@ func (v Value) typeSlow() Type {
panic("reflect: internal error: invalid method index")
}
m := &tt.Methods[i]
return toRType(typeOffFor(typ, m.Typ))
return typeOffFor(typ, m.Typ)
}
// Method on concrete type.
ms := typ.ExportedMethods()
@@ -2408,7 +2423,7 @@ func (v Value) typeSlow() Type {
panic("reflect: internal error: invalid method index")
}
m := ms[i]
return toRType(typeOffFor(typ, m.Mtyp))
return typeOffFor(typ, m.Mtyp)
}
// CanUint reports whether [Value.Uint] can be used without panicking.