Skip to content

Commit

Permalink
Adjust for reflect.Type.NumMethod change in Go1.16 (#240)
Browse files Browse the repository at this point in the history
In Go1.16, the reflect.Type.NumMethod method will no longer report
unexported fields, matching the documented behavior on the method.
This means that t.NumMethod() == 0 is no longer a reliable means
to detect whether an interface type is the empty interface or not.
Fix the code to check whether the empty interface itself implements
the target type.
  • Loading branch information
dsnet authored Oct 20, 2020
1 parent 566225a commit ab46b8b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmp/cmpopts/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/internal/function"
"github.com/google/go-cmp/cmp/internal/value"
)

// IgnoreFields returns an Option that ignores fields of the
Expand Down Expand Up @@ -82,7 +83,7 @@ func newIfaceFilter(ifaces interface{}) (tf ifaceFilter) {
panic("struct cannot have named fields")
case fi.Type.Kind() != reflect.Interface:
panic("embedded field must be an interface type")
case fi.Type.NumMethod() == 0:
case value.IsEmptyInterface(fi.Type):
// This matches everything; why would you ever want this?
panic("cannot ignore empty interface")
default:
Expand Down
14 changes: 14 additions & 0 deletions cmp/internal/value/iface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2020, 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.md file.

package value

import "reflect"

var emptyIfaceType = reflect.TypeOf((*interface{})(nil)).Elem()

// IsEmptyInterface reports whether t is an interface type with no methods.
func IsEmptyInterface(t reflect.Type) bool {
return t.Kind() == reflect.Interface && emptyIfaceType.Implements(t)
}
35 changes: 35 additions & 0 deletions cmp/internal/value/iface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020, 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.md file.

package value

import (
"reflect"
"testing"
)

func TestIsEmptyInterface(t *testing.T) {
type (
Empty interface{}
Exported interface{ X() }
Unexported interface{ x() }
)
tests := []struct {
in reflect.Type
want bool
}{
{reflect.TypeOf((*interface{})(nil)).Elem(), true},
{reflect.TypeOf((*Empty)(nil)).Elem(), true},
{reflect.TypeOf((*Exported)(nil)).Elem(), false},
{reflect.TypeOf((*Unexported)(nil)).Elem(), false},
{reflect.TypeOf(5), false},
{reflect.TypeOf(struct{}{}), false},
}
for _, tt := range tests {
got := IsEmptyInterface(tt.in)
if got != tt.want {
t.Errorf("IsEmptyInterface(%v) = %v, want %v", tt.in, got, tt.want)
}
}
}
7 changes: 4 additions & 3 deletions cmp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/google/go-cmp/cmp/internal/function"
"github.com/google/go-cmp/cmp/internal/value"
)

// Option configures for specific behavior of Equal and Diff. In particular,
Expand Down Expand Up @@ -161,7 +162,7 @@ func FilterValues(f interface{}, opt Option) Option {
}
if opt := normalizeOption(opt); opt != nil {
vf := &valuesFilter{fnc: v, opt: opt}
if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
if ti := v.Type().In(0); !value.IsEmptyInterface(ti) {
vf.typ = ti
}
return vf
Expand Down Expand Up @@ -286,7 +287,7 @@ func Transformer(name string, f interface{}) Option {
panic(fmt.Sprintf("invalid name: %q", name))
}
tr := &transformer{name: name, fnc: reflect.ValueOf(f)}
if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
if ti := v.Type().In(0); !value.IsEmptyInterface(ti) {
tr.typ = ti
}
return tr
Expand Down Expand Up @@ -345,7 +346,7 @@ func Comparer(f interface{}) Option {
panic(fmt.Sprintf("invalid comparer function: %T", f))
}
cm := &comparer{fnc: v}
if ti := v.Type().In(0); ti.Kind() != reflect.Interface || ti.NumMethod() > 0 {
if ti := v.Type().In(0); !value.IsEmptyInterface(ti) {
cm.typ = ti
}
return cm
Expand Down

0 comments on commit ab46b8b

Please sign in to comment.