aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDamian Gryski <[email protected]>2023-03-22 18:27:07 -0700
committerRon Evans <[email protected]>2023-03-25 22:32:29 +0100
commit7201b130856577dbdb53fbf8a4614e0cba8d78a1 (patch)
tree7f5b5dcdfd86ccd4f92a2fdb718c8232f04a4212
parent9c0bf8bd2ce78caf6bcba335651f498046c1ee0e (diff)
downloadtinygo-7201b130856577dbdb53fbf8a4614e0cba8d78a1.tar.gz
tinygo-7201b130856577dbdb53fbf8a4614e0cba8d78a1.zip
reflect: fix key type logic for maps
-rw-r--r--src/reflect/value.go12
1 files changed, 8 insertions, 4 deletions
diff --git a/src/reflect/value.go b/src/reflect/value.go
index 2d39028dd..384e67b12 100644
--- a/src/reflect/value.go
+++ b/src/reflect/value.go
@@ -810,8 +810,10 @@ func (v Value) MapIndex(key Value) Value {
panic(&ValueError{Method: "MapIndex", Kind: v.Kind()})
}
+ vkey := v.typecode.key()
+
// compare key type with actual key type of map
- if key.typecode != v.typecode.key() {
+ if !key.typecode.AssignableTo(vkey) {
// type error?
panic("reflect.Value.MapIndex: incompatible types for key")
}
@@ -819,12 +821,12 @@ func (v Value) MapIndex(key Value) Value {
elemType := v.typecode.Elem()
elem := New(elemType)
- if key.Kind() == String {
+ if vkey.Kind() == String {
if ok := hashmapStringGet(v.pointer(), *(*string)(key.value), elem.value, elemType.Size()); !ok {
return Value{}
}
return elem.Elem()
- } else if key.typecode.isBinary() {
+ } else if vkey.isBinary() {
var keyptr unsafe.Pointer
if key.isIndirect() || key.typecode.Size() > unsafe.Sizeof(uintptr(0)) {
keyptr = key.value
@@ -1367,8 +1369,10 @@ func (v Value) SetMapIndex(key, elem Value) {
panic(&ValueError{Method: "SetMapIndex", Kind: v.Kind()})
}
+ vkey := v.typecode.key()
+
// 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")
}