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

fmt: set p.field before nil check

Fixes #3752.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6331062
This commit is contained in:
Rob Pike 2012-06-25 16:48:20 -07:00
parent d1537809ba
commit a308be5fa8
2 changed files with 15 additions and 2 deletions

View File

@ -851,3 +851,15 @@ func TestIsSpace(t *testing.T) {
}
}
}
func TestNilDoesNotBecomeTyped(t *testing.T) {
type A struct{}
type B struct{}
var a *A = nil
var b B = B{}
got := Sprintf("%s %s %s %s %s", nil, a, nil, b, nil)
const expect = "%!s(<nil>) %!s(*fmt_test.A=<nil>) %!s(<nil>) {} %!s(<nil>)"
if got != expect {
t.Errorf("expected:\n\t%q\ngot:\n\t%q", expect, got)
}
}

View File

@ -712,6 +712,9 @@ func (p *pp) handleMethods(verb rune, plus, goSyntax bool, depth int) (wasString
}
func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth int) (wasString bool) {
p.field = field
p.value = reflect.Value{}
if field == nil {
if verb == 'T' || verb == 'v' {
p.buf.Write(nilAngleBytes)
@ -721,8 +724,6 @@ func (p *pp) printField(field interface{}, verb rune, plus, goSyntax bool, depth
return false
}
p.field = field
p.value = reflect.Value{}
// Special processing considerations.
// %T (the value's type) and %p (its address) are special; we always do them first.
switch verb {