Skip to content

Commit 773e894

Browse files
committed
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/129777 Reviewed-by: Alan Donovan <[email protected]>
1 parent 34c58fe commit 773e894

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/fmt/fmt_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -861,13 +861,8 @@ var fmtTests = []struct {
861861
// Extra argument errors should format without flags set.
862862
{"%010.2", "12345", "%!(NOVERB)%!(EXTRA string=12345)"},
863863

864-
// The "<nil>" show up because maps are printed by
865-
// first obtaining a list of keys and then looking up
866-
// each key. Since NaNs can be map keys but cannot
867-
// be fetched directly, the lookup fails and returns a
868-
// zero reflect.Value, which formats as <nil>.
869-
// This test is just to check that it shows the two NaNs at all.
870-
{"%v", map[float64]int{NaN: 1, NaN: 2}, "map[NaN:<nil> NaN:<nil>]"},
864+
// Test that maps with non-reflexive keys print all keys and values.
865+
{"%v", map[float64]int{NaN: 1, NaN: 1}, "map[NaN:1 NaN:1]"},
871866

872867
// Comparison of padding rules with C printf.
873868
/*
@@ -1033,7 +1028,7 @@ var fmtTests = []struct {
10331028
{"%☠", &[]interface{}{I(1), G(2)}, "&[%!☠(fmt_test.I=1) %!☠(fmt_test.G=2)]"},
10341029
{"%☠", SI{&[]interface{}{I(1), G(2)}}, "{%!☠(*[]interface {}=&[1 2])}"},
10351030
{"%☠", reflect.Value{}, "<invalid reflect.Value>"},
1036-
{"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(<nil>)]"},
1031+
{"%☠", map[float64]int{NaN: 1}, "map[%!☠(float64=NaN):%!☠(int=1)]"},
10371032
}
10381033

10391034
// zeroFill generates zero-filled strings of the specified width. The length

src/fmt/print.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,18 +743,18 @@ func (p *pp) printValue(value reflect.Value, verb rune, depth int) {
743743
} else {
744744
p.buf.WriteString(mapString)
745745
}
746-
keys := f.MapKeys()
747-
for i, key := range keys {
746+
iter := f.MapRange()
747+
for i := 0; iter.Next(); i++ {
748748
if i > 0 {
749749
if p.fmt.sharpV {
750750
p.buf.WriteString(commaSpaceString)
751751
} else {
752752
p.buf.WriteByte(' ')
753753
}
754754
}
755-
p.printValue(key, verb, depth+1)
755+
p.printValue(iter.Key(), verb, depth+1)
756756
p.buf.WriteByte(':')
757-
p.printValue(f.MapIndex(key), verb, depth+1)
757+
p.printValue(iter.Value(), verb, depth+1)
758758
}
759759
if p.fmt.sharpV {
760760
p.buf.WriteByte('}')

0 commit comments

Comments
 (0)