mirror of
https://github.com/golang/go
synced 2024-11-26 16:26:49 -07:00
text/template: compare reflect.Value instances differently
To avoid false positives from the reflectvaluecompare checker #43993 Use v.IsValid() instead of var zero reflect.Value v != zero Also avoid comparing directly with the singleton reflect.Value representing a missing value. Detect the missing value by type instead. Change-Id: I3a00d63cf61c077e7c7ae816474aa1f032be325b Reviewed-on: https://go-review.googlesource.com/c/go/+/308769 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
8d57f4dcef
commit
9a9aad449f
@ -94,6 +94,12 @@ type missingValType struct{}
|
|||||||
|
|
||||||
var missingVal = reflect.ValueOf(missingValType{})
|
var missingVal = reflect.ValueOf(missingValType{})
|
||||||
|
|
||||||
|
var missingValReflectType = reflect.TypeOf(missingValType{})
|
||||||
|
|
||||||
|
func isMissing(v reflect.Value) bool {
|
||||||
|
return v.IsValid() && v.Type() == missingValReflectType
|
||||||
|
}
|
||||||
|
|
||||||
// at marks the state to be on node n, for error reporting.
|
// at marks the state to be on node n, for error reporting.
|
||||||
func (s *state) at(node parse.Node) {
|
func (s *state) at(node parse.Node) {
|
||||||
s.node = node
|
s.node = node
|
||||||
@ -471,7 +477,7 @@ func (s *state) evalPipeline(dot reflect.Value, pipe *parse.PipeNode) (value ref
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *state) notAFunction(args []parse.Node, final reflect.Value) {
|
func (s *state) notAFunction(args []parse.Node, final reflect.Value) {
|
||||||
if len(args) > 1 || final != missingVal {
|
if len(args) > 1 || !isMissing(final) {
|
||||||
s.errorf("can't give argument to non-function %s", args[0])
|
s.errorf("can't give argument to non-function %s", args[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -629,7 +635,7 @@ func (s *state) evalField(dot reflect.Value, fieldName string, node parse.Node,
|
|||||||
if method := ptr.MethodByName(fieldName); method.IsValid() {
|
if method := ptr.MethodByName(fieldName); method.IsValid() {
|
||||||
return s.evalCall(dot, method, false, node, fieldName, args, final)
|
return s.evalCall(dot, method, false, node, fieldName, args, final)
|
||||||
}
|
}
|
||||||
hasArgs := len(args) > 1 || final != missingVal
|
hasArgs := len(args) > 1 || !isMissing(final)
|
||||||
// It's not a method; must be a field of a struct or an element of a map.
|
// It's not a method; must be a field of a struct or an element of a map.
|
||||||
switch receiver.Kind() {
|
switch receiver.Kind() {
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
@ -700,7 +706,7 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
|
|||||||
}
|
}
|
||||||
typ := fun.Type()
|
typ := fun.Type()
|
||||||
numIn := len(args)
|
numIn := len(args)
|
||||||
if final != missingVal {
|
if !isMissing(final) {
|
||||||
numIn++
|
numIn++
|
||||||
}
|
}
|
||||||
numFixed := len(args)
|
numFixed := len(args)
|
||||||
@ -763,7 +769,7 @@ func (s *state) evalCall(dot, fun reflect.Value, isBuiltin bool, node parse.Node
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Add final value if necessary.
|
// Add final value if necessary.
|
||||||
if final != missingVal {
|
if !isMissing(final) {
|
||||||
t := typ.In(typ.NumIn() - 1)
|
t := typ.In(typ.NumIn() - 1)
|
||||||
if typ.IsVariadic() {
|
if typ.IsVariadic() {
|
||||||
if numIn-1 < numFixed {
|
if numIn-1 < numFixed {
|
||||||
|
@ -438,7 +438,7 @@ func basicKind(v reflect.Value) (kind, error) {
|
|||||||
|
|
||||||
// isNil returns true if v is the zero reflect.Value, or nil of its type.
|
// isNil returns true if v is the zero reflect.Value, or nil of its type.
|
||||||
func isNil(v reflect.Value) bool {
|
func isNil(v reflect.Value) bool {
|
||||||
if v == zero {
|
if !v.IsValid() {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
switch v.Kind() {
|
switch v.Kind() {
|
||||||
|
Loading…
Reference in New Issue
Block a user