Home | History | Annotate | Download | only in testdata
      1 // Copyright 2010 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 // This file contains tests for the printf checker.
      6 
      7 // TODO(rsc): The user-defined wrapper tests are commented out
      8 // because they produced too many false positives when vet was
      9 // enabled during go test. See the TODO in ../print.go for a plan
     10 // to fix that; when it's fixed, uncomment the user-defined wrapper tests.
     11 
     12 package testdata
     13 
     14 import (
     15 	"fmt"
     16 	. "fmt"
     17 	"math"
     18 	"os"
     19 	"testing"
     20 	"unsafe" // just for test case printing unsafe.Pointer
     21 	// For testing printf-like functions from external package.
     22 	// "github.com/foobar/externalprintf"
     23 )
     24 
     25 func UnsafePointerPrintfTest() {
     26 	var up unsafe.Pointer
     27 	fmt.Printf("%p, %x %X", up, up, up)
     28 }
     29 
     30 // Error methods that do not satisfy the Error interface and should be checked.
     31 type errorTest1 int
     32 
     33 func (errorTest1) Error(...interface{}) string {
     34 	return "hi"
     35 }
     36 
     37 type errorTest2 int // Analogous to testing's *T type.
     38 func (errorTest2) Error(...interface{}) {
     39 }
     40 
     41 type errorTest3 int
     42 
     43 func (errorTest3) Error() { // No return value.
     44 }
     45 
     46 type errorTest4 int
     47 
     48 func (errorTest4) Error() int { // Different return type.
     49 	return 3
     50 }
     51 
     52 type errorTest5 int
     53 
     54 func (errorTest5) error() { // niladic; don't complain if no args (was bug)
     55 }
     56 
     57 // This function never executes, but it serves as a simple test for the program.
     58 // Test with make test.
     59 func PrintfTests() {
     60 	var b bool
     61 	var i int
     62 	var r rune
     63 	var s string
     64 	var x float64
     65 	var p *int
     66 	var imap map[int]int
     67 	var fslice []float64
     68 	var c complex64
     69 	// Some good format/argtypes
     70 	fmt.Printf("")
     71 	fmt.Printf("%b %b %b", 3, i, x)
     72 	fmt.Printf("%c %c %c %c", 3, i, 'x', r)
     73 	fmt.Printf("%d %d %d", 3, i, imap)
     74 	fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
     75 	fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
     76 	fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
     77 	fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
     78 	fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
     79 	fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
     80 	fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
     81 	fmt.Printf("%o %o", 3, i)
     82 	fmt.Printf("%p %p", p, nil)
     83 	fmt.Printf("%q %q %q %q", 3, i, 'x', r)
     84 	fmt.Printf("%s %s %s", "hi", s, []byte{65})
     85 	fmt.Printf("%t %t", true, b)
     86 	fmt.Printf("%T %T", 3, i)
     87 	fmt.Printf("%U %U", 3, i)
     88 	fmt.Printf("%v %v", 3, i)
     89 	fmt.Printf("%x %x %x %x", 3, i, "hi", s)
     90 	fmt.Printf("%X %X %X %X", 3, i, "hi", s)
     91 	fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
     92 	fmt.Printf("%s", &stringerv)
     93 	fmt.Printf("%v", &stringerv)
     94 	fmt.Printf("%T", &stringerv)
     95 	fmt.Printf("%s", &embeddedStringerv)
     96 	fmt.Printf("%v", &embeddedStringerv)
     97 	fmt.Printf("%T", &embeddedStringerv)
     98 	fmt.Printf("%v", notstringerv)
     99 	fmt.Printf("%T", notstringerv)
    100 	fmt.Printf("%q", stringerarrayv)
    101 	fmt.Printf("%v", stringerarrayv)
    102 	fmt.Printf("%s", stringerarrayv)
    103 	fmt.Printf("%v", notstringerarrayv)
    104 	fmt.Printf("%T", notstringerarrayv)
    105 	fmt.Printf("%d", new(Formatter))
    106 	fmt.Printf("%*%", 2)               // Ridiculous but allowed.
    107 	fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
    108 
    109 	fmt.Printf("%g", 1+2i)
    110 	fmt.Printf("%#e %#E %#f %#F %#g %#G", 1.2, 1.2, 1.2, 1.2, 1.2, 1.2) // OK since Go 1.9
    111 	// Some bad format/argTypes
    112 	fmt.Printf("%b", "hi")                      // ERROR "Printf format %b has arg \x22hi\x22 of wrong type string"
    113 	fmt.Printf("%t", c)                         // ERROR "Printf format %t has arg c of wrong type complex64"
    114 	fmt.Printf("%t", 1+2i)                      // ERROR "Printf format %t has arg 1 \+ 2i of wrong type complex128"
    115 	fmt.Printf("%c", 2.3)                       // ERROR "Printf format %c has arg 2.3 of wrong type float64"
    116 	fmt.Printf("%d", 2.3)                       // ERROR "Printf format %d has arg 2.3 of wrong type float64"
    117 	fmt.Printf("%e", "hi")                      // ERROR "Printf format %e has arg \x22hi\x22 of wrong type string"
    118 	fmt.Printf("%E", true)                      // ERROR "Printf format %E has arg true of wrong type bool"
    119 	fmt.Printf("%f", "hi")                      // ERROR "Printf format %f has arg \x22hi\x22 of wrong type string"
    120 	fmt.Printf("%F", 'x')                       // ERROR "Printf format %F has arg 'x' of wrong type rune"
    121 	fmt.Printf("%g", "hi")                      // ERROR "Printf format %g has arg \x22hi\x22 of wrong type string"
    122 	fmt.Printf("%g", imap)                      // ERROR "Printf format %g has arg imap of wrong type map\[int\]int"
    123 	fmt.Printf("%G", i)                         // ERROR "Printf format %G has arg i of wrong type int"
    124 	fmt.Printf("%o", x)                         // ERROR "Printf format %o has arg x of wrong type float64"
    125 	fmt.Printf("%p", 23)                        // ERROR "Printf format %p has arg 23 of wrong type int"
    126 	fmt.Printf("%q", x)                         // ERROR "Printf format %q has arg x of wrong type float64"
    127 	fmt.Printf("%s", b)                         // ERROR "Printf format %s has arg b of wrong type bool"
    128 	fmt.Printf("%s", byte(65))                  // ERROR "Printf format %s has arg byte\(65\) of wrong type byte"
    129 	fmt.Printf("%t", 23)                        // ERROR "Printf format %t has arg 23 of wrong type int"
    130 	fmt.Printf("%U", x)                         // ERROR "Printf format %U has arg x of wrong type float64"
    131 	fmt.Printf("%x", nil)                       // ERROR "Printf format %x has arg nil of wrong type untyped nil"
    132 	fmt.Printf("%X", 2.3)                       // ERROR "Printf format %X has arg 2.3 of wrong type float64"
    133 	fmt.Printf("%s", stringerv)                 // ERROR "Printf format %s has arg stringerv of wrong type testdata.ptrStringer"
    134 	fmt.Printf("%t", stringerv)                 // ERROR "Printf format %t has arg stringerv of wrong type testdata.ptrStringer"
    135 	fmt.Printf("%s", embeddedStringerv)         // ERROR "Printf format %s has arg embeddedStringerv of wrong type testdata.embeddedStringer"
    136 	fmt.Printf("%t", embeddedStringerv)         // ERROR "Printf format %t has arg embeddedStringerv of wrong type testdata.embeddedStringer"
    137 	fmt.Printf("%q", notstringerv)              // ERROR "Printf format %q has arg notstringerv of wrong type testdata.notstringer"
    138 	fmt.Printf("%t", notstringerv)              // ERROR "Printf format %t has arg notstringerv of wrong type testdata.notstringer"
    139 	fmt.Printf("%t", stringerarrayv)            // ERROR "Printf format %t has arg stringerarrayv of wrong type testdata.stringerarray"
    140 	fmt.Printf("%t", notstringerarrayv)         // ERROR "Printf format %t has arg notstringerarrayv of wrong type testdata.notstringerarray"
    141 	fmt.Printf("%q", notstringerarrayv)         // ERROR "Printf format %q has arg notstringerarrayv of wrong type testdata.notstringerarray"
    142 	fmt.Printf("%d", BoolFormatter(true))       // ERROR "Printf format %d has arg BoolFormatter\(true\) of wrong type testdata.BoolFormatter"
    143 	fmt.Printf("%z", FormatterVal(true))        // correct (the type is responsible for formatting)
    144 	fmt.Printf("%d", FormatterVal(true))        // correct (the type is responsible for formatting)
    145 	fmt.Printf("%s", nonemptyinterface)         // correct (the type is responsible for formatting)
    146 	fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // ERROR "Printf format %6g has arg 'x' of wrong type rune"
    147 	fmt.Println()                               // not an error
    148 	fmt.Println("%s", "hi")                     // ERROR "Println call has possible formatting directive %s"
    149 	fmt.Println("%v", "hi")                     // ERROR "Println call has possible formatting directive %v"
    150 	fmt.Println("0.0%")                         // correct (trailing % couldn't be a formatting directive)
    151 	fmt.Printf("%s", "hi", 3)                   // ERROR "Printf call needs 1 arg but has 2 args"
    152 	_ = fmt.Sprintf("%"+("s"), "hi", 3)         // ERROR "Sprintf call needs 1 arg but has 2 args"
    153 	fmt.Printf("%s%%%d", "hi", 3)               // correct
    154 	fmt.Printf("%08s", "woo")                   // correct
    155 	fmt.Printf("% 8s", "woo")                   // correct
    156 	fmt.Printf("%.*d", 3, 3)                    // correct
    157 	fmt.Printf("%.*d x", 3, 3, 3, 3)            // ERROR "Printf call needs 2 args but has 4 args"
    158 	fmt.Printf("%.*d x", "hi", 3)               // ERROR "Printf format %.*d uses non-int \x22hi\x22 as argument of \*"
    159 	fmt.Printf("%.*d x", i, 3)                  // correct
    160 	fmt.Printf("%.*d x", s, 3)                  // ERROR "Printf format %.\*d uses non-int s as argument of \*"
    161 	fmt.Printf("%*% x", 0.22)                   // ERROR "Printf format %\*% uses non-int 0.22 as argument of \*"
    162 	fmt.Printf("%q %q", multi()...)             // ok
    163 	fmt.Printf("%#q", `blah`)                   // ok
    164 	// printf("now is the time", "buddy")          // no error "printf call has arguments but no formatting directives"
    165 	Printf("now is the time", "buddy") // ERROR "Printf call has arguments but no formatting directives"
    166 	Printf("hi")                       // ok
    167 	const format = "%s %s\n"
    168 	Printf(format, "hi", "there")
    169 	Printf(format, "hi")              // ERROR "Printf format %s reads arg #2, but call has only 1 arg$"
    170 	Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has only 2 args"
    171 	f := new(ptrStringer)
    172 	f.Warn(0, "%s", "hello", 3)           // ERROR "Warn call has possible formatting directive %s"
    173 	f.Warnf(0, "%s", "hello", 3)          // ERROR "Warnf call needs 1 arg but has 2 args"
    174 	f.Warnf(0, "%r", "hello")             // ERROR "Warnf format %r has unknown verb r"
    175 	f.Warnf(0, "%#s", "hello")            // ERROR "Warnf format %#s has unrecognized flag #"
    176 	fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
    177 	Printf("d%", 2)                       // ERROR "Printf format % is missing verb at end of string"
    178 	Printf("%d", percentDV)
    179 	Printf("%d", &percentDV)
    180 	Printf("%d", notPercentDV)  // ERROR "Printf format %d has arg notPercentDV of wrong type testdata.notPercentDStruct"
    181 	Printf("%d", &notPercentDV) // ERROR "Printf format %d has arg &notPercentDV of wrong type \*testdata.notPercentDStruct"
    182 	Printf("%p", &notPercentDV) // Works regardless: we print it as a pointer.
    183 	Printf("%s", percentSV)
    184 	Printf("%s", &percentSV)
    185 	// Good argument reorderings.
    186 	Printf("%[1]d", 3)
    187 	Printf("%[1]*d", 3, 1)
    188 	Printf("%[2]*[1]d", 1, 3)
    189 	Printf("%[2]*.[1]*[3]d", 2, 3, 4)
    190 	fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
    191 	// Bad argument reorderings.
    192 	Printf("%[xd", 3)                      // ERROR "Printf format %\[xd is missing closing \]"
    193 	Printf("%[x]d x", 3)                   // ERROR "Printf format has invalid argument index \[x\]"
    194 	Printf("%[3]*s x", "hi", 2)            // ERROR "Printf format has invalid argument index \[3\]"
    195 	_ = fmt.Sprintf("%[3]d x", 2)          // ERROR "Sprintf format has invalid argument index \[3\]"
    196 	Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // ERROR "Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*"
    197 	Printf("%[0]s x", "arg1")              // ERROR "Printf format has invalid argument index \[0\]"
    198 	Printf("%[0]d x", 1)                   // ERROR "Printf format has invalid argument index \[0\]"
    199 	// Something that satisfies the error interface.
    200 	var e error
    201 	fmt.Println(e.Error()) // ok
    202 	// Something that looks like an error interface but isn't, such as the (*T).Error method
    203 	// in the testing package.
    204 	var et1 *testing.T
    205 	et1.Error()        // ok
    206 	et1.Error("hi")    // ok
    207 	et1.Error("%d", 3) // ERROR "Error call has possible formatting directive %d"
    208 	var et3 errorTest3
    209 	et3.Error() // ok, not an error method.
    210 	var et4 errorTest4
    211 	et4.Error() // ok, not an error method.
    212 	var et5 errorTest5
    213 	et5.error() // ok, not an error method.
    214 	// Interfaces can be used with any verb.
    215 	var iface interface {
    216 		ToTheMadness() bool // Method ToTheMadness usually returns false
    217 	}
    218 	fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type
    219 	// Can't print a function.
    220 	Printf("%d", someFunction) // ERROR "Printf format %d arg someFunction is a func value, not called"
    221 	Printf("%v", someFunction) // ERROR "Printf format %v arg someFunction is a func value, not called"
    222 	Println(someFunction)      // ERROR "Println arg someFunction is a func value, not called"
    223 	Printf("%p", someFunction) // ok: maybe someone wants to see the pointer
    224 	Printf("%T", someFunction) // ok: maybe someone wants to see the type
    225 	// Bug: used to recur forever.
    226 	Printf("%p %x", recursiveStructV, recursiveStructV.next)
    227 	Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next)
    228 	Printf("%p %x", recursiveSliceV, recursiveSliceV)
    229 	Printf("%p %x", recursiveMapV, recursiveMapV)
    230 	// Special handling for Log.
    231 	math.Log(3) // OK
    232 	var t *testing.T
    233 	t.Log("%d", 3) // ERROR "Log call has possible formatting directive %d"
    234 	t.Logf("%d", 3)
    235 	t.Logf("%d", "hi") // ERROR "Logf format %d has arg \x22hi\x22 of wrong type string"
    236 
    237 	// Errorf(1, "%d", 3)    // OK
    238 	// Errorf(1, "%d", "hi") // no error "Errorf format %d has arg \x22hi\x22 of wrong type string"
    239 
    240 	// Multiple string arguments before variadic args
    241 	// errorf("WARNING", "foobar")            // OK
    242 	// errorf("INFO", "s=%s, n=%d", "foo", 1) // OK
    243 	// errorf("ERROR", "%d")                  // no error "errorf format %d reads arg #1, but call has only 0 args"
    244 
    245 	// Printf from external package
    246 	// externalprintf.Printf("%d", 42) // OK
    247 	// externalprintf.Printf("foobar") // OK
    248 	// level := 123
    249 	// externalprintf.Logf(level, "%d", 42)                        // OK
    250 	// externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK
    251 	// externalprintf.Logf(level, "%d")                            // no error "Logf format %d reads arg #1, but call has only 0 args"
    252 	// var formatStr = "%s %s"
    253 	// externalprintf.Sprintf(formatStr, "a", "b")     // OK
    254 	// externalprintf.Logf(level, formatStr, "a", "b") // OK
    255 
    256 	// user-defined Println-like functions
    257 	ss := &someStruct{}
    258 	ss.Log(someFunction, "foo")          // OK
    259 	ss.Error(someFunction, someFunction) // OK
    260 	ss.Println()                         // OK
    261 	ss.Println(1.234, "foo")             // OK
    262 	ss.Println(1, someFunction)          // no error "Println arg someFunction is a func value, not called"
    263 	ss.log(someFunction)                 // OK
    264 	ss.log(someFunction, "bar", 1.33)    // OK
    265 	ss.log(someFunction, someFunction)   // no error "log arg someFunction is a func value, not called"
    266 
    267 	// indexed arguments
    268 	Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4)             // OK
    269 	Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4)             // ERROR "Printf format has invalid argument index \[0\]"
    270 	Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4)            // ERROR "Printf format has invalid argument index \[-2\]"
    271 	Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[2234234234234\]"
    272 	Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3)             // ERROR "Printf format %-10d reads arg #4, but call has only 3 args"
    273 	Printf("%[1][3]d x", 1, 2)                            // ERROR "Printf format %\[1\]\[ has unknown verb \["
    274 	Printf("%[1]d x", 1, 2)                               // OK
    275 	Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5)          // OK
    276 
    277 	// wrote Println but meant Fprintln
    278 	Printf("%p\n", os.Stdout)   // OK
    279 	Println(os.Stdout, "hello") // ERROR "Println does not take io.Writer but has first arg os.Stdout"
    280 
    281 	Printf(someString(), "hello") // OK
    282 
    283 }
    284 
    285 func someString() string { return "X" }
    286 
    287 type someStruct struct{}
    288 
    289 // Log is non-variadic user-define Println-like function.
    290 // Calls to this func must be skipped when checking
    291 // for Println-like arguments.
    292 func (ss *someStruct) Log(f func(), s string) {}
    293 
    294 // Error is variadic user-define Println-like function.
    295 // Calls to this func mustn't be checked for Println-like arguments,
    296 // since variadic arguments type isn't interface{}.
    297 func (ss *someStruct) Error(args ...func()) {}
    298 
    299 // Println is variadic user-defined Println-like function.
    300 // Calls to this func must be checked for Println-like arguments.
    301 func (ss *someStruct) Println(args ...interface{}) {}
    302 
    303 // log is variadic user-defined Println-like function.
    304 // Calls to this func must be checked for Println-like arguments.
    305 func (ss *someStruct) log(f func(), args ...interface{}) {}
    306 
    307 // A function we use as a function value; it has no other purpose.
    308 func someFunction() {}
    309 
    310 /*
    311 // Printf is used by the test so we must declare it.
    312 func Printf(format string, args ...interface{}) {
    313 	panic("don't call - testing only")
    314 }
    315 
    316 // Println is used by the test so we must declare it.
    317 func Println(args ...interface{}) {
    318 	panic("don't call - testing only")
    319 }
    320 
    321 // Logf is used by the test so we must declare it.
    322 func Logf(format string, args ...interface{}) {
    323 	panic("don't call - testing only")
    324 }
    325 
    326 // Log is used by the test so we must declare it.
    327 func Log(args ...interface{}) {
    328 	panic("don't call - testing only")
    329 }
    330 */
    331 
    332 // printf is used by the test so we must declare it.
    333 func printf(format string, args ...interface{}) {
    334 	panic("don't call - testing only")
    335 }
    336 
    337 /*
    338 // Errorf is used by the test for a case in which the first parameter
    339 // is not a format string.
    340 func Errorf(i int, format string, args ...interface{}) {
    341 	panic("don't call - testing only")
    342 }
    343 
    344 // errorf is used by the test for a case in which the function accepts multiple
    345 // string parameters before variadic arguments
    346 func errorf(level, format string, args ...interface{}) {
    347 	panic("don't call - testing only")
    348 }
    349 */
    350 
    351 // multi is used by the test.
    352 func multi() []interface{} {
    353 	panic("don't call - testing only")
    354 }
    355 
    356 type stringer int
    357 
    358 func (stringer) String() string { return "string" }
    359 
    360 type ptrStringer float64
    361 
    362 var stringerv ptrStringer
    363 
    364 func (*ptrStringer) String() string {
    365 	return "string"
    366 }
    367 
    368 func (*ptrStringer) Warn(int, ...interface{}) string {
    369 	return "warn"
    370 }
    371 
    372 func (*ptrStringer) Warnf(int, string, ...interface{}) string {
    373 	return "warnf"
    374 }
    375 
    376 type embeddedStringer struct {
    377 	foo string
    378 	ptrStringer
    379 	bar int
    380 }
    381 
    382 var embeddedStringerv embeddedStringer
    383 
    384 type notstringer struct {
    385 	f float64
    386 }
    387 
    388 var notstringerv notstringer
    389 
    390 type stringerarray [4]float64
    391 
    392 func (stringerarray) String() string {
    393 	return "string"
    394 }
    395 
    396 var stringerarrayv stringerarray
    397 
    398 type notstringerarray [4]float64
    399 
    400 var notstringerarrayv notstringerarray
    401 
    402 var nonemptyinterface = interface {
    403 	f()
    404 }(nil)
    405 
    406 // A data type we can print with "%d".
    407 type percentDStruct struct {
    408 	a int
    409 	b []byte
    410 	c *float64
    411 }
    412 
    413 var percentDV percentDStruct
    414 
    415 // A data type we cannot print correctly with "%d".
    416 type notPercentDStruct struct {
    417 	a int
    418 	b []byte
    419 	c bool
    420 }
    421 
    422 var notPercentDV notPercentDStruct
    423 
    424 // A data type we can print with "%s".
    425 type percentSStruct struct {
    426 	a string
    427 	b []byte
    428 	C stringerarray
    429 }
    430 
    431 var percentSV percentSStruct
    432 
    433 type recursiveStringer int
    434 
    435 func (s recursiveStringer) String() string {
    436 	_ = fmt.Sprintf("%d", s)
    437 	_ = fmt.Sprintf("%#v", s)
    438 	_ = fmt.Sprintf("%v", s)  // ERROR "Sprintf format %v with arg s causes recursive String method call"
    439 	_ = fmt.Sprintf("%v", &s) // ERROR "Sprintf format %v with arg &s causes recursive String method call"
    440 	_ = fmt.Sprintf("%T", s)  // ok; does not recursively call String
    441 	return fmt.Sprintln(s)    // ERROR "Sprintln arg s causes recursive call to String method"
    442 }
    443 
    444 type recursivePtrStringer int
    445 
    446 func (p *recursivePtrStringer) String() string {
    447 	_ = fmt.Sprintf("%v", *p)
    448 	_ = fmt.Sprint(&p)     // ok; prints address
    449 	return fmt.Sprintln(p) // ERROR "Sprintln arg p causes recursive call to String method"
    450 }
    451 
    452 type BoolFormatter bool
    453 
    454 func (*BoolFormatter) Format(fmt.State, rune) {
    455 }
    456 
    457 // Formatter with value receiver
    458 type FormatterVal bool
    459 
    460 func (FormatterVal) Format(fmt.State, rune) {
    461 }
    462 
    463 type RecursiveSlice []RecursiveSlice
    464 
    465 var recursiveSliceV = &RecursiveSlice{}
    466 
    467 type RecursiveMap map[int]RecursiveMap
    468 
    469 var recursiveMapV = make(RecursiveMap)
    470 
    471 type RecursiveStruct struct {
    472 	next *RecursiveStruct
    473 }
    474 
    475 var recursiveStructV = &RecursiveStruct{}
    476 
    477 type RecursiveStruct1 struct {
    478 	next *RecursiveStruct2
    479 }
    480 
    481 type RecursiveStruct2 struct {
    482 	next *RecursiveStruct1
    483 }
    484 
    485 var recursiveStruct1V = &RecursiveStruct1{}
    486 
    487 type unexportedInterface struct {
    488 	f interface{}
    489 }
    490 
    491 // Issue 17798: unexported ptrStringer cannot be formatted.
    492 type unexportedStringer struct {
    493 	t ptrStringer
    494 }
    495 type unexportedStringerOtherFields struct {
    496 	s string
    497 	t ptrStringer
    498 	S string
    499 }
    500 
    501 // Issue 17798: unexported error cannot be formatted.
    502 type unexportedError struct {
    503 	e error
    504 }
    505 type unexportedErrorOtherFields struct {
    506 	s string
    507 	e error
    508 	S string
    509 }
    510 
    511 type errorer struct{}
    512 
    513 func (e errorer) Error() string { return "errorer" }
    514 
    515 type unexportedCustomError struct {
    516 	e errorer
    517 }
    518 
    519 type errorInterface interface {
    520 	error
    521 	ExtraMethod()
    522 }
    523 
    524 type unexportedErrorInterface struct {
    525 	e errorInterface
    526 }
    527 
    528 func UnexportedStringerOrError() {
    529 	fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
    530 	fmt.Printf("%s", unexportedInterface{3})     // ok; we can't see the problem
    531 
    532 	us := unexportedStringer{}
    533 	fmt.Printf("%s", us)  // ERROR "Printf format %s has arg us of wrong type testdata.unexportedStringer"
    534 	fmt.Printf("%s", &us) // ERROR "Printf format %s has arg &us of wrong type [*]testdata.unexportedStringer"
    535 
    536 	usf := unexportedStringerOtherFields{
    537 		s: "foo",
    538 		S: "bar",
    539 	}
    540 	fmt.Printf("%s", usf)  // ERROR "Printf format %s has arg usf of wrong type testdata.unexportedStringerOtherFields"
    541 	fmt.Printf("%s", &usf) // ERROR "Printf format %s has arg &usf of wrong type [*]testdata.unexportedStringerOtherFields"
    542 
    543 	ue := unexportedError{
    544 		e: &errorer{},
    545 	}
    546 	fmt.Printf("%s", ue)  // ERROR "Printf format %s has arg ue of wrong type testdata.unexportedError"
    547 	fmt.Printf("%s", &ue) // ERROR "Printf format %s has arg &ue of wrong type [*]testdata.unexportedError"
    548 
    549 	uef := unexportedErrorOtherFields{
    550 		s: "foo",
    551 		e: &errorer{},
    552 		S: "bar",
    553 	}
    554 	fmt.Printf("%s", uef)  // ERROR "Printf format %s has arg uef of wrong type testdata.unexportedErrorOtherFields"
    555 	fmt.Printf("%s", &uef) // ERROR "Printf format %s has arg &uef of wrong type [*]testdata.unexportedErrorOtherFields"
    556 
    557 	uce := unexportedCustomError{
    558 		e: errorer{},
    559 	}
    560 	fmt.Printf("%s", uce) // ERROR "Printf format %s has arg uce of wrong type testdata.unexportedCustomError"
    561 
    562 	uei := unexportedErrorInterface{}
    563 	fmt.Printf("%s", uei)       // ERROR "Printf format %s has arg uei of wrong type testdata.unexportedErrorInterface"
    564 	fmt.Println("foo\n", "bar") // not an error
    565 
    566 	fmt.Println("foo\n")  // ERROR "Println arg list ends with redundant newline"
    567 	fmt.Println("foo\\n") // not an error
    568 	fmt.Println(`foo\n`)  // not an error
    569 
    570 	intSlice := []int{3, 4}
    571 	fmt.Printf("%s", intSlice) // ERROR "Printf format %s has arg intSlice of wrong type \[\]int"
    572 	nonStringerArray := [1]unexportedStringer{{}}
    573 	fmt.Printf("%s", nonStringerArray)  // ERROR "Printf format %s has arg nonStringerArray of wrong type \[1\]testdata.unexportedStringer"
    574 	fmt.Printf("%s", []stringer{3, 4})  // not an error
    575 	fmt.Printf("%s", [2]stringer{3, 4}) // not an error
    576 }
    577 
    578 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
    579 // See issues 23598 and 23605.
    580 func DisableErrorForFlag0() {
    581 	fmt.Printf("%0t", true)
    582 }
    583