From 571a56ba783fe6ec6c9f3aa12166e8e33652e4c5 Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Wed, 22 Feb 2023 14:28:49 -0800 Subject: [PATCH] Remove purego fallbacks (#325) Having go-cmp panic when using the purego build tag makes it hard to use go-cmp for testing packages that themselves have purego fallbacks. Since go-cmp can't implement its functionality without unsafe (the "fallback" panics) and since environments that prohibit unsafe are much less common these days anyway, simply remove purego code entirely. Fixes #283. --- cmp/{export_unsafe.go => export.go} | 5 --- cmp/export_panic.go | 16 --------- .../value/{pointer_unsafe.go => pointer.go} | 3 -- cmp/internal/value/pointer_purego.go | 34 ------------------- cmp/options.go | 3 -- cmp/report_reflect.go | 2 +- 6 files changed, 1 insertion(+), 62 deletions(-) rename cmp/{export_unsafe.go => export.go} (94%) delete mode 100644 cmp/export_panic.go rename cmp/internal/value/{pointer_unsafe.go => pointer.go} (95%) delete mode 100644 cmp/internal/value/pointer_purego.go diff --git a/cmp/export_unsafe.go b/cmp/export.go similarity index 94% rename from cmp/export_unsafe.go rename to cmp/export.go index e2c0f74..29f82fe 100644 --- a/cmp/export_unsafe.go +++ b/cmp/export.go @@ -2,9 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build !purego -// +build !purego - package cmp import ( @@ -12,8 +9,6 @@ import ( "unsafe" ) -const supportExporters = true - // retrieveUnexportedField uses unsafe to forcibly retrieve any field from // a struct such that the value has read-write permissions. // diff --git a/cmp/export_panic.go b/cmp/export_panic.go deleted file mode 100644 index ae851fe..0000000 --- a/cmp/export_panic.go +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017, The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build purego -// +build purego - -package cmp - -import "reflect" - -const supportExporters = false - -func retrieveUnexportedField(reflect.Value, reflect.StructField, bool) reflect.Value { - panic("no support for forcibly accessing unexported fields") -} diff --git a/cmp/internal/value/pointer_unsafe.go b/cmp/internal/value/pointer.go similarity index 95% rename from cmp/internal/value/pointer_unsafe.go rename to cmp/internal/value/pointer.go index 16e6860..e5dfff6 100644 --- a/cmp/internal/value/pointer_unsafe.go +++ b/cmp/internal/value/pointer.go @@ -2,9 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build !purego -// +build !purego - package value import ( diff --git a/cmp/internal/value/pointer_purego.go b/cmp/internal/value/pointer_purego.go deleted file mode 100644 index 1a71bfc..0000000 --- a/cmp/internal/value/pointer_purego.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2018, The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build purego -// +build purego - -package value - -import "reflect" - -// Pointer is an opaque typed pointer and is guaranteed to be comparable. -type Pointer struct { - p uintptr - t reflect.Type -} - -// PointerOf returns a Pointer from v, which must be a -// reflect.Ptr, reflect.Slice, or reflect.Map. -func PointerOf(v reflect.Value) Pointer { - // NOTE: Storing a pointer as an uintptr is technically incorrect as it - // assumes that the GC implementation does not use a moving collector. - return Pointer{v.Pointer(), v.Type()} -} - -// IsNil reports whether the pointer is nil. -func (p Pointer) IsNil() bool { - return p.p == 0 -} - -// Uintptr returns the pointer as a uintptr. -func (p Pointer) Uintptr() uintptr { - return p.p -} diff --git a/cmp/options.go b/cmp/options.go index 1f9ca9c..62d8bdb 100644 --- a/cmp/options.go +++ b/cmp/options.go @@ -403,9 +403,6 @@ func (cm comparer) String() string { // In other cases, the cmpopts.IgnoreUnexported option can be used to ignore // all unexported fields on specified struct types. func Exporter(f func(reflect.Type) bool) Option { - if !supportExporters { - panic("Exporter is not supported on purego builds") - } return exporter(f) } diff --git a/cmp/report_reflect.go b/cmp/report_reflect.go index 2ab41fa..e39f422 100644 --- a/cmp/report_reflect.go +++ b/cmp/report_reflect.go @@ -199,7 +199,7 @@ func (opts formatOptions) FormatValue(v reflect.Value, parentKind reflect.Kind, break } sf := t.Field(i) - if supportExporters && !isExported(sf.Name) { + if !isExported(sf.Name) { vv = retrieveUnexportedField(v, sf, true) } s := opts.WithTypeMode(autoType).FormatValue(vv, t.Kind(), ptrs)