aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2011-10-19 18:26:08 -0200
committerGustavo Niemeyer <gustavo@niemeyer.net>2011-10-19 18:26:08 -0200
commit526d0818cca60a021e8c3c5ca34f7ed7d43f61ae (patch)
treedf73918cf449207b843ed5edb96528fa5d0e77f4
parentb0ec32db117a750d6259a601b37d5bd89ff44a2b (diff)
downloadgo-526d0818cca60a021e8c3c5ca34f7ed7d43f61ae.tar.gz
go-526d0818cca60a021e8c3c5ca34f7ed7d43f61ae.zip
fmt: don't panic formatting nil interfaces
R=golang-dev, r CC=golang-dev https://golang.org/cl/5296044
-rw-r--r--src/pkg/fmt/fmt_test.go5
-rw-r--r--src/pkg/fmt/print.go2
2 files changed, 6 insertions, 1 deletions
diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go
index 030ad6187f..38280d61f6 100644
--- a/src/pkg/fmt/fmt_test.go
+++ b/src/pkg/fmt/fmt_test.go
@@ -88,6 +88,10 @@ type S struct {
G G // a struct field that GoStrings
}
+type SI struct {
+ I interface{}
+}
+
// A type with a String method with pointer receiver for testing %p
type P int
@@ -352,6 +356,7 @@ var fmttests = []struct {
{"%#v", map[string]int{"a": 1}, `map[string] int{"a":1}`},
{"%#v", map[string]B{"a": {1, 2}}, `map[string] fmt_test.B{"a":fmt_test.B{I:1, j:2}}`},
{"%#v", []string{"a", "b"}, `[]string{"a", "b"}`},
+ {"%#v", SI{}, `fmt_test.SI{I:interface { }(nil)}`},
// 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 7721e72411..ba50b93283 100644
--- a/src/pkg/fmt/print.go
+++ b/src/pkg/fmt/print.go
@@ -842,7 +842,7 @@ BigSwitch:
value := f.Elem()
if !value.IsValid() {
if goSyntax {
- p.buf.WriteString(value.Type().String())
+ p.buf.WriteString(f.Type().String())
p.buf.Write(nilParenBytes)
} else {
p.buf.Write(nilAngleBytes)