1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:34:40 -07:00

fmt: fix inadvertent change to %#v

The reordering speedup in CL 6245068 changed the semantics
of %#v by delaying the clearing of some flags.  Restore the old
semantics and add a test.

Fixes #3706.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6302048
This commit is contained in:
Russ Cox 2012-06-06 15:08:00 -04:00
parent 58993e514e
commit ee3c272611
2 changed files with 17 additions and 0 deletions

View File

@ -375,6 +375,7 @@ var fmttests = []struct {
{"%#v", &iarray, `&[4]interface {}{1, "hello", 2.5, interface {}(nil)}`},
{"%#v", map[int]byte(nil), `map[int]uint8(nil)`},
{"%#v", map[int]byte{}, `map[int]uint8{}`},
{"%#v", "foo", `"foo"`},
// slices with other formats
{"%#x", []int{1, 2, 15}, `[0x1 0x2 0xf]`},

View File

@ -734,6 +734,19 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
return false
}
// Clear flags for base formatters.
// handleMethods needs them, so we must restore them later.
// We could call handleMethods here and avoid this work, but
// handleMethods is expensive enough to be worth delaying.
oldPlus := p.fmt.plus
oldSharp := p.fmt.sharp
if plus {
p.fmt.plus = false
}
if goSyntax {
p.fmt.sharp = false
}
// Some types can be done without reflection.
switch f := field.(type) {
case bool:
@ -775,6 +788,9 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
p.fmtBytes(f, verb, goSyntax, depth)
wasString = verb == 's'
default:
// Restore flags in case handleMethods finds a Formatter.
p.fmt.plus = oldPlus
p.fmt.sharp = oldSharp
// If the type is not simple, it might have methods.
if wasString, handled := p.handleMethods(verb, plus, goSyntax, depth); handled {
return wasString