Commit 773e8946 authored by Martin Möhrmann's avatar Martin Möhrmann

fmt: print values for map keys with non-reflexive equality

Previously fmt would first obtain a list of map keys
and then look up the value for each key. Since NaNs can
be map keys but cannot be fetched directly, the lookup would
fail and return a zero reflect.Value, which formats as <nil>.

golang.org/cl/33572 added a map iterator to the reflect package
that is used in this CL to retrieve the key and value from
the map and prints the correct value even for keys that are not
equal to themselves.

Fixes #14427

Change-Id: I9e1522959760b3de8b7ecf7a6e67cd603339632a
Reviewed-on: https://go-review.googlesource.com/129777Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 34c58fe1
...@@ -861,13 +861,8 @@ var fmtTests = []struct { ...@@ -861,13 +861,8 @@ var fmtTests = []struct {
// Extra argument errors should format without flags set. // Extra argument errors should format without flags set.
{"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"}, {"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"},
// The "<nil>" show up because maps are printed by // Test that maps with non-reflexive keys print all keys and values.
// first obtaining a list of keys and then looking up {"%v", map[float64]int{NaN: 1, NaN: 1}, "map[NaN:1 NaN:1]"},
// each key. Since NaNs can be map keys but cannot
// be fetched directly, the lookup fails and returns a
// zero reflect.Value, which formats as <nil>.
// This test is just to check that it shows the two NaNs at all.
{"%v", map[float64]int{NaN: 1, NaN: 2}, "map[NaN:<nil> NaN:<nil>]"},
// Comparison of padding rules with C printf. // Comparison of padding rules with C printf.
/* /*
...@@ -1033,7 +1028,7 @@ var fmtTests = []struct { ...@@ -1033,7 +1028,7 @@ var fmtTests = []struct {
{"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"}, {"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"},
{"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"}, {"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
{"%☠", reflect.Value{}, "<invalid reflect.Value>"}, {"%☠", reflect.Value{}, "<invalid reflect.Value>"},
{"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(<nil>)]"}, {"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"},
} }
// zeroFill generates zero-filled strings of the specified width. The length // zeroFill generates zero-filled strings of the specified width. The length
......
...@@ -743,8 +743,8 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) { ...@@ -743,8 +743,8 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
} else { } else {
p.buf.WriteString(mapString) p.buf.WriteString(mapString)
} }
keys := f.MapKeys() iter := f.MapRange()
for i, key := range keys { for i := 0; iter.Next(); i++ {
if i > 0 { if i > 0 {
if p.fmt.sharpV { if p.fmt.sharpV {
p.buf.WriteString(commaSpaceString) p.buf.WriteString(commaSpaceString)
...@@ -752,9 +752,9 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) { ...@@ -752,9 +752,9 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
p.buf.WriteByte(' ') p.buf.WriteByte(' ')
} }
} }
p.printValue(key, verb, depth+1) p.printValue(iter.Key(), verb, depth+1)
p.buf.WriteByte(':') p.buf.WriteByte(':')
p.printValue(f.MapIndex(key), verb, depth+1) p.printValue(iter.Value(), verb, depth+1)
} }
if p.fmt.sharpV { if p.fmt.sharpV {
p.buf.WriteByte('}') p.buf.WriteByte('}')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment