cmd/compile: start using reflect.Value.IsZero

We now require Go 1.17.13 to bootstrap via make.bash,
and since reflect.Value.IsZero was added in Go 1.13,
we can now use it directly to save a bit of copy pasting.

Change-Id: I77eef782cbbf86c72a4505c8b4866c9658914a24
Reviewed-on: https://go-review.googlesource.com/c/go/+/479395
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Daniel Martí 2023-03-25 11:30:26 +00:00
parent 524f820b7f
commit fc99c4b3f3
2 changed files with 2 additions and 56 deletions

View File

@ -218,7 +218,7 @@ func (p *dumper) dump(x reflect.Value, depth int) {
continue // Op field already printed for Nodes
}
x := x.Field(i)
if isZeroVal(x) {
if x.IsZero() {
omitted = true
continue // exclude zero-valued fields
}
@ -248,22 +248,6 @@ func (p *dumper) dump(x reflect.Value, depth int) {
}
}
func isZeroVal(x reflect.Value) bool {
switch x.Kind() {
case reflect.Bool:
return !x.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return x.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return x.Uint() == 0
case reflect.String:
return x.String() == ""
case reflect.Interface, reflect.Ptr, reflect.Slice:
return x.IsNil()
}
return false
}
func commonPrefixLen(a, b string) (i int) {
for i < len(a) && i < len(b) && a[i] == b[i] {
i++

View File

@ -9,7 +9,6 @@ import (
"fmt"
"go/constant"
"io"
"math"
"os"
"path/filepath"
"reflect"
@ -1022,7 +1021,7 @@ func dumpNodeHeader(w io.Writer, n Node) {
name := strings.TrimSuffix(tf.Name, "_")
vf := v.Field(i)
vfi := vf.Interface()
if name == "Offset" && vfi == types.BADWIDTH || name != "Offset" && isZero(vf) {
if name == "Offset" && vfi == types.BADWIDTH || name != "Offset" && vf.IsZero() {
continue
}
if vfi == true {
@ -1261,40 +1260,3 @@ func dumpNodes(w io.Writer, list Nodes, depth int) {
dumpNode(w, n, depth)
}
}
// reflect.IsZero is not available in Go 1.4 (added in Go 1.13), so we use this copy instead.
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return math.Float64bits(v.Float()) == 0
case reflect.Complex64, reflect.Complex128:
c := v.Complex()
return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if !isZero(v.Index(i)) {
return false
}
}
return true
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return v.IsNil()
case reflect.String:
return v.Len() == 0
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if !isZero(v.Field(i)) {
return false
}
}
return true
default:
return false
}
}