From 061a410285e826b5ac1a262116bd64782da4b9fd Mon Sep 17 00:00:00 2001 From: Denise Li Date: Mon, 19 Aug 2024 14:11:36 -0400 Subject: [PATCH] nuke reflect changes minus list.List --- internal/reflect/reflect.go | 23 +++-------------------- internal/reflect/reflect_test.go | 25 ------------------------- 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/internal/reflect/reflect.go b/internal/reflect/reflect.go index 6f25cca53b..2c5aa5e5a9 100644 --- a/internal/reflect/reflect.go +++ b/internal/reflect/reflect.go @@ -145,11 +145,7 @@ func copyAny(src any, ptrs map[uintptr]any, copyConf *copyConfig) (dst any) { reflect.Complex64, reflect.Complex128, reflect.Func: dst = copyPremitive(src, ptrs, copyConf) case reflect.String: - if v.Type() == reflect.TypeFor[string]() { - dst = strings.Clone(src.(string)) - } else { - dst = copyStringAlias(src, ptrs, copyConf) - } + dst = strings.Clone(src.(string)) case reflect.Slice: dst = copySlice(src, ptrs, copyConf) case reflect.Array: @@ -192,13 +188,6 @@ func copyPremitive(src any, ptr map[uintptr]any, copyConf *copyConfig) (dst any) return } -func copyStringAlias(src any, ptr map[uintptr]any, copyConf *copyConfig) any { - v := reflect.ValueOf(src) - dc := reflect.New(v.Type()).Elem() - dc.Set(v) - return dc.Interface() -} - func copySlice(x any, ptrs map[uintptr]any, copyConf *copyConfig) any { v := reflect.ValueOf(x) kind := v.Kind() @@ -253,14 +242,8 @@ func copyPointer(x any, ptrs map[uintptr]any, copyConf *copyConfig) any { v := reflect.ValueOf(x) t := reflect.TypeOf(x) - if v.Kind() != reflect.Pointer && v.Kind() != reflect.UnsafePointer { - panic(fmt.Errorf("reflect: internal error: must be a Pointer or UnsafePointer; got %v", v.Kind())) - } - - if v.Kind() == reflect.UnsafePointer { - // For unsafe.Pointer, just return a copy of the pointer itself, since it - // has no element type to copy (t.Elem() panics). - return x + if v.Kind() != reflect.Pointer { + panic(fmt.Errorf("reflect: internal error: must be a Pointer; got %v", v.Kind())) } if v.IsNil() { diff --git a/internal/reflect/reflect_test.go b/internal/reflect/reflect_test.go index 349a98021d..849c9212f7 100644 --- a/internal/reflect/reflect_test.go +++ b/internal/reflect/reflect_test.go @@ -3,21 +3,10 @@ package reflect import ( "container/list" "testing" - "unsafe" "gotest.tools/v3/assert" ) -type mystring string -type structWithMystring struct { - str mystring -} - -func TestAliasedString(t *testing.T) { - output := DeepCopy(structWithMystring{"asdf"}) - assert.Equal(t, output, structWithMystring{"asdf"}) -} - func TestListElements(t *testing.T) { l := list.New() l.PushBack("one") @@ -26,20 +15,6 @@ func TestListElements(t *testing.T) { assert.Equal(t, output.Len(), l.Len()) } -type structWithUnsafePtr struct { - ptr unsafe.Pointer -} - -func TestUnsafePointer(t *testing.T) { - var x int - // #nosec G103 - obj := structWithUnsafePtr{ptr: unsafe.Pointer(&x)} - DeepCopy(obj) // should not panic - x = 3 - intPtr := (*int)(obj.ptr) - assert.Equal(t, 3, *intPtr) -} - type structOfPointers struct { intPtr *int floatPtr *float64