mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
text/template: fix type bug in eq
{{eq .x 0}} where .x is a nil interface{} should be false, not a type error.
Similarly, {{eq .x .x}} should succeed, not panic in reflect.
Fixes #45982.
Change-Id: I90aba82bb2f1a9e162bde1290c94f5028f56f412
Reviewed-on: https://go-review.googlesource.com/c/go/+/317470
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
0e7a7a68cd
commit
d2fd503f68
2 changed files with 15 additions and 10 deletions
|
|
@ -1201,8 +1201,11 @@ var cmpTests = []cmpTest{
|
||||||
{"eq .Ptr .NilPtr", "false", true},
|
{"eq .Ptr .NilPtr", "false", true},
|
||||||
{"eq .NilPtr .NilPtr", "true", true},
|
{"eq .NilPtr .NilPtr", "true", true},
|
||||||
{"eq .Iface1 .Iface1", "true", true},
|
{"eq .Iface1 .Iface1", "true", true},
|
||||||
{"eq .Iface1 .Iface2", "false", true},
|
{"eq .Iface1 .NilIface", "false", true},
|
||||||
{"eq .Iface2 .Iface2", "true", true},
|
{"eq .NilIface .NilIface", "true", true},
|
||||||
|
{"eq .NilIface .Iface1", "false", true},
|
||||||
|
{"eq .NilIface 0", "false", true},
|
||||||
|
{"eq 0 .NilIface", "false", true},
|
||||||
// Errors
|
// Errors
|
||||||
{"eq `xy` 1", "", false}, // Different types.
|
{"eq `xy` 1", "", false}, // Different types.
|
||||||
{"eq 2 2.0", "", false}, // Different types.
|
{"eq 2 2.0", "", false}, // Different types.
|
||||||
|
|
@ -1217,12 +1220,12 @@ var cmpTests = []cmpTest{
|
||||||
func TestComparison(t *testing.T) {
|
func TestComparison(t *testing.T) {
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
var cmpStruct = struct {
|
var cmpStruct = struct {
|
||||||
Uthree, Ufour uint
|
Uthree, Ufour uint
|
||||||
NegOne, Three int
|
NegOne, Three int
|
||||||
Ptr, NilPtr *int
|
Ptr, NilPtr *int
|
||||||
Map map[int]int
|
Map map[int]int
|
||||||
V1, V2 V
|
V1, V2 V
|
||||||
Iface1, Iface2 fmt.Stringer
|
Iface1, NilIface fmt.Stringer
|
||||||
}{
|
}{
|
||||||
Uthree: 3,
|
Uthree: 3,
|
||||||
Ufour: 4,
|
Ufour: 4,
|
||||||
|
|
|
||||||
|
|
@ -478,7 +478,9 @@ func eq(arg1 reflect.Value, arg2 ...reflect.Value) (bool, error) {
|
||||||
case k1 == uintKind && k2 == intKind:
|
case k1 == uintKind && k2 == intKind:
|
||||||
truth = arg.Int() >= 0 && arg1.Uint() == uint64(arg.Int())
|
truth = arg.Int() >= 0 && arg1.Uint() == uint64(arg.Int())
|
||||||
default:
|
default:
|
||||||
return false, errBadComparison
|
if arg1 != zero && arg != zero {
|
||||||
|
return false, errBadComparison
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
switch k1 {
|
switch k1 {
|
||||||
|
|
@ -495,7 +497,7 @@ func eq(arg1 reflect.Value, arg2 ...reflect.Value) (bool, error) {
|
||||||
case uintKind:
|
case uintKind:
|
||||||
truth = arg1.Uint() == arg.Uint()
|
truth = arg1.Uint() == arg.Uint()
|
||||||
default:
|
default:
|
||||||
if arg == zero {
|
if arg == zero || arg1 == zero {
|
||||||
truth = arg1 == arg
|
truth = arg1 == arg
|
||||||
} else {
|
} else {
|
||||||
if t2 := arg.Type(); !t2.Comparable() {
|
if t2 := arg.Type(); !t2.Comparable() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue