Skip to content

Commit 3242228

Browse files
authored
Drop hacks to work around Go reflection bugs in Go1.9 (#282)
Now that Go 1.11 is the minimally supported version, we can drop some local hacks to work around bugs in reflect that were present in Go1.9.
1 parent f59cd61 commit 3242228

File tree

4 files changed

+2
-49
lines changed

4 files changed

+2
-49
lines changed

cmp/compare.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"strings"
3737

3838
"github.com/google/go-cmp/cmp/internal/diff"
39-
"github.com/google/go-cmp/cmp/internal/flags"
4039
"github.com/google/go-cmp/cmp/internal/function"
4140
"github.com/google/go-cmp/cmp/internal/value"
4241
)
@@ -319,7 +318,6 @@ func (s *state) tryMethod(t reflect.Type, vx, vy reflect.Value) bool {
319318
}
320319

321320
func (s *state) callTRFunc(f, v reflect.Value, step Transform) reflect.Value {
322-
v = sanitizeValue(v, f.Type().In(0))
323321
if !s.dynChecker.Next() {
324322
return f.Call([]reflect.Value{v})[0]
325323
}
@@ -343,8 +341,6 @@ func (s *state) callTRFunc(f, v reflect.Value, step Transform) reflect.Value {
343341
}
344342

345343
func (s *state) callTTBFunc(f, x, y reflect.Value) bool {
346-
x = sanitizeValue(x, f.Type().In(0))
347-
y = sanitizeValue(y, f.Type().In(1))
348344
if !s.dynChecker.Next() {
349345
return f.Call([]reflect.Value{x, y})[0].Bool()
350346
}
@@ -372,19 +368,6 @@ func detectRaces(c chan<- reflect.Value, f reflect.Value, vs ...reflect.Value) {
372368
ret = f.Call(vs)[0]
373369
}
374370

375-
// sanitizeValue converts nil interfaces of type T to those of type R,
376-
// assuming that T is assignable to R.
377-
// Otherwise, it returns the input value as is.
378-
func sanitizeValue(v reflect.Value, t reflect.Type) reflect.Value {
379-
// TODO(≥go1.10): Workaround for reflect bug (https://golang.org/issue/22143).
380-
if !flags.AtLeastGo110 {
381-
if v.Kind() == reflect.Interface && v.IsNil() && v.Type() != t {
382-
return reflect.New(t).Elem()
383-
}
384-
}
385-
return v
386-
}
387-
388371
func (s *state) compareStruct(t reflect.Type, vx, vy reflect.Value) {
389372
var addr bool
390373
var vax, vay reflect.Value // Addressable versions of vx and vy

cmp/compare_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,14 +1447,6 @@ func embeddedTests() []test {
14471447
return s
14481448
}
14491449

1450-
// TODO(≥go1.10): Workaround for reflect bug (https://golang.org/issue/21122).
1451-
wantPanicNotGo110 := func(s string) string {
1452-
if !flags.AtLeastGo110 {
1453-
return ""
1454-
}
1455-
return s
1456-
}
1457-
14581450
return []test{{
14591451
label: label + "/ParentStructA/PanicUnexported1",
14601452
x: ts.ParentStructA{},
@@ -1745,8 +1737,7 @@ func embeddedTests() []test {
17451737
label: label + "/ParentStructG/PanicUnexported1",
17461738
x: ts.ParentStructG{},
17471739
y: ts.ParentStructG{},
1748-
wantPanic: wantPanicNotGo110("cannot handle unexported field"),
1749-
wantEqual: !flags.AtLeastGo110,
1740+
wantPanic: "cannot handle unexported field",
17501741
reason: "ParentStructG has unexported fields",
17511742
}, {
17521743
label: label + "/ParentStructG/Ignored",
@@ -1836,8 +1827,7 @@ func embeddedTests() []test {
18361827
label: label + "/ParentStructI/PanicUnexported1",
18371828
x: ts.ParentStructI{},
18381829
y: ts.ParentStructI{},
1839-
wantPanic: wantPanicNotGo110("cannot handle unexported field"),
1840-
wantEqual: !flags.AtLeastGo110,
1830+
wantPanic: "cannot handle unexported field",
18411831
reason: "ParentStructI has unexported fields",
18421832
}, {
18431833
label: label + "/ParentStructI/Ignored1",

cmp/internal/flags/toolchain_legacy.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

cmp/internal/flags/toolchain_recent.go

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)