]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: fix inadvertent change to %#v
authorRuss Cox <rsc@golang.org>
Wed, 6 Jun 2012 19:08:00 +0000 (15:08 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 6 Jun 2012 19:08:00 +0000 (15:08 -0400)
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

src/pkg/fmt/fmt_test.go
src/pkg/fmt/print.go

index a7632de8eeb3e94be2b51d1964080ad19c146d18..e7a5bd82aa8f71379ce8f2ba844dd27f55c6f623 100644 (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]`},
index c730b18e9f0db7bf99a9d25740bd12b765dd3bae..5007de2e39e17468858add4c57ce6e65d40e0373 100644 (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