reflect: fix key type logic for maps

Этот коммит содержится в:
Damian Gryski 2023-03-22 18:27:07 -07:00 коммит произвёл Ron Evans
родитель 9c0bf8bd2c
коммит 7201b13085

Просмотреть файл

@ -810,8 +810,10 @@ func (v Value) MapIndex(key Value) Value {
panic(&ValueError{Method: "MapIndex", Kind: v.Kind()}) panic(&ValueError{Method: "MapIndex", Kind: v.Kind()})
} }
vkey := v.typecode.key()
// compare key type with actual key type of map // compare key type with actual key type of map
if key.typecode != v.typecode.key() { if !key.typecode.AssignableTo(vkey) {
// type error? // type error?
panic("reflect.Value.MapIndex: incompatible types for key") panic("reflect.Value.MapIndex: incompatible types for key")
} }
@ -819,12 +821,12 @@ func (v Value) MapIndex(key Value) Value {
elemType := v.typecode.Elem() elemType := v.typecode.Elem()
elem := New(elemType) elem := New(elemType)
if key.Kind() == String { if vkey.Kind() == String {
if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok { if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok {
return Value{} return Value{}
} }
return elem.Elem() return elem.Elem()
} else if key.typecode.isBinary() { } else if vkey.isBinary() {
var keyptr unsafe.Pointer var keyptr unsafe.Pointer
if key.isIndirect() || key.typecode.Size() > unsafe.Sizeof(uintptr(0)) { if key.isIndirect() || key.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
keyptr = key.value keyptr = key.value
@ -1367,8 +1369,10 @@ func (v Value) SetMapIndex(key, elem Value) {
panic(&ValueError{Method: "SetMapIndex", Kind: v.Kind()}) panic(&ValueError{Method: "SetMapIndex", Kind: v.Kind()})
} }
vkey := v.typecode.key()
// compare key type with actual key type of map // compare key type with actual key type of map
if key.typecode != v.typecode.key() { if !key.typecode.AssignableTo(vkey) {
panic("reflect.Value.SetMapIndex: incompatible types for key") panic("reflect.Value.SetMapIndex: incompatible types for key")
} }