From d29e8e5f222eb172444032f365c0487b4d8034cf Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Mon, 25 May 2026 11:14:02 -0700 Subject: [PATCH] reflect: add converting complex --- src/internal/reflectlite/value.go | 39 +++++++++++++++++++++++++------ src/reflect/type.go | 2 +- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/internal/reflectlite/value.go b/src/internal/reflectlite/value.go index 5650c1266..e75485985 100644 --- a/src/internal/reflectlite/value.go +++ b/src/internal/reflectlite/value.go @@ -1485,13 +1485,11 @@ func convertOp(src Value, typ Type) (Value, bool) { return cvtFloat(src, rtype), true } - /* - case Complex64, Complex128: - switch src.Kind() { - case Complex64, Complex128: - return cvtComplex - } - */ + case Complex64, Complex128: + switch src.Kind() { + case Complex64, Complex128: + return cvtComplex(src, typ.(*RawType)), true + } case Slice: switch rtype := typ.(*RawType); rtype.Kind() { @@ -1578,6 +1576,10 @@ func cvtFloat(v Value, t *RawType) Value { return makeFloat(v.flags, v.Float(), t) } +func cvtComplex(v Value, t *RawType) Value { + return makeComplex(v.flags, v.Complex(), t) +} + //go:linkname stringToBytes runtime.stringToBytes func stringToBytes(x string) []byte @@ -1661,6 +1663,29 @@ func makeFloat32(flags valueFlags, f float32, t *RawType) Value { return v } +func makeComplex(flags valueFlags, f complex128, t *RawType) Value { + size := t.Size() + + v := Value{ + typecode: t, + flags: flags, + } + + ptr := unsafe.Pointer(&v.value) + if size > unsafe.Sizeof(uintptr(0)) { + ptr = alloc(size, nil) + v.value = ptr + } + + switch size { + case 8: + *(*complex64)(ptr) = complex64(f) + case 16: + *(*complex128)(ptr) = f + } + return v +} + func cvtIntString(src Value, t *RawType) Value { panic("cvtUintString: unimplemented") } diff --git a/src/reflect/type.go b/src/reflect/type.go index 9357cef52..777db1a81 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -402,7 +402,7 @@ func (t *rawType) CanSeq2() bool { } func (t *rawType) ConvertibleTo(u Type) bool { - return t.RawType.Convertibleto(u.(*rawType).RawType) + return t.RawType.ConvertibleTo(&(u.(*rawType).RawType)) } func (t *rawType) Elem() Type {