mirror of
https://github.com/golang/go
synced 2024-11-20 10:54:49 -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:
parent
d1537809ba
commit
a308be5fa8
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user