Commit a308be5f authored by Rob Pike's avatar Rob Pike

fmt: set p.field before nil check

Fixes #3752.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6331062
parent d1537809
......@@ -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)
}
}
......@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment