From ee3c272611ab59ee68399596e5fb764b81a9dd8d Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 6 Jun 2012 15:08:00 -0400 Subject: [PATCH] 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 --- src/pkg/fmt/fmt_test.go | 1 + src/pkg/fmt/print.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index a7632de8ee..e7a5bd82aa 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -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]`}, diff --git a/src/pkg/fmt/print.go b/src/pkg/fmt/print.go index c730b18e9f..5007de2e39 100644 --- a/src/pkg/fmt/print.go +++ b/src/pkg/fmt/print.go @@ -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 -- 2.48.1