reflect: Value.Set: fix direction of assignment check

This commit is contained in:
Damian Gryski
2023-03-11 14:11:32 -08:00
committed by Ron Evans
parent 63c7a41337
commit 9c0bf8bd2c
2 changed files with 10 additions and 1 deletions
+1 -1
View File
@@ -907,7 +907,7 @@ func (it *MapIter) Next() bool {
func (v Value) Set(x Value) {
v.checkAddressable()
if !v.typecode.AssignableTo(x.typecode) {
if !x.typecode.AssignableTo(v.typecode) {
panic("reflect: cannot set")
}
+9
View File
@@ -504,6 +504,15 @@ func TestTinyNumMethods(t *testing.T) {
}
}
func TestAssignableTo(t *testing.T) {
var a any
refa := ValueOf(&a).Elem()
refa.Set(ValueOf(4))
if got, want := refa.Interface().(int), 4; got != want {
t.Errorf("AssignableTo / Set failed, got %v, want %v", got, want)
}
}
func equal[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false