1
0
mirror of https://github.com/golang/go synced 2024-11-25 14:37:56 -07:00

fmt: diagnose invalid verb applied to pointer

Fixes #2851.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/5644048
This commit is contained in:
Russ Cox 2012-02-07 23:37:05 -05:00
parent 0bd53d2ce0
commit 00134fe8ef
2 changed files with 21 additions and 18 deletions

View File

@ -423,6 +423,7 @@ var fmttests = []struct {
{"p0=%p", new(int), "p0=0xPTR"}, {"p0=%p", new(int), "p0=0xPTR"},
{"p1=%s", &pValue, "p1=String(p)"}, // String method... {"p1=%s", &pValue, "p1=String(p)"}, // String method...
{"p2=%p", &pValue, "p2=0xPTR"}, // ... not called with %p {"p2=%p", &pValue, "p2=0xPTR"}, // ... not called with %p
{"p3=%p", (*int)(nil), "p3=0x0"},
{"p4=%#p", new(int), "p4=PTR"}, {"p4=%#p", new(int), "p4=PTR"},
// %p on non-pointers // %p on non-pointers
@ -431,6 +432,14 @@ var fmttests = []struct {
{"%p", make([]int, 1), "0xPTR"}, {"%p", make([]int, 1), "0xPTR"},
{"%p", 27, "%!p(int=27)"}, // not a pointer at all {"%p", 27, "%!p(int=27)"}, // not a pointer at all
// %q on pointers
{"%q", (*int)(nil), "%!q(*int=<nil>)"},
{"%q", new(int), "%!q(*int=0xPTR)"},
// %v on pointers formats 0 as <nil>
{"%v", (*int)(nil), "<nil>"},
{"%v", new(int), "0xPTR"},
// %d on Stringer should give integer if possible // %d on Stringer should give integer if possible
{"%s", time.Time{}.Month(), "January"}, {"%s", time.Time{}.Month(), "January"},
{"%d", time.Time{}.Month(), "1"}, {"%d", time.Time{}.Month(), "1"},

View File

@ -553,6 +553,14 @@ func (p *pp) fmtBytes(v []byte, verb rune, goSyntax bool, depth int) {
} }
func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) { func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
switch verb {
case 'p', 'v', 'b', 'd', 'o', 'x', 'X':
// ok
default:
p.badVerb(verb)
return
}
var u uintptr var u uintptr
switch value.Kind() { switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer: case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
@ -561,6 +569,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
p.badVerb(verb) p.badVerb(verb)
return return
} }
if goSyntax { if goSyntax {
p.add('(') p.add('(')
p.buf.WriteString(value.Type().String()) p.buf.WriteString(value.Type().String())
@ -572,6 +581,8 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
p.fmt0x64(uint64(u), true) p.fmt0x64(uint64(u), true)
} }
p.add(')') p.add(')')
} else if verb == 'v' && u == 0 {
p.buf.Write(nilAngleBytes)
} else { } else {
p.fmt0x64(uint64(u), !p.fmt.sharp) p.fmt0x64(uint64(u), !p.fmt.sharp)
} }
@ -929,24 +940,7 @@ BigSwitch:
break BigSwitch break BigSwitch
} }
} }
if goSyntax { fallthrough
p.buf.WriteByte('(')
p.buf.WriteString(value.Type().String())
p.buf.WriteByte(')')
p.buf.WriteByte('(')
if v == 0 {
p.buf.Write(nilBytes)
} else {
p.fmt0x64(uint64(v), true)
}
p.buf.WriteByte(')')
break
}
if v == 0 {
p.buf.Write(nilAngleBytes)
break
}
p.fmt0x64(uint64(v), true)
case reflect.Chan, reflect.Func, reflect.UnsafePointer: case reflect.Chan, reflect.Func, reflect.UnsafePointer:
p.fmtPointer(value, verb, goSyntax) p.fmtPointer(value, verb, goSyntax)
default: default: