Skip to content

Commit

Permalink
Remove purego fallbacks (#325)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cespare authored Feb 22, 2023
1 parent a97318b commit 571a56b
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 62 deletions.
5 changes: 0 additions & 5 deletions cmp/export_unsafe.go → cmp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
// 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"
"unsafe"
)

const supportExporters = true

// retrieveUnexportedField uses unsafe to forcibly retrieve any field from
// a struct such that the value has read-write permissions.
//
Expand Down
16 changes: 0 additions & 16 deletions cmp/export_panic.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
34 changes: 0 additions & 34 deletions cmp/internal/value/pointer_purego.go

This file was deleted.

3 changes: 0 additions & 3 deletions cmp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion cmp/report_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 571a56b

Please sign in to comment.