mirror of https://github.com/golang/go.git
src/log/slog: disallow == on Values
Comparing two Values with == is sensitive to the internal
representation of Values, and may not correspond to
equality on the Go values they represent. For example,
StringValue("X") != StringValue(strings.ToUpper("x"))
because Go ends up doing a pointer comparison on the data
stored in the Values.
So make Values non-comparable by adding a non-comparable field.
Updates #56345.
Change-Id: Ieedbf454e631cda10bc6fcf470b57d3f1d2182cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/479516
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
parent
9368210508
commit
d49b11be1d
|
|
@ -82,3 +82,7 @@ func (a Attr) Equal(b Attr) bool {
|
||||||
func (a Attr) String() string {
|
func (a Attr) String() string {
|
||||||
return fmt.Sprintf("%s=%s", a.Key, a.Value)
|
return fmt.Sprintf("%s=%s", a.Key, a.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a Attr) isEmpty() bool {
|
||||||
|
return a.Key == "" && a.Value.num == 0 && a.Value.any == nil
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ func (r *Record) AddAttrs(attrs ...Attr) {
|
||||||
// and seeing if the Attr there is non-zero.
|
// and seeing if the Attr there is non-zero.
|
||||||
if cap(r.back) > len(r.back) {
|
if cap(r.back) > len(r.back) {
|
||||||
end := r.back[:len(r.back)+1][len(r.back)]
|
end := r.back[:len(r.back)+1][len(r.back)]
|
||||||
if end != (Attr{}) {
|
if !end.isEmpty() {
|
||||||
panic("copies of a slog.Record were both modified")
|
panic("copies of a slog.Record were both modified")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ import (
|
||||||
// it can represent most small values without an allocation.
|
// it can represent most small values without an allocation.
|
||||||
// The zero Value corresponds to nil.
|
// The zero Value corresponds to nil.
|
||||||
type Value struct {
|
type Value struct {
|
||||||
|
_ [0]func() // disallow ==
|
||||||
// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
|
// num holds the value for Kinds Int64, Uint64, Float64, Bool and Duration,
|
||||||
// the string length for KindString, and nanoseconds since the epoch for KindTime.
|
// the string length for KindString, and nanoseconds since the epoch for KindTime.
|
||||||
num uint64
|
num uint64
|
||||||
|
|
@ -371,7 +372,7 @@ func (v Value) group() []Attr {
|
||||||
|
|
||||||
//////////////// Other
|
//////////////// Other
|
||||||
|
|
||||||
// Equal reports whether v and w have equal keys and values.
|
// Equal reports whether v and w represent the same Go value.
|
||||||
func (v Value) Equal(w Value) bool {
|
func (v Value) Equal(w Value) bool {
|
||||||
k1 := v.Kind()
|
k1 := v.Kind()
|
||||||
k2 := w.Kind()
|
k2 := w.Kind()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue