Файловый менеджер - Редактировать - /var/www/html/test.zip
Ðазад
PK ! (���� � switch3.gonu �[��� // errorcheck // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that erroneous switch statements are detected by the compiler. // Does not compile. package main type I interface { M() } func bad() { var i I var s string switch i { case s: // ERROR "mismatched types string and I|incompatible types" } switch s { case i: // ERROR "mismatched types I and string|incompatible types" } var m, m1 map[int]int switch m { case nil: case m1: // ERROR "can only compare map m to nil|map can only be compared to nil|cannot compare" default: } var a, a1 []int switch a { case nil: case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil|cannot compare" default: } var f, f1 func() switch f { case nil: case f1: // ERROR "can only compare func f to nil|func can only be compared to nil|cannot compare" default: } var ar, ar1 [4]func() switch ar { // ERROR "cannot switch on" case ar1: default: } var st, st1 struct{ f func() } switch st { // ERROR "cannot switch on" case st1: } } func good() { var i interface{} var s string switch i { case s: } switch s { case i: } } PK ! �D rotate1.gonu �[��� // runoutput ./rotate.go // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Generate test of bit rotations. // The output is compiled and run. package main const mode = 1 PK ! ��Mtf f nilptr2.gonu �[��� // run // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main func main() { ok := true for _, tt := range tests { func() { defer func() { if err := recover(); err == nil { println(tt.name, "did not panic") ok = false } }() tt.fn() }() } if !ok { println("BUG") } } var intp *int var slicep *[]byte var a10p *[10]int var a10Mp *[1<<20]int var structp *Struct var bigstructp *BigStruct var i int var m *M var m1 *M1 var m2 *M2 var V interface{} func use(x interface{}) { V = x } var tests = []struct{ name string fn func() }{ // Edit .+1,/^}/s/^[^ ].+/ {"&", func() { println(&) }},\n {"\&&", func() { println(\&&) }},/g {"*intp", func() { println(*intp) }}, {"&*intp", func() { println(&*intp) }}, {"*slicep", func() { println(*slicep) }}, {"&*slicep", func() { println(&*slicep) }}, {"(*slicep)[0]", func() { println((*slicep)[0]) }}, {"&(*slicep)[0]", func() { println(&(*slicep)[0]) }}, {"(*slicep)[i]", func() { println((*slicep)[i]) }}, {"&(*slicep)[i]", func() { println(&(*slicep)[i]) }}, {"*a10p", func() { use(*a10p) }}, {"&*a10p", func() { println(&*a10p) }}, {"a10p[0]", func() { println(a10p[0]) }}, {"&a10p[0]", func() { println(&a10p[0]) }}, {"a10p[i]", func() { println(a10p[i]) }}, {"&a10p[i]", func() { println(&a10p[i]) }}, {"*structp", func() { use(*structp) }}, {"&*structp", func() { println(&*structp) }}, {"structp.i", func() { println(structp.i) }}, {"&structp.i", func() { println(&structp.i) }}, {"structp.j", func() { println(structp.j) }}, {"&structp.j", func() { println(&structp.j) }}, {"structp.k", func() { println(structp.k) }}, {"&structp.k", func() { println(&structp.k) }}, {"structp.x[0]", func() { println(structp.x[0]) }}, {"&structp.x[0]", func() { println(&structp.x[0]) }}, {"structp.x[i]", func() { println(structp.x[i]) }}, {"&structp.x[i]", func() { println(&structp.x[i]) }}, {"structp.x[9]", func() { println(structp.x[9]) }}, {"&structp.x[9]", func() { println(&structp.x[9]) }}, {"structp.l", func() { println(structp.l) }}, {"&structp.l", func() { println(&structp.l) }}, {"*bigstructp", func() { use(*bigstructp) }}, {"&*bigstructp", func() { println(&*bigstructp) }}, {"bigstructp.i", func() { println(bigstructp.i) }}, {"&bigstructp.i", func() { println(&bigstructp.i) }}, {"bigstructp.j", func() { println(bigstructp.j) }}, {"&bigstructp.j", func() { println(&bigstructp.j) }}, {"bigstructp.k", func() { println(bigstructp.k) }}, {"&bigstructp.k", func() { println(&bigstructp.k) }}, {"bigstructp.x[0]", func() { println(bigstructp.x[0]) }}, {"&bigstructp.x[0]", func() { println(&bigstructp.x[0]) }}, {"bigstructp.x[i]", func() { println(bigstructp.x[i]) }}, {"&bigstructp.x[i]", func() { println(&bigstructp.x[i]) }}, {"bigstructp.x[9]", func() { println(bigstructp.x[9]) }}, {"&bigstructp.x[9]", func() { println(&bigstructp.x[9]) }}, {"bigstructp.x[100<<20]", func() { println(bigstructp.x[100<<20]) }}, {"&bigstructp.x[100<<20]", func() { println(&bigstructp.x[100<<20]) }}, {"bigstructp.l", func() { println(bigstructp.l) }}, {"&bigstructp.l", func() { println(&bigstructp.l) }}, {"m1.F()", func() { println(m1.F()) }}, {"m1.M.F()", func() { println(m1.M.F()) }}, {"m2.F()", func() { println(m2.F()) }}, {"m2.M.F()", func() { println(m2.M.F()) }}, } type Struct struct { i int j float64 k string x [10]int l []byte } type BigStruct struct { i int j float64 k string x [128<<20]byte l []byte } type M struct { } func (m *M) F() int {return 0} type M1 struct { M } type M2 struct { x int M } PK ! �Nؠr r if.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test if statements in various forms. package main func assertequal(is, shouldbe int, msg string) { if is != shouldbe { print("assertion fail", msg, "\n") panic(1) } } func main() { i5 := 5 i7 := 7 var count int count = 0 if true { count = count + 1 } assertequal(count, 1, "if true") count = 0 if false { count = count + 1 } assertequal(count, 0, "if false") count = 0 if one := 1; true { count = count + one } assertequal(count, 1, "if true one") count = 0 if one := 1; false { count = count + 1 _ = one } assertequal(count, 0, "if false one") count = 0 if i5 < i7 { count = count + 1 } assertequal(count, 1, "if cond") count = 0 if true { count = count + 1 } else { count = count - 1 } assertequal(count, 1, "if else true") count = 0 if false { count = count + 1 } else { count = count - 1 } assertequal(count, -1, "if else false") count = 0 if t := 1; false { count = count + 1 _ = t t := 7 _ = t } else { count = count - t } assertequal(count, -1, "if else false var") count = 0 t := 1 if false { count = count + 1 t := 7 _ = t } else { count = count - t } _ = t assertequal(count, -1, "if else false var outside") } PK ! ��l l closure4.gonu �[��� // run // Copyright 2018 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Check that calling a nil func causes a proper panic. package main func main() { defer func() { err := recover() if err == nil { panic("panic expected") } }() var f func() f() } PK ! I_�%� � ddd.gonu �[��� // run // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test variadic functions and calls (dot-dot-dot). package main func sum(args ...int) int { s := 0 for _, v := range args { s += v } return s } func sumC(args ...int) int { return func() int { return sum(args...) }() } var sumD = func(args ...int) int { return sum(args...) } var sumE = func() func(...int) int { return func(args ...int) int { return sum(args...) } }() var sumF = func(args ...int) func() int { return func() int { return sum(args...) } } func sumA(args []int) int { s := 0 for _, v := range args { s += v } return s } func sumB(args []int) int { return sum(args...) } func sum2(args ...int) int { return 2 * sum(args...) } func sum3(args ...int) int { return 3 * sumA(args) } func sum4(args ...int) int { return 4 * sumB(args) } func intersum(args ...interface{}) int { s := 0 for _, v := range args { s += v.(int) } return s } type T []T func ln(args ...T) int { return len(args) } func ln2(args ...T) int { return 2 * ln(args...) } func (*T) Sum(args ...int) int { return sum(args...) } type U struct { *T } type I interface { Sum(...int) int } func main() { if x := sum(1, 2, 3); x != 6 { println("sum 6", x) panic("fail") } if x := sum(); x != 0 { println("sum 0", x) panic("fail") } if x := sum(10); x != 10 { println("sum 10", x) panic("fail") } if x := sum(1, 8); x != 9 { println("sum 9", x) panic("fail") } if x := sumC(4, 5, 6); x != 15 { println("sumC 15", x) panic("fail") } if x := sumD(4, 5, 7); x != 16 { println("sumD 16", x) panic("fail") } if x := sumE(4, 5, 8); x != 17 { println("sumE 17", x) panic("fail") } if x := sumF(4, 5, 9)(); x != 18 { println("sumF 18", x) panic("fail") } if x := sum2(1, 2, 3); x != 2*6 { println("sum 6", x) panic("fail") } if x := sum2(); x != 2*0 { println("sum 0", x) panic("fail") } if x := sum2(10); x != 2*10 { println("sum 10", x) panic("fail") } if x := sum2(1, 8); x != 2*9 { println("sum 9", x) panic("fail") } if x := sum3(1, 2, 3); x != 3*6 { println("sum 6", x) panic("fail") } if x := sum3(); x != 3*0 { println("sum 0", x) panic("fail") } if x := sum3(10); x != 3*10 { println("sum 10", x) panic("fail") } if x := sum3(1, 8); x != 3*9 { println("sum 9", x) panic("fail") } if x := sum4(1, 2, 3); x != 4*6 { println("sum 6", x) panic("fail") } if x := sum4(); x != 4*0 { println("sum 0", x) panic("fail") } if x := sum4(10); x != 4*10 { println("sum 10", x) panic("fail") } if x := sum4(1, 8); x != 4*9 { println("sum 9", x) panic("fail") } if x := intersum(1, 2, 3); x != 6 { println("intersum 6", x) panic("fail") } if x := intersum(); x != 0 { println("intersum 0", x) panic("fail") } if x := intersum(10); x != 10 { println("intersum 10", x) panic("fail") } if x := intersum(1, 8); x != 9 { println("intersum 9", x) panic("fail") } if x := ln(nil, nil, nil); x != 3 { println("ln 3", x) panic("fail") } if x := ln([]T{}); x != 1 { println("ln 1", x) panic("fail") } if x := ln2(nil, nil, nil); x != 2*3 { println("ln2 3", x) panic("fail") } if x := ln2([]T{}); x != 2*1 { println("ln2 1", x) panic("fail") } if x := ((*T)(nil)).Sum(1, 3, 5, 7); x != 16 { println("(*T)(nil).Sum", x) panic("fail") } if x := (*T).Sum(nil, 1, 3, 5, 6); x != 15 { println("(*T).Sum", x) panic("fail") } if x := (&U{}).Sum(1, 3, 5, 5); x != 14 { println("(&U{}).Sum", x) panic("fail") } var u U if x := u.Sum(1, 3, 5, 4); x != 13 { println("u.Sum", x) panic("fail") } if x := (&u).Sum(1, 3, 5, 3); x != 12 { println("(&u).Sum", x) panic("fail") } var i interface { Sum(...int) int } = &u if x := i.Sum(2, 3, 5, 7); x != 17 { println("i(=&u).Sum", x) panic("fail") } i = u if x := i.Sum(2, 3, 5, 6); x != 16 { println("i(=u).Sum", x) panic("fail") } var s struct { I } s.I = &u if x := s.Sum(2, 3, 5, 8); x != 18 { println("s{&u}.Sum", x) panic("fail") } if x := (*U).Sum(&U{}, 1, 3, 5, 2); x != 11 { println("(*U).Sum", x) panic("fail") } if x := U.Sum(U{}, 1, 3, 5, 1); x != 10 { println("U.Sum", x) panic("fail") } } PK ! ����� � reflectmethod2.gonu �[��� // run // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // The linker can prune methods that are not directly called or // assigned to interfaces, but only if reflect.Type.MethodByName is // never used. Test it here. package main import reflect1 "reflect" var called = false type M int func (m M) UniqueMethodName() { called = true } var v M type MyType interface { MethodByName(string) (reflect1.Method, bool) } func main() { var t MyType = reflect1.TypeOf(v) m, _ := t.MethodByName("UniqueMethodName") m.Func.Interface().(func(M))(v) if !called { panic("UniqueMethodName not called") } } PK ! ��� switch5.gonu �[��� // errorcheck // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that switch statements with duplicate cases are detected by the compiler. // Does not compile. package main func f0(x int) { switch x { case 0: case 0: // ERROR "duplicate case (0 in switch)?" } switch x { case 0: case int(0): // ERROR "duplicate case (int.0. .value 0. in switch)?" } } func f1(x float32) { switch x { case 5: case 5: // ERROR "duplicate case (5 in switch)?" case 5.0: // ERROR "duplicate case (5 in switch)?" } } func f2(s string) { switch s { case "": case "": // ERROR "duplicate case (.. in switch)?" case "abc": case "abc": // ERROR "duplicate case (.abc. in switch)?" } } func f3(e interface{}) { switch e { case 0: case 0: // ERROR "duplicate case (0 in switch)?" case int64(0): case float32(10): case float32(10): // ERROR "duplicate case (float32\(10\) .value 10. in switch)?" case float64(10): case float64(10): // ERROR "duplicate case (float64\(10\) .value 10. in switch)?" } } func f5(a [1]int) { switch a { case [1]int{0}: case [1]int{0}: // OK -- see issue 15896 } } // Ensure duplicate const bool clauses are accepted. func f6() int { switch { case 0 == 0: return 0 case 1 == 1: // Intentionally OK, even though a duplicate of the above const true return 1 } return 2 } // Ensure duplicates in ranges are detected (issue #17517). func f7(a int) { switch a { case 0: case 0, 1: // ERROR "duplicate case 0" case 1, 2, 3, 4: // ERROR "duplicate case 1" } } // Ensure duplicates with simple literals are printed as they were // written, not just their values. Particularly useful for runes. func f8(r rune) { const x = 10 switch r { case 33, 33: // ERROR "duplicate case (33 in switch)?" case 34, '"': // ERROR "duplicate case '"' .value 34. in switch" case 35, rune('#'): // ERROR "duplicate case (rune.'#'. .value 35. in switch)?" case 36, rune(36): // ERROR "duplicate case (rune.36. .value 36. in switch)?" case 37, '$'+1: // ERROR "duplicate case ('\$' \+ 1 .value 37. in switch)?" case 'b': case 'a', 'b', 'c', 'd': // ERROR "duplicate case ('b' .value 98.)?" case x, x: // ERROR "duplicate case (x .value 10.)?" } } PK ! E�N� � mergemul.gonu �[��� // runoutput // Copyright 2017 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import "fmt" // Check that expressions like (c*n + d*(n+k)) get correctly merged by // the compiler into (c+d)*n + d*k (with c+d and d*k computed at // compile time). // // The merging is performed by a combination of the multiplication // merge rules // (c*n + d*n) -> (c+d)*n // and the distributive multiplication rules // c * (d+x) -> c*d + c*x // Generate a MergeTest that looks like this: // // a8, b8 = m1*n8 + m2*(n8+k), (m1+m2)*n8 + m2*k // if a8 != b8 { // // print error msg and panic // } func makeMergeAddTest(m1, m2, k int, size string) string { model := " a" + size + ", b" + size model += fmt.Sprintf(" = %%d*n%s + %%d*(n%s+%%d), (%%d+%%d)*n%s + (%%d*%%d)", size, size, size) test := fmt.Sprintf(model, m1, m2, k, m1, m2, m2, k) test += fmt.Sprintf(` if a%s != b%s { fmt.Printf("MergeAddTest(%d, %d, %d, %s) failed\n") fmt.Printf("%%d != %%d\n", a%s, b%s) panic("FAIL") } `, size, size, m1, m2, k, size, size, size) return test + "\n" } // Check that expressions like (c*n - d*(n+k)) get correctly merged by // the compiler into (c-d)*n - d*k (with c-d and d*k computed at // compile time). // // The merging is performed by a combination of the multiplication // merge rules // (c*n - d*n) -> (c-d)*n // and the distributive multiplication rules // c * (d-x) -> c*d - c*x // Generate a MergeTest that looks like this: // // a8, b8 = m1*n8 - m2*(n8+k), (m1-m2)*n8 - m2*k // if a8 != b8 { // // print error msg and panic // } func makeMergeSubTest(m1, m2, k int, size string) string { model := " a" + size + ", b" + size model += fmt.Sprintf(" = %%d*n%s - %%d*(n%s+%%d), (%%d-%%d)*n%s - (%%d*%%d)", size, size, size) test := fmt.Sprintf(model, m1, m2, k, m1, m2, m2, k) test += fmt.Sprintf(` if a%s != b%s { fmt.Printf("MergeSubTest(%d, %d, %d, %s) failed\n") fmt.Printf("%%d != %%d\n", a%s, b%s) panic("FAIL") } `, size, size, m1, m2, k, size, size, size) return test + "\n" } func makeAllSizes(m1, m2, k int) string { var tests string tests += makeMergeAddTest(m1, m2, k, "8") tests += makeMergeAddTest(m1, m2, k, "16") tests += makeMergeAddTest(m1, m2, k, "32") tests += makeMergeAddTest(m1, m2, k, "64") tests += makeMergeSubTest(m1, m2, k, "8") tests += makeMergeSubTest(m1, m2, k, "16") tests += makeMergeSubTest(m1, m2, k, "32") tests += makeMergeSubTest(m1, m2, k, "64") tests += "\n" return tests } func main() { fmt.Println(`package main import "fmt" var n8 int8 = 42 var n16 int16 = 42 var n32 int32 = 42 var n64 int64 = 42 func main() { var a8, b8 int8 var a16, b16 int16 var a32, b32 int32 var a64, b64 int64 `) fmt.Println(makeAllSizes(03, 05, 0)) // 3*n + 5*n fmt.Println(makeAllSizes(17, 33, 0)) fmt.Println(makeAllSizes(80, 45, 0)) fmt.Println(makeAllSizes(32, 64, 0)) fmt.Println(makeAllSizes(7, 11, +1)) // 7*n + 11*(n+1) fmt.Println(makeAllSizes(9, 13, +2)) fmt.Println(makeAllSizes(11, 16, -1)) fmt.Println(makeAllSizes(17, 9, -2)) fmt.Println("}") } PK ! �j��� � rename1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that renamed identifiers no longer have their old meaning. // Does not compile. package main func main() { var n byte // ERROR "not a type|expected type" var y = float32(0) // ERROR "cannot call|expected function" const ( a = 1 + iota // ERROR "invalid operation|incompatible types|cannot convert" ) _, _ = n, y } const ( append = 1 bool = 2 byte = 3 complex = 4 complex64 = 5 complex128 = 6 cap = 7 close = 8 delete = 9 error = 10 false = 11 float32 = 12 float64 = 13 imag = 14 int = 15 int8 = 16 int16 = 17 int32 = 18 int64 = 19 len = 20 make = 21 new = 22 nil = 23 panic = 24 print = 25 println = 26 real = 27 recover = 28 rune = 29 string = 30 true = 31 uint = 32 uint8 = 33 uint16 = 34 uint32 = 35 uint64 = 36 uintptr = 37 iota = "38" ) PK ! ��f� � escape_unsafe.gonu �[��� // errorcheck -0 -m -l // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test escape analysis for unsafe.Pointer rules. package escape import ( "reflect" "unsafe" ) // (1) Conversion of a *T1 to Pointer to *T2. func convert(p *float64) *uint64 { // ERROR "leaking param: p to result ~r0 level=0$" return (*uint64)(unsafe.Pointer(p)) } // (3) Conversion of a Pointer to a uintptr and back, with arithmetic. func arithAdd() unsafe.Pointer { var x [2]byte // ERROR "moved to heap: x" return unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + 1) } func arithSub() unsafe.Pointer { var x [2]byte // ERROR "moved to heap: x" return unsafe.Pointer(uintptr(unsafe.Pointer(&x[1])) - 1) } func arithMask() unsafe.Pointer { var x [2]byte // ERROR "moved to heap: x" return unsafe.Pointer(uintptr(unsafe.Pointer(&x[1])) &^ 1) } // (5) Conversion of the result of reflect.Value.Pointer or // reflect.Value.UnsafeAddr from uintptr to Pointer. // BAD: should be "leaking param: p to result ~r0 level=0$" func valuePointer(p *int) unsafe.Pointer { // ERROR "leaking param: p$" return unsafe.Pointer(reflect.ValueOf(p).Pointer()) } // BAD: should be "leaking param: p to result ~r0 level=0$" func valueUnsafeAddr(p *int) unsafe.Pointer { // ERROR "leaking param: p$" return unsafe.Pointer(reflect.ValueOf(p).Elem().UnsafeAddr()) } // (6) Conversion of a reflect.SliceHeader or reflect.StringHeader // Data field to or from Pointer. func fromSliceData(s []int) unsafe.Pointer { // ERROR "leaking param: s to result ~r0 level=0$" return unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&s)).Data) } func fromStringData(s string) unsafe.Pointer { // ERROR "leaking param: s to result ~r0 level=0$" return unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&s)).Data) } func toSliceData(s *[]int, p unsafe.Pointer) { // ERROR "s does not escape" "leaking param: p$" (*reflect.SliceHeader)(unsafe.Pointer(s)).Data = uintptr(p) } func toStringData(s *string, p unsafe.Pointer) { // ERROR "s does not escape" "leaking param: p$" (*reflect.StringHeader)(unsafe.Pointer(s)).Data = uintptr(p) } PK ! � U�< < intrinsic.dir/main.gonu �[��� // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import ( "fmt" T "runtime/internal/sys" ) var A = []uint64{0x0102030405060708, 0x1122334455667788} var B = []uint64{0x0807060504030201, 0x8877665544332211} var errors int func logf(f string, args ...interface{}) { errors++ fmt.Printf(f, args...) if errors > 100 { // 100 is enough spewage panic("100 errors is plenty is enough") } } func test(i int, x uint64) { t := T.TrailingZeros64(x) // ERROR "intrinsic substitution for TrailingZeros64" if i != t { logf("TrailingZeros64(0x%x) expected %d but got %d\n", x, i, t) } x = -x t = T.TrailingZeros64(x) // ERROR "intrinsic substitution for TrailingZeros64" if i != t { logf("TrailingZeros64(0x%x) expected %d but got %d\n", x, i, t) } if i <= 32 { x32 := uint32(x) t32 := T.TrailingZeros32(x32) // ERROR "intrinsic substitution for TrailingZeros32" if i != t32 { logf("TrailingZeros32(0x%x) expected %d but got %d\n", x32, i, t32) } x32 = -x32 t32 = T.TrailingZeros32(x32) // ERROR "intrinsic substitution for TrailingZeros32" if i != t32 { logf("TrailingZeros32(0x%x) expected %d but got %d\n", x32, i, t32) } } } func main() { // Test Bswap first because the other test relies on it // working correctly (to implement bit reversal). for i := range A { x := A[i] y := B[i] X := T.Bswap64(x) // ERROR "intrinsic substitution for Bswap64" Y := T.Bswap64(y) // ERROR "intrinsic substitution for Bswap64" if y != X { logf("Bswap64(0x%08x) expected 0x%08x but got 0x%08x\n", x, y, X) } if x != Y { logf("Bswap64(0x%08x) expected 0x%08x but got 0x%08x\n", y, x, Y) } x32 := uint32(X) y32 := uint32(Y >> 32) X32 := T.Bswap32(x32) // ERROR "intrinsic substitution for Bswap32" Y32 := T.Bswap32(y32) // ERROR "intrinsic substitution for Bswap32" if y32 != X32 { logf("Bswap32(0x%08x) expected 0x%08x but got 0x%08x\n", x32, y32, X32) } if x32 != Y32 { logf("Bswap32(0x%08x) expected 0x%08x but got 0x%08x\n", y32, x32, Y32) } } // Zero is a special case, be sure it is done right. if T.TrailingZeros32(0) != 32 { // ERROR "intrinsic substitution for TrailingZeros32" logf("TrailingZeros32(0) != 32") } if T.TrailingZeros64(0) != 64 { // ERROR "intrinsic substitution for TrailingZeros64" logf("TrailingZeros64(0) != 64") } for i := 0; i <= 64; i++ { for j := uint64(1); j <= 255; j += 2 { for k := uint64(1); k <= 65537; k += 128 { x := (j * k) << uint(i) test(i, x) } } } } PK ! �%Q� � README.mdnu �[��� The test directory contains tests of the Go tool chain and runtime. It includes black box tests, regression tests, and error output tests. They are run as part of all.bash. To run just these tests, execute: ../bin/go test cmd/internal/testdir To run just tests from specified files in this directory, execute: ../bin/go test cmd/internal/testdir -run='Test/(file1.go|file2.go|...)' Standard library tests should be written as regular Go tests in the appropriate package. The tool chain and runtime also have regular Go tests in their packages. The main reasons to add a new test to this directory are: * it is most naturally expressed using the test runner; or * it is also applicable to `gccgo` and other Go tool chains. PK ! ��� � func1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that result parameters are in the same scope as regular parameters. // Does not compile. package main func f1(a int) (int, float32) { return 7, 7.0 } func f2(a int) (a int, b float32) { // ERROR "duplicate argument a|definition|redeclared" return 8, 8.0 } PK ! 4b }G G ddd2.dir/ddd3.gonu �[��� // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that variadic functions work across package boundaries. package main import "./ddd2" func main() { if x := ddd.Sum(1, 2, 3); x != 6 { println("ddd.Sum 6", x) panic("fail") } if x := ddd.Sum(); x != 0 { println("ddd.Sum 0", x) panic("fail") } if x := ddd.Sum(10); x != 10 { println("ddd.Sum 10", x) panic("fail") } if x := ddd.Sum(1, 8); x != 9 { println("ddd.Sum 9", x) panic("fail") } } PK ! (d�@< < ddd2.dir/ddd2.gonu �[��� // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // This file is compiled and then imported by ddd3.go. package ddd func Sum(args ...int) int { s := 0 for _, v := range args { s += v } return s } PK ! �@�� atomicload.gonu �[��� // run // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Check that we do loads exactly once. The SSA backend // once tried to do the load in f twice, once sign extended // and once zero extended. This can cause problems in // racy code, particularly sync/mutex. package main func f(p *byte) bool { x := *p a := int64(int8(x)) b := int64(uint8(x)) return a == b } func main() { var x byte const N = 1000000 c := make(chan struct{}) go func() { for i := 0; i < N; i++ { x = 1 } c <- struct{}{} }() go func() { for i := 0; i < N; i++ { x = 2 } c <- struct{}{} }() for i := 0; i < N; i++ { if !f(&x) { panic("non-atomic load!") } } <-c <-c } PK ! �F�� � const3.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test typed integer constants. package main import "fmt" type T int func (t T) String() string { return fmt.Sprintf("T%d", int(t)) } const ( A T = 1 << (1 << iota) B C D E ) func main() { s := fmt.Sprintf("%v %v %v %v %v", A, B, C, D, E) if s != "T2 T4 T16 T256 T65536" { println("type info didn't propagate in const: got", s) panic("fail") } x := uint(5) y := float64(uint64(1)<<x) // used to fail to compile if y != 32 { println("wrong y", y) panic("fail") } } PK ! �;��B B funcdup.gonu �[��� // errorcheck // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package p type T interface { F1(i int) (i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" F2(i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" F3() (i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" } type T1 func(i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" type T2 func(i int) (i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" type T3 func() (i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" type R struct{} func (i *R) F1(i int) {} // ERROR "duplicate argument i|redefinition|previous|redeclared" func (i *R) F2() (i int) {return 0} // ERROR "duplicate argument i|redefinition|previous|redeclared" func (i *R) F3(j int) (j int) {return 0} // ERROR "duplicate argument j|redefinition|previous|redeclared" func F1(i, i int) {} // ERROR "duplicate argument i|redefinition|previous|redeclared" func F2(i int) (i int) {return 0} // ERROR "duplicate argument i|redefinition|previous|redeclared" func F3() (i, i int) {return 0, 0} // ERROR "duplicate argument i|redefinition|previous|redeclared" PK ! $E�� � func7.gonu �[��� // run // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test evaluation order in if condition. package main var calledf = false func f() int { calledf = true return 1 } func g() int { if !calledf { panic("BUG: func7 - called g before f") } return 0 } func main() { // gc used to evaluate g() before f(). if f() < g() { panic("wrong answer") } } PK ! 1C6� � funcdup2.gonu �[��� // errorcheck // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package p var T interface { F1(i int) (i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" F2(i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" F3() (i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" } var T1 func(i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" var T2 func(i int) (i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" var T3 func() (i, i int) // ERROR "duplicate argument i|redefinition|previous|redeclared" PK ! ����� � method2.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that pointers and interface types cannot be method receivers. // Does not compile. package main type T struct { a int } type P *T type P1 *T func (p P) val() int { return 1 } // ERROR "receiver.* pointer|invalid pointer or interface receiver|invalid receiver" func (p *P1) val() int { return 1 } // ERROR "receiver.* pointer|invalid pointer or interface receiver|invalid receiver" type I interface{} type I1 interface{} func (p I) val() int { return 1 } // ERROR "receiver.*interface|invalid pointer or interface receiver" func (p *I1) val() int { return 1 } // ERROR "receiver.*interface|invalid pointer or interface receiver" type Val interface { val() int } var _ = (*Val).val // ERROR "method|type \*Val is pointer to interface, not interface" var v Val var pv = &v var _ = pv.val() // ERROR "undefined|pointer to interface" var _ = pv.val // ERROR "undefined|pointer to interface" func (t *T) g() int { return t.a } var _ = (T).g() // ERROR "needs pointer receiver|undefined|method requires pointer|cannot call pointer method" PK ! #ij��. �. inline.gonu �[��� // errorcheckwithauto -0 -m -d=inlfuncswithclosures=1 //go:build !goexperiment.newinliner // Copyright 2015 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test, using compiler diagnostic flags, that inlining is working. // Compiles but does not run. package foo import ( "errors" "runtime" "unsafe" ) func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result" return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1" } func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result" return unsafe.Pointer(uintptr(p) + x) } func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result" return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1" } //go:noinline func g(x int) int { return x + 1 } func h(x int) int { // ERROR "can inline h" return x + 2 } func i(x int) int { // ERROR "can inline i" const y = 2 return x + y } func j(x int) int { // ERROR "can inline j" switch { case x > 0: return x + 2 default: return x + 1 } } func f2() int { // ERROR "can inline f2" tmp1 := h tmp2 := tmp1 return tmp2(0) // ERROR "inlining call to h" } var abc = errors.New("abc") // ERROR "inlining call to errors.New" var somethingWrong error // local closures can be inlined func l(x, y int) (int, int, error) { // ERROR "can inline l" e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result" return 0, 0, err } if x == y { e(somethingWrong) // ERROR "inlining call to l.func1" } else { f := e f(nil) // ERROR "inlining call to l.func1" } return y, x, nil } // any re-assignment prevents closure inlining func m() int { foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape" x := foo() foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape" return x + foo() } // address taking prevents closure inlining func n() int { foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape" bar := &foo x := (*bar)() + foo() return x } // make sure assignment inside closure is detected func o() int { foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape" func(x int) { // ERROR "can inline o.func2" if x > 10 { foo = func() int { return 2 } // ERROR "can inline o.func2" } }(11) // ERROR "func literal does not escape" "inlining call to o.func2" return foo() } func p() int { // ERROR "can inline p" return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1" } func q(x int) int { // ERROR "can inline q" foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape" return foo() // ERROR "inlining call to q.func1" } func r(z int) int { foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape" return x + z } bar := func(x int) int { // ERROR "func literal does not escape" "can inline r.func2" return x + func(y int) int { // ERROR "can inline r.func2.1" "can inline r.r.func2.func3" return 2*y + x*z }(x) // ERROR "inlining call to r.func2.1" } return foo(42) + bar(42) // ERROR "inlining call to r.func1" "inlining call to r.func2" "inlining call to r.r.func2.func3" } func s0(x int) int { // ERROR "can inline s0" foo := func() { // ERROR "can inline s0.func1" "func literal does not escape" x = x + 1 } foo() // ERROR "inlining call to s0.func1" return x } func s1(x int) int { // ERROR "can inline s1" foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape" return x } x = x + 1 return foo() // ERROR "inlining call to s1.func1" } func switchBreak(x, y int) int { // ERROR "can inline switchBreak" var n int switch x { case 0: n = 1 Done: switch y { case 0: n += 10 break Done } n = 2 } return n } func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape" switch x.(type) { case int: return x.(int) default: return 0 } } // Test that switches on constant things, with constant cases, only cost anything for // the case that matches. See issue 50253. func switchConst1(p func(string)) { // ERROR "can inline switchConst" "p does not escape" const c = 1 switch c { case 0: p("zero") case 1: p("one") case 2: p("two") default: p("other") } } func switchConst2() string { // ERROR "can inline switchConst2" switch runtime.GOOS { case "linux": return "Leenooks" case "windows": return "Windoze" case "darwin": return "MackBone" case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100": return "Numbers" default: return "oh nose!" } } func switchConst3() string { // ERROR "can inline switchConst3" switch runtime.GOOS { case "Linux": panic("Linux") case "Windows": panic("Windows") case "Darwin": panic("Darwin") case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100": panic("Numbers") default: return "oh nose!" } } func switchConst4() { // ERROR "can inline switchConst4" const intSize = 32 << (^uint(0) >> 63) want := func() string { // ERROR "can inline switchConst4.func1" switch intSize { case 32: return "32" case 64: return "64" default: panic("unreachable") } }() // ERROR "inlining call to switchConst4.func1" _ = want } func inlineRangeIntoMe(data []int) { // ERROR "can inline inlineRangeIntoMe" "data does not escape" rangeFunc(data, 12) // ERROR "inlining call to rangeFunc" } func rangeFunc(xs []int, b int) int { // ERROR "can inline rangeFunc" "xs does not escape" for i, x := range xs { if x == b { return i } } return -1 } type T struct{} func (T) meth(int, int) {} // ERROR "can inline T.meth" func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k" func f3() { // ERROR "can inline f3" T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth" // ERRORAUTO "inlining call to T.meth" } func small1() { // ERROR "can inline small1" runtime.GC() } func small2() int { // ERROR "can inline small2" return runtime.GOMAXPROCS(0) } func small3(t T) { // ERROR "can inline small3" t.meth2(3, 5) } func small4(t T) { // not inlineable - has 2 calls. t.meth2(runtime.GOMAXPROCS(0), 5) } func (T) meth2(int, int) { // not inlineable - has 2 calls. runtime.GC() runtime.GC() } // Issue #29737 - make sure we can do inlining for a chain of recursive functions func ee() { // ERROR "can inline ee" ff(100) // ERROR "inlining call to ff" "inlining call to gg" "inlining call to hh" } func ff(x int) { // ERROR "can inline ff" if x < 0 { return } gg(x - 1) // ERROR "inlining call to gg" "inlining call to hh" } func gg(x int) { // ERROR "can inline gg" hh(x - 1) // ERROR "inlining call to hh" "inlining call to ff" } func hh(x int) { // ERROR "can inline hh" ff(x - 1) // ERROR "inlining call to ff" "inlining call to gg" } // Issue #14768 - make sure we can inline for loops. func for1(fn func() bool) { // ERROR "can inline for1" "fn does not escape" for { if fn() { break } else { continue } } } func for2(fn func() bool) { // ERROR "can inline for2" "fn does not escape" Loop: for { if fn() { break Loop } else { continue Loop } } } // Issue #18493 - make sure we can do inlining of functions with a method value type T1 struct{} func (a T1) meth(val int) int { // ERROR "can inline T1.meth" return val + 5 } func getMeth(t1 T1) func(int) int { // ERROR "can inline getMeth" return t1.meth // ERROR "t1.meth escapes to heap" // ERRORAUTO "inlining call to T1.meth" } func ii() { // ERROR "can inline ii" var t1 T1 f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape" _ = f(3) } // Issue #42194 - make sure that functions evaluated in // go and defer statements can be inlined. func gd1(int) { defer gd1(gd2()) // ERROR "inlining call to gd2" defer gd3()() // ERROR "inlining call to gd3" go gd1(gd2()) // ERROR "inlining call to gd2" go gd3()() // ERROR "inlining call to gd3" } func gd2() int { // ERROR "can inline gd2" return 1 } func gd3() func() { // ERROR "can inline gd3" return ii } // Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost. func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape" _ = d[:6] d[0] = float32bits(x[0]) // ERROR "inlining call to float32bits" d[1] = float32bits(x[1]) // ERROR "inlining call to float32bits" d[2] = float32bits(x[2]) // ERROR "inlining call to float32bits" d[3] = float32bits(x[3]) // ERROR "inlining call to float32bits" d[4] = float32bits(x[4]) // ERROR "inlining call to float32bits" d[5] = float32bits(x[5]) // ERROR "inlining call to float32bits" } // float32bits is a copy of math.Float32bits to ensure that // these tests pass with `-gcflags=-l`. func float32bits(f float32) uint32 { // ERROR "can inline float32bits" return *(*uint32)(unsafe.Pointer(&f)) } // Ensure OCONVNOP is zero cost. func Conv(v uint64) uint64 { // ERROR "can inline Conv" return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)" } func conv2(v uint64) uint64 { // ERROR "can inline conv2" return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1" } func conv1(v uint64) uint64 { // ERROR "can inline conv1" return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v))))))))))) } func select1(x, y chan bool) int { // ERROR "can inline select1" "x does not escape" "y does not escape" select { case <-x: return 1 case <-y: return 2 } } func select2(x, y chan bool) { // ERROR "can inline select2" "x does not escape" "y does not escape" loop: // test that labeled select can be inlined. select { case <-x: break loop case <-y: } } func inlineSelect2(x, y chan bool) { // ERROR "can inline inlineSelect2" ERROR "x does not escape" "y does not escape" loop: for i := 0; i < 5; i++ { if i == 3 { break loop } select2(x, y) // ERROR "inlining call to select2" } } // Issue #62211: inlining a function with unreachable "return" // statements could trip up phi insertion. func issue62211(x bool) { // ERROR "can inline issue62211" if issue62211F(x) { // ERROR "inlining call to issue62211F" } if issue62211G(x) { // ERROR "inlining call to issue62211G" } // Initial fix CL caused a "non-monotonic scope positions" failure // on code like this. if z := 0; false { panic(z) } } func issue62211F(x bool) bool { // ERROR "can inline issue62211F" if x || true { return true } return true } func issue62211G(x bool) bool { // ERROR "can inline issue62211G" if x || true { return true } else { return true } } PK ! ϳV V typeswitch2b.gonu �[��� // errorcheck // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that various erroneous type switches are caught by the compiler. // Does not compile. package main func notused(x interface{}) { // The first t is in a different scope than the 2nd t; it cannot // be accessed (=> declared and not used error); but it is legal // to declare it. switch t := 0; t := x.(type) { // ERROR "declared and not used" case int: _ = t // this is using the t of "t := x.(type)" } } PK ! 0ĴK crlf.gonu �[��� // runoutput // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test source files and strings containing \r and \r\n. package main import ( "fmt" "strings" ) func main() { prog = strings.Replace(prog, "BQ", "`", -1) prog = strings.Replace(prog, "CR", "\r", -1) fmt.Print(prog) } var prog = ` package main CR import "fmt" var CR s = "hello\n" + CR " world"CR var t = BQhelloCR worldBQ var u = BQhCReCRlCRlCRoCR worldBQ var golden = "hello\n world" func main() { if s != golden { fmt.Printf("s=%q, want %q", s, golden) } if t != golden { fmt.Printf("t=%q, want %q", t, golden) } if u != golden { fmt.Printf("u=%q, want %q", u, golden) } } ` PK ! ��+o o uintptrescapes.dir/a.gonu �[��� // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package a import ( "unsafe" ) func recurse(i int, s []byte) byte { s[0] = byte(i) if i == 0 { return s[i] } else { var a [1024]byte r := recurse(i-1, a[:]) return r + a[0] } } //go:uintptrescapes func F1(a uintptr) { var s [16]byte recurse(4096, s[:]) *(*int)(unsafe.Pointer(a)) = 42 } //go:uintptrescapes func F2(a ...uintptr) { var s [16]byte recurse(4096, s[:]) *(*int)(unsafe.Pointer(a[0])) = 42 } type t struct{} func GetT() *t { return &t{} } //go:uintptrescapes func (*t) M1(a uintptr) { var s [16]byte recurse(4096, s[:]) *(*int)(unsafe.Pointer(a)) = 42 } //go:uintptrescapes func (*t) M2(a ...uintptr) { var s [16]byte recurse(4096, s[:]) *(*int)(unsafe.Pointer(a[0])) = 42 } PK ! j�'" " uintptrescapes.dir/main.gonu �[��� // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import ( "fmt" "os" "sync" "unsafe" "./a" ) func F1() int { var buf [1024]int a.F1(uintptr(unsafe.Pointer(&buf[0]))) return buf[0] } func F2() int { var buf [1024]int a.F2(uintptr(unsafe.Pointer(&buf[0]))) return buf[0] } var t = a.GetT() func M1() int { var buf [1024]int t.M1(uintptr(unsafe.Pointer(&buf[0]))) return buf[0] } func M2() int { var buf [1024]int t.M2(uintptr(unsafe.Pointer(&buf[0]))) return buf[0] } func main() { // Use different goroutines to force stack growth. var wg sync.WaitGroup wg.Add(4) c := make(chan bool, 4) go func() { defer wg.Done() b := F1() if b != 42 { fmt.Printf("F1: got %d, expected 42\n", b) c <- false } }() go func() { defer wg.Done() b := F2() if b != 42 { fmt.Printf("F2: got %d, expected 42\n", b) c <- false } }() go func() { defer wg.Done() b := M1() if b != 42 { fmt.Printf("M1: got %d, expected 42\n", b) c <- false } }() go func() { defer wg.Done() b := M2() if b != 42 { fmt.Printf("M2: got %d, expected 42\n", b) c <- false } }() wg.Wait() select { case <-c: os.Exit(1) default: } } PK ! �w9�� � asmhdr.gonu �[��� // buildrundir // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test the -asmhdr output of the compiler. package ignored PK ! d��J J deferprint.gonu �[��� // run // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that we can defer the predeclared functions print and println. package main func main() { defer println(42, true, false, true, 1.5, "world", (chan int)(nil), []int(nil), (map[string]int)(nil), (func())(nil), byte(255)) defer println(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) // Disabled so the test doesn't crash but left here for reference. // defer panic("dead") defer print("printing: ") } PK ! `l(f f closure3.gonu �[��� // errorcheckandrundir -0 -m -d=inlfuncswithclosures=1 //go:build !goexperiment.newinliner // Copyright 2017 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Check correctness of various closure corner cases // that are expected to be inlined package ignored PK ! o�e�< < empty.gonu �[��� // compile // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that top-level parenthesized declarations can be empty. // Compiles but does not run. package P import ( ) const ( ) var ( ) type ( ) PK ! �u˙� � escape_slice.gonu �[��� // errorcheck -0 -m -l // Copyright 2015 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test escape analysis for slices. package escape import ( "os" "strings" ) var sink interface{} func slice0() { var s []*int // BAD: i should not escape i := 0 // ERROR "moved to heap: i" s = append(s, &i) _ = s } func slice1() *int { var s []*int i := 0 // ERROR "moved to heap: i" s = append(s, &i) return s[0] } func slice2() []*int { var s []*int i := 0 // ERROR "moved to heap: i" s = append(s, &i) return s } func slice3() *int { var s []*int i := 0 // ERROR "moved to heap: i" s = append(s, &i) for _, p := range s { return p } return nil } func slice4(s []*int) { // ERROR "s does not escape" i := 0 // ERROR "moved to heap: i" s[0] = &i } func slice5(s []*int) { // ERROR "s does not escape" if s != nil { s = make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape" } i := 0 // ERROR "moved to heap: i" s[0] = &i } func slice6() { s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape" // BAD: i should not escape i := 0 // ERROR "moved to heap: i" s[0] = &i _ = s } func slice7() *int { s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape" i := 0 // ERROR "moved to heap: i" s[0] = &i return s[0] } func slice8() { i := 0 s := []*int{&i} // ERROR "\[\]\*int{...} does not escape" _ = s } func slice9() *int { i := 0 // ERROR "moved to heap: i" s := []*int{&i} // ERROR "\[\]\*int{...} does not escape" return s[0] } func slice10() []*int { i := 0 // ERROR "moved to heap: i" s := []*int{&i} // ERROR "\[\]\*int{...} escapes to heap" return s } func slice11() { i := 2 s := make([]int, 2, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape" s = make([]int, i, 3) // ERROR "make\(\[\]int, i, 3\) does not escape" s = make([]int, i, 1) // ERROR "make\(\[\]int, i, 1\) does not escape" _ = s } func slice12(x []int) *[1]int { // ERROR "leaking param: x to result ~r0 level=0$" return (*[1]int)(x) } func slice13(x []*int) [1]*int { // ERROR "leaking param: x to result ~r0 level=1$" return [1]*int(x) } func envForDir(dir string) []string { // ERROR "dir does not escape" env := os.Environ() return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string{...} does not escape" } func mergeEnvLists(in, out []string) []string { // ERROR "leaking param content: in" "leaking param content: out" "leaking param: out to result ~r0 level=0" NextVar: for _, inkv := range in { k := strings.SplitAfterN(inkv, "=", 2)[0] for i, outkv := range out { if strings.HasPrefix(outkv, k) { out[i] = inkv continue NextVar } } out = append(out, inkv) } return out } const ( IPv4len = 4 IPv6len = 16 ) var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff} func IPv4(a, b, c, d byte) IP { p := make(IP, IPv6len) // ERROR "make\(IP, 16\) escapes to heap" copy(p, v4InV6Prefix) p[12] = a p[13] = b p[14] = c p[15] = d return p } type IP []byte type IPAddr struct { IP IP Zone string // IPv6 scoped addressing zone } type resolveIPAddrTest struct { network string litAddrOrName string addr *IPAddr err error } var resolveIPAddrTests = []resolveIPAddrTest{ {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil}, } func setupTestData() { resolveIPAddrTests = append(resolveIPAddrTests, []resolveIPAddrTest{ // ERROR "\[\]resolveIPAddrTest{...} does not escape" {"ip", "localhost", &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap" nil}, {"ip4", "localhost", &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap" nil}, }...) } PK ! �B�I�# �# escape_struct_param2.gonu �[��� // errorcheck -0 -m -l // Copyright 2015 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test escape analysis for struct function parameters. // Note companion strict_param1 checks *struct function parameters with similar tests. package notmain var Ssink *string type U struct { _sp *string _spp **string } type V struct { _u U _up *U _upp **U } func (u U) SP() *string { // ERROR "leaking param: u to result ~r0 level=0$" return u._sp } func (u U) SPP() **string { // ERROR "leaking param: u to result ~r0 level=0$" return u._spp } func (u U) SPPi() *string { // ERROR "leaking param: u to result ~r0 level=1$" return *u._spp } func tSPPi() { s := "cat" // ERROR "moved to heap: s$" ps := &s pps := &ps pu := &U{ps, pps} // ERROR "&U{...} does not escape$" Ssink = pu.SPPi() } func tiSPP() { s := "cat" // ERROR "moved to heap: s$" ps := &s pps := &ps pu := &U{ps, pps} // ERROR "&U{...} does not escape$" Ssink = *pu.SPP() } // BAD: need fine-grained analysis to avoid spurious escape of ps func tSP() { s := "cat" // ERROR "moved to heap: s$" ps := &s // ERROR "moved to heap: ps$" pps := &ps pu := &U{ps, pps} // ERROR "&U{...} does not escape$" Ssink = pu.SP() } func (v V) u() U { // ERROR "leaking param: v to result ~r0 level=0$" return v._u } func (v V) UP() *U { // ERROR "leaking param: v to result ~r0 level=0$" return v._up } func (v V) UPP() **U { // ERROR "leaking param: v to result ~r0 level=0$" return v._upp } func (v V) UPPia() *U { // ERROR "leaking param: v to result ~r0 level=1$" return *v._upp } func (v V) UPPib() *U { // ERROR "leaking param: v to result ~r0 level=1$" return *v.UPP() } func (v V) USPa() *string { // ERROR "leaking param: v to result ~r0 level=0$" return v._u._sp } func (v V) USPb() *string { // ERROR "leaking param: v to result ~r0 level=0$" return v.u()._sp } func (v V) USPPia() *string { // ERROR "leaking param: v to result ~r0 level=1$" return *v._u._spp } func (v V) USPPib() *string { // ERROR "leaking param: v to result ~r0 level=1$" return v._u.SPPi() } func (v V) UPiSPa() *string { // ERROR "leaking param: v to result ~r0 level=1$" return v._up._sp } func (v V) UPiSPb() *string { // ERROR "leaking param: v to result ~r0 level=1$" return v._up.SP() } func (v V) UPiSPc() *string { // ERROR "leaking param: v to result ~r0 level=1$" return v.UP()._sp } func (v V) UPiSPd() *string { // ERROR "leaking param: v to result ~r0 level=1$" return v.UP().SP() } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3 func tUPiSPa() { s1 := "ant" s2 := "bat" // ERROR "moved to heap: s2$" s3 := "cat" // ERROR "moved to heap: s3$" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 // ERROR "moved to heap: ps4$" ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPa() // Ssink = &s3 (only &s3 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3 func tUPiSPb() { s1 := "ant" s2 := "bat" // ERROR "moved to heap: s2$" s3 := "cat" // ERROR "moved to heap: s3$" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 // ERROR "moved to heap: ps4$" ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPb() // Ssink = &s3 (only &s3 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3 func tUPiSPc() { s1 := "ant" s2 := "bat" // ERROR "moved to heap: s2$" s3 := "cat" // ERROR "moved to heap: s3$" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 // ERROR "moved to heap: ps4$" ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPc() // Ssink = &s3 (only &s3 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s3 func tUPiSPd() { s1 := "ant" s2 := "bat" // ERROR "moved to heap: s2$" s3 := "cat" // ERROR "moved to heap: s3$" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 // ERROR "moved to heap: ps4$" ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} escapes to heap$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPd() // Ssink = &s3 (only &s3 really escapes) } func (v V) UPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=2$" return *v._up._spp } func (v V) UPiSPPib() *string { // ERROR "leaking param: v to result ~r0 level=2$" return v._up.SPPi() } func (v V) UPiSPPic() *string { // ERROR "leaking param: v to result ~r0 level=2$" return *v.UP()._spp } func (v V) UPiSPPid() *string { // ERROR "leaking param: v to result ~r0 level=2$" return v.UP().SPPi() } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4 func tUPiSPPia() { s1 := "ant" s2 := "bat" s3 := "cat" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPPia() // Ssink = *&ps4 = &s4 (only &s4 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4 func tUPiSPPib() { s1 := "ant" s2 := "bat" s3 := "cat" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPPib() // Ssink = *&ps4 = &s4 (only &s4 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4 func tUPiSPPic() { s1 := "ant" s2 := "bat" s3 := "cat" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPPic() // Ssink = *&ps4 = &s4 (only &s4 really escapes) } // BAD: need fine-grained (field-sensitive) analysis to avoid spurious escape of all but &s4 func tUPiSPPid() { s1 := "ant" s2 := "bat" s3 := "cat" s4 := "dog" // ERROR "moved to heap: s4$" s5 := "emu" // ERROR "moved to heap: s5$" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 ps6 := &s6 // ERROR "moved to heap: ps6$" u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPiSPPid() // Ssink = *&ps4 = &s4 (only &s4 really escapes) } func (v V) UPPiSPPia() *string { // ERROR "leaking param: v to result ~r0 level=3$" return *(*v._upp)._spp } // This test isolates the one value that needs to escape, not because // it distinguishes fields but because it knows that &s6 is the only // value reachable by two indirects from v. // The test depends on the level cap in the escape analysis tags // being able to encode that fact. func tUPPiSPPia() { // This test is sensitive to the level cap in function summary results. s1 := "ant" s2 := "bat" s3 := "cat" s4 := "dog" s5 := "emu" s6 := "fox" // ERROR "moved to heap: s6$" ps2 := &s2 ps4 := &s4 ps6 := &s6 u1 := U{&s1, &ps2} u2 := &U{&s3, &ps4} // ERROR "&U{...} does not escape$" u3 := &U{&s5, &ps6} // ERROR "&U{...} does not escape$" v := &V{u1, u2, &u3} // ERROR "&V{...} does not escape$" Ssink = v.UPPiSPPia() // Ssink = *&ps6 = &s6 (only &s6 really escapes) } PK ! ��Բ � index.gonu �[��� // skip // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Generate test of index and slice bounds checks. // The actual tests are index0.go, index1.go, index2.go. package main import ( "bufio" "fmt" "os" "unsafe" ) const prolog = ` package main import ( "runtime" ) type quad struct { x, y, z, w int } const ( cj = 100011 ci int = 100012 ci8 int8 = 115 ci16 int16 = 10016 ci32 int32 = 100013 ci64 int64 = 100014 ci64big int64 = 1<<31 ci64bigger int64 = 1<<32 chuge = 1<<100 cfgood = 2.0 cfbad = 2.1 cnj = -2 cni int = -3 cni8 int8 = -6 cni16 int16 = -7 cni32 int32 = -4 cni64 int64 = -5 cni64big int64 = -1<<31 cni64bigger int64 = -1<<32 cnhuge = -1<<100 cnfgood = -2.0 cnfbad = -2.1 ) var j int = 100020 var i int = 100021 var i8 int8 = 126 var i16 int16 = 10025 var i32 int32 = 100022 var i64 int64 = 100023 var i64big int64 = 1<<31 var i64bigger int64 = 1<<32 var huge uint64 = 1<<64 - 1 var fgood float64 = 2.0 var fbad float64 = 2.1 var nj int = -10 var ni int = -11 var ni8 int8 = -14 var ni16 int16 = -15 var ni32 int32 = -12 var ni64 int64 = -13 var ni64big int64 = -1<<31 var ni64bigger int64 = -1<<32 var nhuge int64 = -1<<63 var nfgood float64 = -2.0 var nfbad float64 = -2.1 var si []int = make([]int, 10) var ai [10]int var pai *[10]int = &ai var sq []quad = make([]quad, 10) var aq [10]quad var paq *[10]quad = &aq var sib []int = make([]int, 100000) var aib [100000]int var paib *[100000]int = &aib var sqb []quad = make([]quad, 100000) var aqb [100000]quad var paqb *[100000]quad = &aqb type T struct { si []int ai [10]int pai *[10]int sq []quad aq [10]quad paq *[10]quad sib []int aib [100000]int paib *[100000]int sqb []quad aqb [100000]quad paqb *[100000]quad } var t = T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb} var pt = &T{si, ai, pai, sq, aq, paq, sib, aib, paib, sqb, aqb, paqb} // test that f panics func test(f func(), s string) { defer func() { if err := recover(); err == nil { _, file, line, _ := runtime.Caller(2) bug() print(file, ":", line, ": ", s, " did not panic\n") } else if !contains(err.(error).Error(), "out of range") { _, file, line, _ := runtime.Caller(2) bug() print(file, ":", line, ": ", s, " unexpected panic: ", err.(error).Error(), "\n") } }() f() } func contains(x, y string) bool { for i := 0; i+len(y) <= len(x); i++ { if x[i:i+len(y)] == y { return true } } return false } var X interface{} func use(y interface{}) { X = y } var didBug = false func bug() { if !didBug { didBug = true println("BUG") } } func main() { ` // pass variable set in index[012].go // 0 - dynamic checks // 1 - static checks of invalid constants (cannot assign to types) // 2 - static checks of array bounds func testExpr(b *bufio.Writer, expr string) { if pass == 0 { fmt.Fprintf(b, "\ttest(func(){use(%s)}, %q)\n", expr, expr) } else { fmt.Fprintf(b, "\tuse(%s) // ERROR \"index|overflow|truncated|must be integer\"\n", expr) } } func main() { b := bufio.NewWriter(os.Stdout) if pass == 0 { fmt.Fprint(b, "// run\n\n") } else { fmt.Fprint(b, "// errorcheck\n\n") } fmt.Fprint(b, prolog) var choices = [][]string{ // Direct value, fetch from struct, fetch from struct pointer. // The last two cases get us to oindex_const_sudo in gsubr.c. []string{"", "t.", "pt."}, // Array, pointer to array, slice. []string{"a", "pa", "s"}, // Element is int, element is quad (struct). // This controls whether we end up in gsubr.c (i) or cgen.c (q). []string{"i", "q"}, // Small or big len. []string{"", "b"}, // Variable or constant. []string{"", "c"}, // Positive or negative. []string{"", "n"}, // Size of index. []string{"j", "i", "i8", "i16", "i32", "i64", "i64big", "i64bigger", "huge", "fgood", "fbad"}, } forall(choices, func(x []string) { p, a, e, big, c, n, i := x[0], x[1], x[2], x[3], x[4], x[5], x[6] // Pass: dynamic=0, static=1, 2. // Which cases should be caught statically? // Only constants, obviously. // Beyond that, must be one of these: // indexing into array or pointer to array // negative constant // large constant thisPass := 0 if c == "c" && (a == "a" || a == "pa" || n == "n" || i == "i64big" || i == "i64bigger" || i == "huge" || i == "fbad") { if i == "huge" { // Due to a detail of gc's internals, // the huge constant errors happen in an // earlier pass than the others and inhibits // the next pass from running. // So run it as a separate check. thisPass = 1 } else if a == "s" && n == "" && (i == "i64big" || i == "i64bigger") && unsafe.Sizeof(int(0)) > 4 { // If int is 64 bits, these huge // numbers do fit in an int, so they // are not rejected at compile time. thisPass = 0 } else { thisPass = 2 } } pae := p + a + e + big cni := c + n + i // If we're using the big-len data, positive int8 and int16 cannot overflow. if big == "b" && n == "" && (i == "i8" || i == "i16") { if pass == 0 { fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni) } return } // Float variables cannot be used as indices. if c == "" && (i == "fgood" || i == "fbad") { return } // Integral float constant is ok. if c == "c" && n == "" && i == "fgood" { if pass == 0 { fmt.Fprintf(b, "\tuse(%s[%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[0:%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[1:%s])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[%s:])\n", pae, cni) fmt.Fprintf(b, "\tuse(%s[%s:%s])\n", pae, cni, cni) } return } // Only print the test case if it is appropriate for this pass. if thisPass == pass { // Index operation testExpr(b, pae+"["+cni+"]") // Slice operation. // Low index 0 is a special case in ggen.c // so test both 0 and 1. testExpr(b, pae+"[0:"+cni+"]") testExpr(b, pae+"[1:"+cni+"]") testExpr(b, pae+"["+cni+":]") testExpr(b, pae+"["+cni+":"+cni+"]") } }) fmt.Fprintln(b, "}") b.Flush() } func forall(choices [][]string, f func([]string)) { x := make([]string, len(choices)) var recurse func(d int) recurse = func(d int) { if d >= len(choices) { f(x) return } for _, x[d] = range choices[d] { recurse(d + 1) } } recurse(0) } PK ! ��ծ switch4.gonu �[��� // errorcheck // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that erroneous switch statements are detected by the compiler. // Does not compile. package main type I interface { M() } func bad() { i5 := 5 switch i5 { case 5: fallthrough // ERROR "cannot fallthrough final case in switch" } } func good() { var i interface{} var s string switch i { case s: } switch s { case i: } } PK ! u��h h gcstring.gonu �[��� // run // Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that s[len(s):] - which can point past the end of the allocated block - // does not confuse the garbage collector. package main import ( "runtime" "time" ) type T struct { ptr **int pad [120]byte } var things []interface{} func main() { setup() runtime.GC() runtime.GC() time.Sleep(10*time.Millisecond) runtime.GC() runtime.GC() time.Sleep(10*time.Millisecond) } func setup() { var Ts []interface{} buf := make([]byte, 128) for i := 0; i < 10000; i++ { s := string(buf) t := &T{ptr: new(*int)} runtime.SetFinalizer(t.ptr, func(**int) { panic("*int freed too early") }) Ts = append(Ts, t) things = append(things, s[len(s):]) } things = append(things, Ts...) } PK ! �p� � convinline.gonu �[��� // runoutput //go:build !wasm // Copyright 2022 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import ( "bytes" "fmt" "math" "math/bits" "os" "strconv" "strings" ) var types = []string{ "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "float32", "float64", } func main() { var prog bytes.Buffer fmt.Fprintf(&prog, "package main\n\n") fmt.Fprintf(&prog, "import ( \"fmt\"; \"math\" )\n") for _, t1 := range types { for _, t2 := range types { fmt.Fprintf(&prog, "func %[1]s_to_%[2]s(x %[1]s) %[2]s { return %[2]s(x) }\n", t1, t2) } } var outputs []string var exprs []string fmt.Fprintf(&prog, "var (\n") for _, t1 := range types { var inputs []string switch t1 { case "int64", "int": if t1 == "int64" || bits.UintSize == 64 { inputs = append(inputs, "-0x8000_0000_0000_0000", "-0x7fff_ffff_ffff_ffff", "-0x12_3456_7890", "0x12_3456_7890", "0x7fff_ffff_ffff_ffff") } fallthrough case "int32": inputs = append(inputs, "-0x8000_0000", "-0x7fff_ffff", "-0x12_3456", "0x12_3456", "0x7fff_ffff") fallthrough case "int16": inputs = append(inputs, "-0x8000", "-0x7fff", "-0x1234", "0x1234", "0x7fff") fallthrough case "int8": inputs = append(inputs, "-0x80", "-0x7f", "-0x12", "-1", "0", "1", "0x12", "0x7f") case "uint64", "uint", "uintptr": if t1 == "uint64" || bits.UintSize == 64 { inputs = append(inputs, "0x12_3456_7890", "0x7fff_ffff_ffff_ffff", "0x8000_0000_0000_0000", "0xffff_ffff_ffff_ffff") } fallthrough case "uint32": inputs = append(inputs, "0x12_3456", "0x7fff_ffff", "0x8000_0000", "0xffff_ffff") fallthrough case "uint16": inputs = append(inputs, "0x1234", "0x7fff", "0x8000", "0xffff") fallthrough case "uint8": inputs = append(inputs, "0", "1", "0x12", "0x7f", "0x80", "0xff") case "float64": inputs = append(inputs, "-1.79769313486231570814527423731704356798070e+308", "-1e300", "-1e100", "-1e40", "-3.5e38", "3.5e38", "1e40", "1e100", "1e300", "1.79769313486231570814527423731704356798070e+308") fallthrough case "float32": inputs = append(inputs, "-3.40282346638528859811704183484516925440e+38", "-1e38", "-1.5", "-1.401298464324817070923729583289916131280e-45", "0", "1.401298464324817070923729583289916131280e-45", "1.5", "1e38", "3.40282346638528859811704183484516925440e+38") } for _, t2 := range types { for _, x := range inputs { code := fmt.Sprintf("%s_to_%s(%s)", t1, t2, x) fmt.Fprintf(&prog, "\tv%d = %s\n", len(outputs), code) exprs = append(exprs, code) outputs = append(outputs, convert(x, t1, t2)) } } } fmt.Fprintf(&prog, ")\n\n") fmt.Fprintf(&prog, "func main() {\n\tok := true\n") for i, out := range outputs { fmt.Fprintf(&prog, "\tif v%d != %s { fmt.Println(%q, \"=\", v%d, \"want\", %s); ok = false }\n", i, out, exprs[i], i, out) } fmt.Fprintf(&prog, "\tif !ok { println(\"FAIL\") }\n") fmt.Fprintf(&prog, "}\n") os.Stdout.Write(prog.Bytes()) } func convert(x, t1, t2 string) string { if strings.HasPrefix(t1, "int") { v, err := strconv.ParseInt(x, 0, 64) if err != nil { println(x, t1, t2) panic(err) } return convert1(v, t2) } if strings.HasPrefix(t1, "uint") { v, err := strconv.ParseUint(x, 0, 64) if err != nil { println(x, t1, t2) panic(err) } return convert1(v, t2) } if strings.HasPrefix(t1, "float") { v, err := strconv.ParseFloat(x, 64) if err != nil { println(x, t1, t2) panic(err) } if t1 == "float32" { v = float64(float32(v)) } return convert1(v, t2) } panic(t1) } func convert1[T int64 | uint64 | float64](v T, t2 string) string { switch t2 { case "int": return fmt.Sprintf("%s(%#x)", t2, int(v)) case "int8": return fmt.Sprintf("%s(%#x)", t2, int8(v)) case "int16": return fmt.Sprintf("%s(%#x)", t2, int16(v)) case "int32": return fmt.Sprintf("%s(%#x)", t2, int32(v)) case "int64": return fmt.Sprintf("%s(%#x)", t2, int64(v)) case "uint": return fmt.Sprintf("%s(%#x)", t2, uint(v)) case "uint8": return fmt.Sprintf("%s(%#x)", t2, uint8(v)) case "uint16": return fmt.Sprintf("%s(%#x)", t2, uint16(v)) case "uint32": return fmt.Sprintf("%s(%#x)", t2, uint32(v)) case "uint64": return fmt.Sprintf("%s(%#x)", t2, uint64(v)) case "uintptr": return fmt.Sprintf("%s(%#x)", t2, uintptr(v)) case "float32": v := float32(v) if math.IsInf(float64(v), -1) { return "float32(math.Inf(-1))" } if math.IsInf(float64(v), +1) { return "float32(math.Inf(+1))" } return fmt.Sprintf("%s(%v)", t2, float64(v)) case "float64": return fmt.Sprintf("%s(%v)", t2, float64(v)) } panic(t2) } PK ! �Cg͟, �, newinline.gonu �[��� // errorcheckwithauto -0 -m -d=inlfuncswithclosures=1 //go:build goexperiment.newinliner // Copyright 2023 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test, using compiler diagnostic flags, that inlining is working. // Compiles but does not run. package foo import ( "errors" "runtime" "unsafe" ) func add2(p *byte, n uintptr) *byte { // ERROR "can inline add2" "leaking param: p to result" return (*byte)(add1(unsafe.Pointer(p), n)) // ERROR "inlining call to add1" } func add1(p unsafe.Pointer, x uintptr) unsafe.Pointer { // ERROR "can inline add1" "leaking param: p to result" return unsafe.Pointer(uintptr(p) + x) } func f(x *byte) *byte { // ERROR "can inline f" "leaking param: x to result" return add2(x, 1) // ERROR "inlining call to add2" "inlining call to add1" } //go:noinline func g(x int) int { return x + 1 } func h(x int) int { // ERROR "can inline h" return x + 2 } func i(x int) int { // ERROR "can inline i" const y = 2 return x + y } func j(x int) int { // ERROR "can inline j" switch { case x > 0: return x + 2 default: return x + 1 } } func f2() int { // ERROR "can inline f2" tmp1 := h tmp2 := tmp1 return tmp2(0) // ERROR "inlining call to h" } var abc = errors.New("abc") // ERROR "inlining call to errors.New" var somethingWrong error // local closures can be inlined func l(x, y int) (int, int, error) { // ERROR "can inline l" e := func(err error) (int, int, error) { // ERROR "can inline l.func1" "func literal does not escape" "leaking param: err to result" return 0, 0, err } if x == y { e(somethingWrong) // ERROR "inlining call to l.func1" } else { f := e f(nil) // ERROR "inlining call to l.func1" } return y, x, nil } // any re-assignment prevents closure inlining func m() int { foo := func() int { return 1 } // ERROR "can inline m.func1" "func literal does not escape" x := foo() foo = func() int { return 2 } // ERROR "can inline m.func2" "func literal does not escape" return x + foo() } // address taking prevents closure inlining func n() int { // ERROR "can inline n" foo := func() int { return 1 } // ERROR "can inline n.func1" "func literal does not escape" bar := &foo x := (*bar)() + foo() return x } // make sure assignment inside closure is detected func o() int { // ERROR "can inline o" foo := func() int { return 1 } // ERROR "can inline o.func1" "func literal does not escape" func(x int) { // ERROR "can inline o.func2" if x > 10 { foo = func() int { return 2 } // ERROR "can inline o.func2" } }(11) // ERROR "func literal does not escape" "inlining call to o.func2" return foo() } func p() int { // ERROR "can inline p" return func() int { return 42 }() // ERROR "can inline p.func1" "inlining call to p.func1" } func q(x int) int { // ERROR "can inline q" foo := func() int { return x * 2 } // ERROR "can inline q.func1" "func literal does not escape" return foo() // ERROR "inlining call to q.func1" } func r(z int) int { // ERROR "can inline r" foo := func(x int) int { // ERROR "can inline r.func1" "func literal does not escape" return x + z } bar := func(x int) int { // ERROR "func literal does not escape" "can inline r.func2" return x + func(y int) int { // ERROR "can inline r.func2.1" "can inline r.r.func2.func3" return 2*y + x*z }(x) // ERROR "inlining call to r.func2.1" } return foo(42) + bar(42) // ERROR "inlining call to r.func1" "inlining call to r.func2" "inlining call to r.r.func2.func3" } func s0(x int) int { // ERROR "can inline s0" foo := func() { // ERROR "can inline s0.func1" "func literal does not escape" x = x + 1 } foo() // ERROR "inlining call to s0.func1" return x } func s1(x int) int { // ERROR "can inline s1" foo := func() int { // ERROR "can inline s1.func1" "func literal does not escape" return x } x = x + 1 return foo() // ERROR "inlining call to s1.func1" } func switchBreak(x, y int) int { // ERROR "can inline switchBreak" var n int switch x { case 0: n = 1 Done: switch y { case 0: n += 10 break Done } n = 2 } return n } func switchType(x interface{}) int { // ERROR "can inline switchType" "x does not escape" switch x.(type) { case int: return x.(int) default: return 0 } } // Test that switches on constant things, with constant cases, only cost anything for // the case that matches. See issue 50253. func switchConst1(p func(string)) { // ERROR "can inline switchConst" "p does not escape" const c = 1 switch c { case 0: p("zero") case 1: p("one") case 2: p("two") default: p("other") } } func switchConst2() string { // ERROR "can inline switchConst2" switch runtime.GOOS { case "linux": return "Leenooks" case "windows": return "Windoze" case "darwin": return "MackBone" case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100": return "Numbers" default: return "oh nose!" } } func switchConst3() string { // ERROR "can inline switchConst3" switch runtime.GOOS { case "Linux": panic("Linux") case "Windows": panic("Windows") case "Darwin": panic("Darwin") case "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100": panic("Numbers") default: return "oh nose!" } } func switchConst4() { // ERROR "can inline switchConst4" const intSize = 32 << (^uint(0) >> 63) want := func() string { // ERROR "can inline switchConst4.func1" switch intSize { case 32: return "32" case 64: return "64" default: panic("unreachable") } }() // ERROR "inlining call to switchConst4.func1" _ = want } func inlineRangeIntoMe(data []int) { // ERROR "can inline inlineRangeIntoMe" "data does not escape" rangeFunc(data, 12) // ERROR "inlining call to rangeFunc" } func rangeFunc(xs []int, b int) int { // ERROR "can inline rangeFunc" "xs does not escape" for i, x := range xs { if x == b { return i } } return -1 } type T struct{} func (T) meth(int, int) {} // ERROR "can inline T.meth" func k() (T, int, int) { return T{}, 0, 0 } // ERROR "can inline k" func f3() { // ERROR "can inline f3" T.meth(k()) // ERROR "inlining call to k" "inlining call to T.meth" // ERRORAUTO "inlining call to T.meth" } func small1() { // ERROR "can inline small1" runtime.GC() } func small2() int { // ERROR "can inline small2" return runtime.GOMAXPROCS(0) } func small3(t T) { // ERROR "can inline small3" t.meth2(3, 5) } func small4(t T) { // ERROR "can inline small4" t.meth2(runtime.GOMAXPROCS(0), 5) } func (T) meth2(int, int) { // ERROR "can inline T.meth2" runtime.GC() runtime.GC() } // Issue #29737 - make sure we can do inlining for a chain of recursive functions func ee() { // ERROR "can inline ee" ff(100) // ERROR "inlining call to ff" "inlining call to gg" "inlining call to hh" } func ff(x int) { // ERROR "can inline ff" if x < 0 { return } gg(x - 1) // ERROR "inlining call to gg" "inlining call to hh" } func gg(x int) { // ERROR "can inline gg" hh(x - 1) // ERROR "inlining call to hh" "inlining call to ff" } func hh(x int) { // ERROR "can inline hh" ff(x - 1) // ERROR "inlining call to ff" "inlining call to gg" } // Issue #14768 - make sure we can inline for loops. func for1(fn func() bool) { // ERROR "can inline for1" "fn does not escape" for { if fn() { break } else { continue } } } func for2(fn func() bool) { // ERROR "can inline for2" "fn does not escape" Loop: for { if fn() { break Loop } else { continue Loop } } } // Issue #18493 - make sure we can do inlining of functions with a method value type T1 struct{} func (a T1) meth(val int) int { // ERROR "can inline T1.meth" return val + 5 } func getMeth(t1 T1) func(int) int { // ERROR "can inline getMeth" return t1.meth // ERROR "t1.meth escapes to heap" // ERRORAUTO "inlining call to T1.meth" } func ii() { // ERROR "can inline ii" var t1 T1 f := getMeth(t1) // ERROR "inlining call to getMeth" "t1.meth does not escape" _ = f(3) } // Issue #42194 - make sure that functions evaluated in // go and defer statements can be inlined. func gd1(int) { defer gd1(gd2()) // ERROR "inlining call to gd2" "can inline gd1.deferwrap1" defer gd3()() // ERROR "inlining call to gd3" go gd1(gd2()) // ERROR "inlining call to gd2" "can inline gd1.gowrap2" go gd3()() // ERROR "inlining call to gd3" } func gd2() int { // ERROR "can inline gd2" return 1 } func gd3() func() { // ERROR "can inline gd3" return ii } // Issue #42788 - ensure ODEREF OCONVNOP* OADDR is low cost. func EncodeQuad(d []uint32, x [6]float32) { // ERROR "can inline EncodeQuad" "d does not escape" _ = d[:6] d[0] = float32bits(x[0]) // ERROR "inlining call to float32bits" d[1] = float32bits(x[1]) // ERROR "inlining call to float32bits" d[2] = float32bits(x[2]) // ERROR "inlining call to float32bits" d[3] = float32bits(x[3]) // ERROR "inlining call to float32bits" d[4] = float32bits(x[4]) // ERROR "inlining call to float32bits" d[5] = float32bits(x[5]) // ERROR "inlining call to float32bits" } // float32bits is a copy of math.Float32bits to ensure that // these tests pass with `-gcflags=-l`. func float32bits(f float32) uint32 { // ERROR "can inline float32bits" return *(*uint32)(unsafe.Pointer(&f)) } // Ensure OCONVNOP is zero cost. func Conv(v uint64) uint64 { // ERROR "can inline Conv" return conv2(conv2(conv2(v))) // ERROR "inlining call to (conv1|conv2)" } func conv2(v uint64) uint64 { // ERROR "can inline conv2" return conv1(conv1(conv1(conv1(v)))) // ERROR "inlining call to conv1" } func conv1(v uint64) uint64 { // ERROR "can inline conv1" return uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(uint64(v))))))))))) } func select1(x, y chan bool) int { // ERROR "can inline select1" "x does not escape" "y does not escape" select { case <-x: return 1 case <-y: return 2 } } func select2(x, y chan bool) { // ERROR "can inline select2" "x does not escape" "y does not escape" loop: // test that labeled select can be inlined. select { case <-x: break loop case <-y: } } func inlineSelect2(x, y chan bool) { // ERROR "can inline inlineSelect2" ERROR "x does not escape" "y does not escape" loop: for i := 0; i < 5; i++ { if i == 3 { break loop } select2(x, y) // ERROR "inlining call to select2" } } PK ! �FN� � blank1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that incorrect uses of the blank identifier are caught. // Does not compile. package _ // ERROR "invalid package name" var t struct { _ int } func (x int) _() { // ERROR "methods on non-local type" println(x) } type T struct { _ []int } func main() { _() // ERROR "cannot use .* as value" x := _+1 // ERROR "cannot use .* as value" _ = x _ = t._ // ERROR "cannot refer to blank field|invalid use of|t._ undefined" var v1, v2 T _ = v1 == v2 // ERROR "cannot be compared|non-comparable|cannot compare v1 == v2" } PK ! �;�G G intrinsic_atomic.gonu �[��� // errorcheck -0 -d=ssa/intrinsics/debug //go:build amd64 || arm64 || loong64 || mips || mipsle || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x // Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import "sync/atomic" var x uint32 func atomics() { _ = atomic.LoadUint32(&x) // ERROR "intrinsic substitution for LoadUint32" atomic.StoreUint32(&x, 1) // ERROR "intrinsic substitution for StoreUint32" atomic.AddUint32(&x, 1) // ERROR "intrinsic substitution for AddUint32" atomic.SwapUint32(&x, 1) // ERROR "intrinsic substitution for SwapUint32" atomic.CompareAndSwapUint32(&x, 1, 2) // ERROR "intrinsic substitution for CompareAndSwapUint32" } PK ! .쬃� � reflectmethod7.gonu �[��� // run // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // See issue 44207. package main import "reflect" type S int func (s S) M() {} func main() { t := reflect.TypeOf(S(0)) fn, ok := reflect.PointerTo(t).MethodByName("M") if !ok { panic("FAIL") } fn.Func.Call([]reflect.Value{reflect.New(t)}) } PK ! �i'�> > devirt.gonu �[��� // errorcheck -0 -d=ssa/opt/debug=1 package main // Trivial interface call devirtualization test. type real struct { value int } func (r *real) Value() int { return r.value } type Valuer interface { Value() int } type indirectiface struct { a, b, c int } func (i indirectiface) Value() int { return i.a + i.b + i.c } func main() { var r Valuer rptr := &real{value: 3} r = rptr if r.Value() != 3 { // ERROR "de-virtualizing call$" panic("not 3") } r = indirectiface{3, 4, 5} if r.Value() != 12 { // ERROR "de-virtualizing call$" panic("not 12") } } PK ! M�9�� � used.gonu �[��� // errorcheck // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package p import "unsafe" const C = 1 var x, x1, x2 int var b bool var s string var c chan int var cp complex128 var slice []int var array [2]int var bytes []byte var runes []rune var r rune func f0() {} func f1() int { return 1 } func f2() (int, int) { return 1, 1 } type T struct{ X int } func (T) M1() int { return 1 } func (T) M0() {} func (T) M() {} var t T var tp *T type I interface{ M() } var i I var m map[int]int func _() { // Note: if the next line changes to x, the error silences the x+x etc below! x1 // ERROR "x1 .* not used" nil // ERROR "nil .* not used" C // ERROR "C .* not used" 1 // ERROR "1 .* not used" x + x // ERROR "x \+ x .* not used" x - x // ERROR "x - x .* not used" x | x // ERROR "x \| x .* not used" "a" + s // ERROR ".a. \+ s .* not used" &x // ERROR "&x .* not used" b && b // ERROR "b && b .* not used" append(slice, 1) // ERROR "append\(slice, 1\) .* not used" string(bytes) // ERROR "string\(bytes\) .* not used" string(runes) // ERROR "string\(runes\) .* not used" f0() // ok f1() // ok f2() // ok _ = f0() // ERROR "f0\(\) .*used as value" _ = f1() // ok _, _ = f2() // ok _ = f2() // ERROR "assignment mismatch: 1 variable but f2 returns 2 values|cannot assign" _ = f1(), 0 // ERROR "assignment mismatch: 1 variable but 2 values|cannot assign" T.M0 // ERROR "T.M0 .* not used" t.M0 // ERROR "t.M0 .* not used" cap // ERROR "use of builtin cap not in function call|must be called" cap(slice) // ERROR "cap\(slice\) .* not used" close(c) // ok _ = close(c) // ERROR "close\(c\) .*used as value" func() {} // ERROR "func literal .* not used|is not used" X{} // ERROR "undefined: X" map[string]int{} // ERROR "map\[string\]int{} .* not used" struct{}{} // ERROR "struct ?{}{} .* not used" [1]int{} // ERROR "\[1\]int{} .* not used" []int{} // ERROR "\[\]int{} .* not used" &struct{}{} // ERROR "&struct ?{}{} .* not used" float32(x) // ERROR "float32\(x\) .* not used" I(t) // ERROR "I\(t\) .* not used" int(x) // ERROR "int\(x\) .* not used" copy(slice, slice) // ok _ = copy(slice, slice) // ok delete(m, 1) // ok _ = delete(m, 1) // ERROR "delete\(m, 1\) .*used as value" t.X // ERROR "t.X .* not used" tp.X // ERROR "tp.X .* not used" t.M // ERROR "t.M .* not used" I.M // ERROR "I.M .* not used" i.(T) // ERROR "i.\(T\) .* not used" x == x // ERROR "x == x .* not used" x != x // ERROR "x != x .* not used" x != x // ERROR "x != x .* not used" x < x // ERROR "x < x .* not used" x >= x // ERROR "x >= x .* not used" x > x // ERROR "x > x .* not used" *tp // ERROR "\*tp .* not used" slice[0] // ERROR "slice\[0\] .* not used" m[1] // ERROR "m\[1\] .* not used" len(slice) // ERROR "len\(slice\) .* not used" make(chan int) // ERROR "make\(chan int\) .* not used" make(map[int]int) // ERROR "make\(map\[int\]int\) .* not used" make([]int, 1) // ERROR "make\(\[\]int, 1\) .* not used" x * x // ERROR "x \* x .* not used" x / x // ERROR "x / x .* not used" x % x // ERROR "x % x .* not used" x << x // ERROR "x << x .* not used" x >> x // ERROR "x >> x .* not used" x & x // ERROR "x & x .* not used" x &^ x // ERROR "x &\^ x .* not used" new(int) // ERROR "new\(int\) .* not used" !b // ERROR "!b .* not used" ^x // ERROR "\^x .* not used" +x // ERROR "\+x .* not used" -x // ERROR "-x .* not used" b || b // ERROR "b \|\| b .* not used" panic(1) // ok _ = panic(1) // ERROR "panic\(1\) .*used as value" print(1) // ok _ = print(1) // ERROR "print\(1\) .*used as value" println(1) // ok _ = println(1) // ERROR "println\(1\) .*used as value" c <- 1 // ok slice[1:1] // ERROR "slice\[1:1\] .* not used" array[1:1] // ERROR "array\[1:1\] .* not used" s[1:1] // ERROR "s\[1:1\] .* not used" slice[1:1:1] // ERROR "slice\[1:1:1\] .* not used" array[1:1:1] // ERROR "array\[1:1:1\] .* not used" recover() // ok <-c // ok string(r) // ERROR "string\(r\) .* not used" iota // ERROR "undefined: iota|cannot use iota" real(cp) // ERROR "real\(cp\) .* not used" imag(cp) // ERROR "imag\(cp\) .* not used" complex(1, 2) // ERROR "complex\(1, 2\) .* not used" unsafe.Alignof(t.X) // ERROR "unsafe.Alignof\(t.X\) .* not used" unsafe.Offsetof(t.X) // ERROR "unsafe.Offsetof\(t.X\) .* not used" unsafe.Sizeof(t) // ERROR "unsafe.Sizeof\(t\) .* not used" _ = int // ERROR "type int is not an expression|not an expression" (x) // ERROR "x .* not used|not used" _ = new(x2) // ERROR "x2 is not a type|not a type" // Disabled due to issue #43125. // _ = new(1 + 1) // DISABLED "1 \+ 1 is not a type" } PK ! ��~'A A goprint.outnu �[��� 42 true false true +1.500000e+000 world 0x0 [0/0]0x0 0x0 0x0 255 PK ! ��P�. . escape_struct_return.gonu �[��� // errorcheck -0 -m -l // Copyright 2015 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test escape analysis for function parameters. package foo var Ssink *string type U struct { _sp *string _spp **string } func A(sp *string, spp **string) U { // ERROR "leaking param: sp to result ~r0 level=0$" "leaking param: spp to result ~r0 level=0$" return U{sp, spp} } func B(spp **string) U { // ERROR "leaking param: spp to result ~r0 level=0$" return U{*spp, spp} } func tA1() { s := "cat" sp := &s spp := &sp u := A(sp, spp) _ = u println(s) } func tA2() { s := "cat" sp := &s spp := &sp u := A(sp, spp) println(*u._sp) } func tA3() { s := "cat" sp := &s spp := &sp u := A(sp, spp) println(**u._spp) } func tB1() { s := "cat" sp := &s spp := &sp u := B(spp) _ = u println(s) } func tB2() { s := "cat" sp := &s spp := &sp u := B(spp) println(*u._sp) } func tB3() { s := "cat" sp := &s spp := &sp u := B(spp) println(**u._spp) } PK ! 0�F�y y bigalg.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test the internal "algorithms" for objects larger than a word: hashing, equality etc. package main type T struct { a float64 b int64 c string d byte } var a = []int{1, 2, 3} var NIL []int func arraycmptest() { if NIL != nil { println("fail1:", NIL, "!= nil") panic("bigalg") } if nil != NIL { println("fail2: nil !=", NIL) panic("bigalg") } if a == nil || nil == a { println("fail3:", a, "== nil") panic("bigalg") } } func SameArray(a, b []int) bool { if len(a) != len(b) || cap(a) != cap(b) { return false } if len(a) > 0 && &a[0] != &b[0] { return false } return true } var t = T{1.5, 123, "hello", 255} var mt = make(map[int]T) var ma = make(map[int][]int) func maptest() { mt[0] = t t1 := mt[0] if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) panic("bigalg") } ma[1] = a a1 := ma[1] if !SameArray(a, a1) { println("fail: map val array", a, a1) panic("bigalg") } } var ct = make(chan T) var ca = make(chan []int) func send() { ct <- t ca <- a } func chantest() { go send() t1 := <-ct if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { println("fail: map val struct", t1.a, t1.b, t1.c, t1.d) panic("bigalg") } a1 := <-ca if !SameArray(a, a1) { println("fail: map val array", a, a1) panic("bigalg") } } type E struct{} var e E func interfacetest() { var i interface{} i = a a1 := i.([]int) if !SameArray(a, a1) { println("interface <-> []int", a, a1) panic("bigalg") } pa := new([]int) *pa = a i = pa a1 = *i.(*[]int) if !SameArray(a, a1) { println("interface <-> *[]int", a, a1) panic("bigalg") } i = t t1 := i.(T) if t1.a != t.a || t1.b != t.b || t1.c != t.c || t1.d != t.d { println("interface <-> struct", t1.a, t1.b, t1.c, t1.d) panic("bigalg") } i = e e1 := i.(E) // nothing to check; just verify it doesn't crash _ = e1 } func main() { arraycmptest() maptest() chantest() interfacetest() } PK ! #H�� � for.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test for loops. package main func assertequal(is, shouldbe int, msg string) { if is != shouldbe { print("assertion fail", msg, "\n") panic(1) } } func main() { var i, sum int i = 0 for { i = i + 1 if i > 5 { break } } assertequal(i, 6, "break") sum = 0 for i := 0; i <= 10; i++ { sum = sum + i } assertequal(sum, 55, "all three") sum = 0 for i := 0; i <= 10; { sum = sum + i i++ } assertequal(sum, 55, "only two") sum = 0 for sum < 100 { sum = sum + 9 } assertequal(sum, 99+9, "only one") sum = 0 for i := 0; i <= 10; i++ { if i%2 == 0 { continue } sum = sum + i } assertequal(sum, 1+3+5+7+9, "continue") i = 0 for i = range [5]struct{}{} { } assertequal(i, 4, " incorrect index value after range loop") i = 0 var a1 [5]struct{} for i = range a1 { a1[i] = struct{}{} } assertequal(i, 4, " incorrect index value after array with zero size elem range clear") i = 0 var a2 [5]int for i = range a2 { a2[i] = 0 } assertequal(i, 4, " incorrect index value after array range clear") } PK ! CeA� indirect1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that illegal uses of indirection are caught by the compiler. // Does not compile. package main var m0 map[string]int var m1 *map[string]int var m2 *map[string]int = &m0 var m3 map[string]int = map[string]int{"a": 1} var m4 *map[string]int = &m3 var s0 string var s1 *string var s2 *string = &s0 var s3 string = "a" var s4 *string = &s3 var a0 [10]int var a1 *[10]int var a2 *[10]int = &a0 var b0 []int var b1 *[]int var b2 *[]int = &b0 var b3 []int = []int{1, 2, 3} var b4 *[]int = &b3 func f() { // this is spaced funny so that // the compiler will print a different // line number for each len call when // it decides there are type errors. x := len(m0)+ len(m1)+ // ERROR "illegal|invalid|must be" len(m2)+ // ERROR "illegal|invalid|must be" len(m3)+ len(m4)+ // ERROR "illegal|invalid|must be" len(s0)+ len(s1)+ // ERROR "illegal|invalid|must be" len(s2)+ // ERROR "illegal|invalid|must be" len(s3)+ len(s4)+ // ERROR "illegal|invalid|must be" len(a0)+ len(a1)+ len(a2)+ cap(a0)+ cap(a1)+ cap(a2)+ len(b0)+ len(b1)+ // ERROR "illegal|invalid|must be" len(b2)+ // ERROR "illegal|invalid|must be" len(b3)+ len(b4)+ // ERROR "illegal|invalid|must be" cap(b0)+ cap(b1)+ // ERROR "illegal|invalid|must be" cap(b2)+ // ERROR "illegal|invalid|must be" cap(b3)+ cap(b4) // ERROR "illegal|invalid|must be" _ = x } PK ! 5���) ) const1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify overflow is detected when using numeric constants. // Does not compile. package main import "unsafe" type I interface{} const ( // assume all types behave similarly to int8/uint8 Int8 int8 = 101 Minus1 int8 = -1 Uint8 uint8 = 102 Const = 103 Float32 float32 = 104.5 Float64 float64 = 105.5 ConstFloat = 106.5 Big float64 = 1e300 String = "abc" Bool = true ) var ( a1 = Int8 * 100 // ERROR "overflow|cannot convert" a2 = Int8 * -1 // OK a3 = Int8 * 1000 // ERROR "overflow|cannot convert" a4 = Int8 * int8(1000) // ERROR "overflow|cannot convert" a5 = int8(Int8 * 1000) // ERROR "overflow|cannot convert" a6 = int8(Int8 * int8(1000)) // ERROR "overflow|cannot convert" a7 = Int8 - 2*Int8 - 2*Int8 // ERROR "overflow|cannot convert" a8 = Int8 * Const / 100 // ERROR "overflow|cannot convert" a9 = Int8 * (Const / 100) // OK b1 = Uint8 * Uint8 // ERROR "overflow|cannot convert" b2 = Uint8 * -1 // ERROR "overflow|cannot convert" b3 = Uint8 - Uint8 // OK b4 = Uint8 - Uint8 - Uint8 // ERROR "overflow|cannot convert" b5 = uint8(^0) // ERROR "overflow|cannot convert" b5a = int64(^0) // OK b6 = ^uint8(0) // OK b6a = ^int64(0) // OK b7 = uint8(Minus1) // ERROR "overflow|cannot convert" b8 = uint8(int8(-1)) // ERROR "overflow|cannot convert" b8a = uint8(-1) // ERROR "overflow|cannot convert" b9 byte = (1 << 10) >> 8 // OK b10 byte = (1 << 10) // ERROR "overflow|cannot convert" b11 byte = (byte(1) << 10) >> 8 // ERROR "overflow|cannot convert" b12 byte = 1000 // ERROR "overflow|cannot convert" b13 byte = byte(1000) // ERROR "overflow|cannot convert" b14 byte = byte(100) * byte(100) // ERROR "overflow|cannot convert" b15 byte = byte(100) * 100 // ERROR "overflow|cannot convert" b16 byte = byte(0) * 1000 // ERROR "overflow|cannot convert" b16a byte = 0 * 1000 // OK b17 byte = byte(0) * byte(1000) // ERROR "overflow|cannot convert" b18 byte = Uint8 / 0 // ERROR "division by zero" c1 float64 = Big c2 float64 = Big * Big // ERROR "overflow|cannot convert" c3 float64 = float64(Big) * Big // ERROR "overflow|cannot convert" c4 = Big * Big // ERROR "overflow|cannot convert" c5 = Big / 0 // ERROR "division by zero" c6 = 1000 % 1e3 // ERROR "invalid operation|expected integer type" ) func f(int) func main() { f(Int8) // ERROR "convert|wrong type|cannot" f(Minus1) // ERROR "convert|wrong type|cannot" f(Uint8) // ERROR "convert|wrong type|cannot" f(Const) // OK f(Float32) // ERROR "convert|wrong type|cannot" f(Float64) // ERROR "convert|wrong type|cannot" f(ConstFloat) // ERROR "truncate" f(ConstFloat - 0.5) // OK f(Big) // ERROR "convert|wrong type|cannot" f(String) // ERROR "convert|wrong type|cannot|incompatible" f(Bool) // ERROR "convert|wrong type|cannot|incompatible" } const ptr = nil // ERROR "const.*nil|not constant" const _ = string([]byte(nil)) // ERROR "is not a? ?constant" const _ = uintptr(unsafe.Pointer((*int)(nil))) // ERROR "is not a? ?constant" const _ = unsafe.Pointer((*int)(nil)) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant" const _ = (*int)(nil) // ERROR "cannot be nil|invalid constant type|is not a constant|not constant" PK ! �WT\� � mallocfin.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test basic operation of finalizers. package main import ( "runtime" "time" ) const N = 250 type A struct { b *B n int } type B struct { n int } var i int var nfinal int var final [N]int // the unused return is to test finalizers with return values func finalA(a *A) (unused [N]int) { if final[a.n] != 0 { println("finalA", a.n, final[a.n]) panic("fail") } final[a.n] = 1 return } func finalB(b *B) { if final[b.n] != 1 { println("finalB", b.n, final[b.n]) panic("fail") } final[b.n] = 2 nfinal++ } func nofinalB(b *B) { panic("nofinalB run") } func main() { runtime.GOMAXPROCS(4) for i = 0; i < N; i++ { b := &B{i} a := &A{b, i} c := new(B) runtime.SetFinalizer(c, nofinalB) runtime.SetFinalizer(b, finalB) runtime.SetFinalizer(a, finalA) runtime.SetFinalizer(c, nil) } for i := 0; i < N; i++ { runtime.GC() runtime.Gosched() time.Sleep(1e6) if nfinal >= N*8/10 { break } } if nfinal < N*8/10 { println("not enough finalizing:", nfinal, "/", N) panic("fail") } } PK ! ���A A convert.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test types of constant expressions, using reflect. package main import "reflect" func typeof(x interface{}) string { return reflect.TypeOf(x).String() } func f() int { return 0 } func g() int { return 0 } type T func() int var m = map[string]T{"f": f} type A int type B int var a A = 1 var b B = 2 var x int func main() { want := typeof(g) if t := typeof(f); t != want { println("type of f is", t, "want", want) panic("fail") } want = typeof(a) if t := typeof(+a); t != want { println("type of +a is", t, "want", want) panic("fail") } if t := typeof(a + 0); t != want { println("type of a+0 is", t, "want", want) panic("fail") } } PK ! k?�;� � divide.gonu �[��� // run // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test divide corner cases. package main import "fmt" func f8(x, y, q, r int8) { if t := x / y; t != q { fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) panic("divide") } if t := x % y; t != r { fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) panic("divide") } } func f16(x, y, q, r int16) { if t := x / y; t != q { fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) panic("divide") } if t := x % y; t != r { fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) panic("divide") } } func f32(x, y, q, r int32) { if t := x / y; t != q { fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) panic("divide") } if t := x % y; t != r { fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) panic("divide") } } func f64(x, y, q, r int64) { if t := x / y; t != q { fmt.Printf("%d/%d = %d, want %d\n", x, y, t, q) panic("divide") } if t := x % y; t != r { fmt.Printf("%d%%%d = %d, want %d\n", x, y, t, r) panic("divide") } } func main() { f8(-1<<7, -1, -1<<7, 0) f16(-1<<15, -1, -1<<15, 0) f32(-1<<31, -1, -1<<31, 0) f64(-1<<63, -1, -1<<63, 0) } PK ! u�n*. . zerosize.gonu �[��� // run // Copyright 2023 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that zero-sized variables get same address as // runtime.zerobase. package main var x, y [0]int var p, q = new([0]int), new([0]int) // should get &runtime.zerobase func main() { if &x != &y { // Failing for now. x and y are at same address, but compiler optimizes &x==&y to false. Skip. // print("&x=", &x, " &y=", &y, " &x==&y = ", &x==&y, "\n") // panic("FAIL") } if p != q { print("p=", p, " q=", q, " p==q = ", p==q, "\n") panic("FAIL") } if &x != p { print("&x=", &x, " p=", p, " &x==p = ", &x==p, "\n") panic("FAIL") } if &y != p { print("&y=", &y, " p=", p, " &y==p = ", &y==p, "\n") panic("FAIL") } } PK ! ��W W runtime/READMEnu �[��� // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. The runtime directory contains tests that specifically need to be compiled as-if in the runtime package. For error-check tests, these require the additional flags -+ and -p=runtime. PK ! �&6�j j runtime/inlinegcpc.gonu �[��� // errorcheck -0 -+ -p=runtime -m // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package runtime // A function that calls runtime.getcallerpc or runtime.getcallersp() // cannot be inlined, no matter how small it is. func getcallerpc() uintptr func getcallersp() uintptr func pc() uintptr { return getcallerpc() + 1 } func cpc() uintptr { // ERROR "can inline cpc" return pc() + 2 } func sp() uintptr { return getcallersp() + 3 } func csp() uintptr { // ERROR "can inline csp" return sp() + 4 } PK ! �P�. . noinit.gonu �[��� // run //go:build !gcflags_noopt // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that many initializations can be done at link time and // generate no executable init functions. // Also test that trivial func init are optimized away. package main import ( "errors" "unsafe" ) // All these initializations should be done at link time. type S struct{ a, b, c int } type SS struct{ aa, bb, cc S } type SA struct{ a, b, c [3]int } type SC struct{ a, b, c []int } var ( zero = 2 one = 1 pi = 3.14 slice = []byte{1, 2, 3} sliceInt = []int{1, 2, 3} hello = "hello, world" bytes = []byte("hello, world") four, five = 4, 5 x, y = 0.1, "hello" nilslice []byte = nil nilmap map[string]int = nil nilfunc func() = nil nilchan chan int = nil nilptr *byte = nil ) var a = [3]int{1001, 1002, 1003} var s = S{1101, 1102, 1103} var c = []int{1201, 1202, 1203} var aa = [3][3]int{[3]int{2001, 2002, 2003}, [3]int{2004, 2005, 2006}, [3]int{2007, 2008, 2009}} var as = [3]S{S{2101, 2102, 2103}, S{2104, 2105, 2106}, S{2107, 2108, 2109}} var sa = SA{[3]int{3001, 3002, 3003}, [3]int{3004, 3005, 3006}, [3]int{3007, 3008, 3009}} var ss = SS{S{3101, 3102, 3103}, S{3104, 3105, 3106}, S{3107, 3108, 3109}} var ca = [][3]int{[3]int{4001, 4002, 4003}, [3]int{4004, 4005, 4006}, [3]int{4007, 4008, 4009}} var cs = []S{S{4101, 4102, 4103}, S{4104, 4105, 4106}, S{4107, 4108, 4109}} var answers = [...]int{ // s 1101, 1102, 1103, // ss 3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108, 3109, // [0] 1001, 1201, 1301, 2101, 2102, 2103, 4101, 4102, 4103, 5101, 5102, 5103, 3001, 3004, 3007, 3201, 3204, 3207, 3301, 3304, 3307, // [0][j] 2001, 2201, 2301, 4001, 4201, 4301, 5001, 5201, 5301, 2002, 2202, 2302, 4002, 4202, 4302, 5002, 5202, 5302, 2003, 2203, 2303, 4003, 4203, 4303, 5003, 5203, 5303, // [1] 1002, 1202, 1302, 2104, 2105, 2106, 4104, 4105, 4106, 5104, 5105, 5106, 3002, 3005, 3008, 3202, 3205, 3208, 3302, 3305, 3308, // [1][j] 2004, 2204, 2304, 4004, 4204, 4304, 5004, 5204, 5304, 2005, 2205, 2305, 4005, 4205, 4305, 5005, 5205, 5305, 2006, 2206, 2306, 4006, 4206, 4306, 5006, 5206, 5306, // [2] 1003, 1203, 1303, 2107, 2108, 2109, 4107, 4108, 4109, 5107, 5108, 5109, 3003, 3006, 3009, 3203, 3206, 3209, 3303, 3306, 3309, // [2][j] 2007, 2207, 2307, 4007, 4207, 4307, 5007, 5207, 5307, 2008, 2208, 2308, 4008, 4208, 4308, 5008, 5208, 5308, 2009, 2209, 2309, 4009, 4209, 4309, 5009, 5209, 5309, } var ( copy_zero = zero copy_one = one copy_pi = pi copy_slice = slice copy_sliceInt = sliceInt // copy_hello = hello // static init of copied strings defeats link -X; see #34675 // Could be handled without an initialization function, but // requires special handling for "a = []byte("..."); b = a" // which is not a likely case. // copy_bytes = bytes // https://codereview.appspot.com/171840043 is one approach to // make this special case work. copy_four, copy_five = four, five copy_x = x // copy_y = y // static init of copied strings defeats link -X; see #34675 copy_nilslice = nilslice copy_nilmap = nilmap copy_nilfunc = nilfunc copy_nilchan = nilchan copy_nilptr = nilptr ) var copy_a = a var copy_s = s var copy_c = c var copy_aa = aa var copy_as = as var copy_sa = sa var copy_ss = ss var copy_ca = ca var copy_cs = cs var copy_answers = answers var bx bool var b0 = false var b1 = true var fx float32 var f0 = float32(0) var f1 = float32(1) var gx float64 var g0 = float64(0) var g1 = float64(1) var ix int var i0 = 0 var i1 = 1 var jx uint var j0 = uint(0) var j1 = uint(1) var cx complex64 var c0 = complex64(0) var c1 = complex64(1) var dx complex128 var d0 = complex128(0) var d1 = complex128(1) var sx []int var s0 = []int{0, 0, 0} var s1 = []int{1, 2, 3} func fi() int { return 1 } var ax [10]int var a0 = [10]int{0, 0, 0} var a1 = [10]int{1, 2, 3, 4} type T struct{ X, Y int } var tx T var t0 = T{} var t0a = T{0, 0} var t0b = T{X: 0} var t1 = T{X: 1, Y: 2} var t1a = T{3, 4} var psx *[]int var ps0 = &[]int{0, 0, 0} var ps1 = &[]int{1, 2, 3} var pax *[10]int var pa0 = &[10]int{0, 0, 0} var pa1 = &[10]int{1, 2, 3} var ptx *T var pt0 = &T{} var pt0a = &T{0, 0} var pt0b = &T{X: 0} var pt1 = &T{X: 1, Y: 2} var pt1a = &T{3, 4} // The checks similar to // var copy_bx = bx // are commented out. The compiler no longer statically initializes them. // See issue 7665 and https://codereview.appspot.com/93200044. // If https://codereview.appspot.com/169040043 is submitted, and this // test is changed to pass -complete to the compiler, then we can // uncomment the copy lines again. // var copy_bx = bx var copy_b0 = b0 var copy_b1 = b1 // var copy_fx = fx var copy_f0 = f0 var copy_f1 = f1 // var copy_gx = gx var copy_g0 = g0 var copy_g1 = g1 // var copy_ix = ix var copy_i0 = i0 var copy_i1 = i1 // var copy_jx = jx var copy_j0 = j0 var copy_j1 = j1 // var copy_cx = cx var copy_c0 = c0 var copy_c1 = c1 // var copy_dx = dx var copy_d0 = d0 var copy_d1 = d1 // var copy_sx = sx var copy_s0 = s0 var copy_s1 = s1 // var copy_ax = ax var copy_a0 = a0 var copy_a1 = a1 // var copy_tx = tx var copy_t0 = t0 var copy_t0a = t0a var copy_t0b = t0b var copy_t1 = t1 var copy_t1a = t1a // var copy_psx = psx var copy_ps0 = ps0 var copy_ps1 = ps1 // var copy_pax = pax var copy_pa0 = pa0 var copy_pa1 = pa1 // var copy_ptx = ptx var copy_pt0 = pt0 var copy_pt0a = pt0a var copy_pt0b = pt0b var copy_pt1 = pt1 var copy_pt1a = pt1a var _ interface{} = 1 type T1 int func (t *T1) M() {} type Mer interface { M() } var _ Mer = (*T1)(nil) var Byte byte var PtrByte unsafe.Pointer = unsafe.Pointer(&Byte) var LitSXInit = &S{1, 2, 3} var LitSAnyXInit any = &S{4, 5, 6} func FS(x, y, z int) *S { return &S{x, y, z} } func FSA(x, y, z int) any { return &S{x, y, z} } func F3(x int) *S { return &S{x, x, x} } var LitSCallXInit = FS(7, 8, 9) var LitSAnyCallXInit any = FSA(10, 11, 12) var LitSRepeat = F3(1 + 2) func F0() *S { return &S{1, 2, 3} } var LitSNoArgs = F0() var myError = errors.New("mine") func gopherize(s string) string { return "gopher gopher gopher " + s } var animals = gopherize("badger") // These init funcs should optimize away. func init() { } func init() { if false { } } func init() { for false { } } // Actual test: check for init funcs in runtime data structures. type initTask struct { state uint32 nfns uint32 } //go:linkname main_inittask main..inittask var main_inittask initTask func main() { if nfns := main_inittask.nfns; nfns != 0 { println(nfns) panic("unexpected init funcs") } } PK ! St$� helloworld.outnu �[��� hello, world PK ! e\�}� � winbatch.gonu �[��� // run // Copyright 2020 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Check that batch files are maintained as CRLF files (consistent // behavior on all operating systems). See golang.org/issue/37791. package main import ( "bytes" "fmt" "io/ioutil" "log" "os" "path/filepath" "runtime" "strings" ) func main() { // Ensure that the GOROOT/src/all.bat file exists and has strict CRLF line endings. enforceBatchStrictCRLF(filepath.Join(runtime.GOROOT(), "src", "all.bat")) // Walk the entire Go repository source tree (without GOROOT/pkg), // skipping directories that start with "." and named "testdata", // and ensure all .bat files found have exact CRLF line endings. err := filepath.WalkDir(runtime.GOROOT(), func(path string, d os.DirEntry, err error) error { if err != nil { return err } if d.IsDir() && (strings.HasPrefix(d.Name(), ".") || d.Name() == "testdata") { return filepath.SkipDir } if path == filepath.Join(runtime.GOROOT(), "pkg") { // GOROOT/pkg is known to contain generated artifacts, not source code. // Skip it to avoid false positives. (Also see golang.org/issue/37929.) return filepath.SkipDir } if filepath.Ext(d.Name()) == ".bat" { enforceBatchStrictCRLF(path) } return nil }) if err != nil { log.Fatalln(err) } } func enforceBatchStrictCRLF(path string) { b, err := ioutil.ReadFile(path) if err != nil { log.Fatalln(err) } cr, lf := bytes.Count(b, []byte{13}), bytes.Count(b, []byte{10}) crlf := bytes.Count(b, []byte{13, 10}) if cr != crlf || lf != crlf { if rel, err := filepath.Rel(runtime.GOROOT(), path); err == nil { // Make the test failure more readable by showing a path relative to GOROOT. path = rel } fmt.Printf("Windows batch file %s does not use strict CRLF line termination.\n", path) fmt.Printf("Please convert it to CRLF before checking it in due to golang.org/issue/37791.\n") os.Exit(1) } } PK ! �%��M M init1.gonu �[��� // run // Copyright 2011 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that goroutines and garbage collection run during init. package main import "runtime" var x []byte func init() { c := make(chan int) go send(c) <-c const N = 1000 const MB = 1 << 20 b := make([]byte, MB) for i := range b { b[i] = byte(i%10 + '0') } s := string(b) memstats := new(runtime.MemStats) runtime.ReadMemStats(memstats) sys, numGC := memstats.Sys, memstats.NumGC // Generate 1,000 MB of garbage, only retaining 1 MB total. for i := 0; i < N; i++ { x = []byte(s) } // Verify that the garbage collector ran by seeing if we // allocated fewer than N*MB bytes from the system. runtime.ReadMemStats(memstats) sys1, numGC1 := memstats.Sys, memstats.NumGC if sys1-sys >= N*MB || numGC1 == numGC { println("allocated 1000 chunks of", MB, "and used ", sys1-sys, "memory") println("numGC went", numGC, "to", numGC1) panic("init1") } } func send(c chan int) { c <- 1 } func main() { } PK ! ���� � method1.gonu �[��� // errorcheck // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that method redeclarations are caught by the compiler. // Does not compile. package main type T struct{} func (t *T) M(int, string) // GCCGO_ERROR "previous" func (t *T) M(int, float64) {} // ERROR "already declared|redefinition" func (t T) H() // GCCGO_ERROR "previous" func (t *T) H() {} // ERROR "already declared|redefinition" func f(int, string) // GCCGO_ERROR "previous" func f(int, float64) {} // ERROR "redeclared|redefinition" func g(a int, b string) // GCCGO_ERROR "previous" func g(a int, c string) // ERROR "redeclared|redefinition" PK ! #� linkmain_run.gonu �[��� // run //go:build !nacl && !js && !wasip1 // Copyright 2014 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Run the sinit test. package main import ( "fmt" "io/ioutil" "os" "os/exec" "path/filepath" "strings" ) var tmpDir string func cleanup() { os.RemoveAll(tmpDir) } func run(cmdline ...string) { args := strings.Fields(strings.Join(cmdline, " ")) cmd := exec.Command(args[0], args[1:]...) out, err := cmd.CombinedOutput() if err != nil { fmt.Printf("$ %s\n", cmdline) fmt.Println(string(out)) fmt.Println(err) cleanup() os.Exit(1) } } func runFail(cmdline ...string) { args := strings.Fields(strings.Join(cmdline, " ")) cmd := exec.Command(args[0], args[1:]...) out, err := cmd.CombinedOutput() if err == nil { fmt.Printf("$ %s\n", cmdline) fmt.Println(string(out)) fmt.Println("SHOULD HAVE FAILED!") cleanup() os.Exit(1) } } func main() { var err error tmpDir, err = ioutil.TempDir("", "") if err != nil { fmt.Println(err) os.Exit(1) } tmp := func(name string) string { return filepath.Join(tmpDir, name) } importcfg, err := exec.Command("go", "list", "-export", "-f", "{{if .Export}}packagefile {{.ImportPath}}={{.Export}}{{end}}", "std").Output() if err != nil { fmt.Println(err) os.Exit(1) } os.WriteFile(tmp("importcfg"), importcfg, 0644) // helloworld.go is package main run("go tool compile -p=main -importcfg", tmp("importcfg"), "-o", tmp("linkmain.o"), "helloworld.go") run("go tool compile -p=main -importcfg", tmp("importcfg"), " -pack -o", tmp("linkmain.a"), "helloworld.go") run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.o")) run("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain.a")) // linkmain.go is not run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -o", tmp("linkmain1.o"), "linkmain.go") run("go tool compile -importcfg", tmp("importcfg"), "-p=notmain -pack -o", tmp("linkmain1.a"), "linkmain.go") runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.o")) runFail("go tool link -importcfg", tmp("importcfg"), "-o", tmp("linkmain.exe"), tmp("linkmain1.a")) cleanup() } PK ! $R�|� � literal2.gonu �[��� // run // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test Go2 literal syntax for basic types. // Avoid running gofmt on this file to preserve the // test cases with upper-case prefixes (0B, 0O, 0X). package main import "fmt" func assert(cond bool) { if !cond { panic("assertion failed") } } func equal(x, y interface{}) bool { if x != y { fmt.Printf("%g != %g\n", x, y) return false } return true } func main() { // 0-octals assert(0_1 == 01) assert(012 == 012) assert(0_1_2 == 012) assert(0_1_2i == complex(0, 12)) // decimal digits despite leading 0 for backward-compatibility assert(00089i == complex(0, 89)) // decimal digits despite leading 0 for backward-compatibility // decimals assert(1_000_000 == 1000000) assert(1_000i == complex(0, 1000)) // hexadecimals assert(0x_1 == 0x1) assert(0x1_2 == 0x12) assert(0x_cafe_f00d == 0xcafef00d) assert(0x_cafei == complex(0, 0xcafe)) // octals assert(0o_1 == 01) assert(0o12 == 012) assert(0o_1_2 == 012) assert(0o_1_2i == complex(0, 0o12)) // binaries assert(0b_1 == 1) assert(0b10 == 2) assert(0b_1_0 == 2) assert(0b_1_0i == complex(0, 2)) // decimal floats assert(0. == 0.0) assert(.0 == 0.0) assert(1_0. == 10.0) assert(.0_1 == 0.01) assert(1_0.0_1 == 10.01) assert(1_0.0_1i == complex(0, 10.01)) assert(0.e1_0 == 0.0e10) assert(.0e1_0 == 0.0e10) assert(1_0.e1_0 == 10.0e10) assert(.0_1e1_0 == 0.01e10) assert(1_0.0_1e1_0 == 10.01e10) assert(1_0.0_1e1_0i == complex(0, 10.01e10)) // hexadecimal floats assert(equal(0x1p-2, 0.25)) assert(equal(0x2.p10, 2048.0)) assert(equal(0x1.Fp+0, 1.9375)) assert(equal(0x.8p-0, 0.5)) assert(equal(0x1FFFp-16, 0.1249847412109375)) assert(equal(0x1.fffffffffffffp1023, 1.7976931348623157e308)) assert(equal(0x1.fffffffffffffp1023i, complex(0, 1.7976931348623157e308))) assert(equal(0x_1p-2, 0.25)) assert(equal(0x2.p1_0, 2048.0)) assert(equal(0x1_0.Fp+0, 16.9375)) assert(equal(0x_0.8p-0, 0.5)) assert(equal(0x_1FF_Fp-16, 0.1249847412109375)) assert(equal(0x1.f_ffff_ffff_ffffp1_023, 1.7976931348623157e308)) assert(equal(0x1.f_ffff_ffff_ffffp1_023i, complex(0, 1.7976931348623157e308))) } PK ! ��)�; �; map.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test maps, almost exhaustively. // Complexity (linearity) test is in maplinear.go. package main import ( "fmt" "math" "strconv" ) const count = 100 func P(a []string) string { s := "{" for i := 0; i < len(a); i++ { if i > 0 { s += "," } s += `"` + a[i] + `"` } s += "}" return s } func main() { testbasic() testfloat() testnan() } func testbasic() { // Test a map literal. mlit := map[string]int{"0": 0, "1": 1, "2": 2, "3": 3, "4": 4} for i := 0; i < len(mlit); i++ { s := string([]byte{byte(i) + '0'}) if mlit[s] != i { panic(fmt.Sprintf("mlit[%s] = %d\n", s, mlit[s])) } } mib := make(map[int]bool) mii := make(map[int]int) mfi := make(map[float32]int) mif := make(map[int]float32) msi := make(map[string]int) mis := make(map[int]string) mss := make(map[string]string) mspa := make(map[string][]string) // BUG need an interface map both ways too type T struct { i int64 // can't use string here; struct values are only compared at the top level f float32 } mipT := make(map[int]*T) mpTi := make(map[*T]int) mit := make(map[int]T) // mti := make(map[T] int) type M map[int]int mipM := make(map[int]M) var apT [2 * count]*T for i := 0; i < count; i++ { s := strconv.Itoa(i) s10 := strconv.Itoa(i * 10) f := float32(i) t := T{int64(i), f} apT[i] = new(T) apT[i].i = int64(i) apT[i].f = f apT[2*i] = new(T) // need twice as many entries as we use, for the nonexistence check apT[2*i].i = int64(i) apT[2*i].f = f m := M{i: i + 1} mib[i] = (i != 0) mii[i] = 10 * i mfi[float32(i)] = 10 * i mif[i] = 10.0 * f mis[i] = s msi[s] = i mss[s] = s10 mss[s] = s10 as := make([]string, 2) as[0] = s10 as[1] = s10 mspa[s] = as mipT[i] = apT[i] mpTi[apT[i]] = i mipM[i] = m mit[i] = t // mti[t] = i } // test len if len(mib) != count { panic(fmt.Sprintf("len(mib) = %d\n", len(mib))) } if len(mii) != count { panic(fmt.Sprintf("len(mii) = %d\n", len(mii))) } if len(mfi) != count { panic(fmt.Sprintf("len(mfi) = %d\n", len(mfi))) } if len(mif) != count { panic(fmt.Sprintf("len(mif) = %d\n", len(mif))) } if len(msi) != count { panic(fmt.Sprintf("len(msi) = %d\n", len(msi))) } if len(mis) != count { panic(fmt.Sprintf("len(mis) = %d\n", len(mis))) } if len(mss) != count { panic(fmt.Sprintf("len(mss) = %d\n", len(mss))) } if len(mspa) != count { panic(fmt.Sprintf("len(mspa) = %d\n", len(mspa))) } if len(mipT) != count { panic(fmt.Sprintf("len(mipT) = %d\n", len(mipT))) } if len(mpTi) != count { panic(fmt.Sprintf("len(mpTi) = %d\n", len(mpTi))) } // if len(mti) != count { // panic(fmt.Sprintf("len(mti) = %d\n", len(mti))) // } if len(mipM) != count { panic(fmt.Sprintf("len(mipM) = %d\n", len(mipM))) } // if len(mti) != count { // panic(fmt.Sprintf("len(mti) = %d\n", len(mti))) // } if len(mit) != count { panic(fmt.Sprintf("len(mit) = %d\n", len(mit))) } // test construction directly for i := 0; i < count; i++ { s := strconv.Itoa(i) s10 := strconv.Itoa(i * 10) f := float32(i) // BUG m := M(i, i+1) if mib[i] != (i != 0) { panic(fmt.Sprintf("mib[%d] = %t\n", i, mib[i])) } if mii[i] != 10*i { panic(fmt.Sprintf("mii[%d] = %d\n", i, mii[i])) } if mfi[f] != 10*i { panic(fmt.Sprintf("mfi[%d] = %d\n", i, mfi[f])) } if mif[i] != 10.0*f { panic(fmt.Sprintf("mif[%d] = %g\n", i, mif[i])) } if mis[i] != s { panic(fmt.Sprintf("mis[%d] = %s\n", i, mis[i])) } if msi[s] != i { panic(fmt.Sprintf("msi[%s] = %d\n", s, msi[s])) } if mss[s] != s10 { panic(fmt.Sprintf("mss[%s] = %g\n", s, mss[s])) } for j := 0; j < len(mspa[s]); j++ { if mspa[s][j] != s10 { panic(fmt.Sprintf("mspa[%s][%d] = %s\n", s, j, mspa[s][j])) } } if mipT[i].i != int64(i) || mipT[i].f != f { panic(fmt.Sprintf("mipT[%d] = %v\n", i, mipT[i])) } if mpTi[apT[i]] != i { panic(fmt.Sprintf("mpTi[apT[%d]] = %d\n", i, mpTi[apT[i]])) } // if(mti[t] != i) { // panic(fmt.Sprintf("mti[%s] = %s\n", s, mti[t])) // } if mipM[i][i] != i+1 { panic(fmt.Sprintf("mipM[%d][%d] = %d\n", i, i, mipM[i][i])) } // if(mti[t] != i) { // panic(fmt.Sprintf("mti[%v] = %d\n", t, mti[t])) // } if mit[i].i != int64(i) || mit[i].f != f { panic(fmt.Sprintf("mit[%d] = {%d %g}\n", i, mit[i].i, mit[i].f)) } } // test existence with tuple check // failed lookups yield a false value for the boolean. for i := 0; i < count; i++ { s := strconv.Itoa(i) f := float32(i) { _, b := mib[i] if !b { panic(fmt.Sprintf("tuple existence decl: mib[%d]\n", i)) } _, b = mib[i] if !b { panic(fmt.Sprintf("tuple existence assign: mib[%d]\n", i)) } } { _, b := mii[i] if !b { panic(fmt.Sprintf("tuple existence decl: mii[%d]\n", i)) } _, b = mii[i] if !b { panic(fmt.Sprintf("tuple existence assign: mii[%d]\n", i)) } } { _, b := mfi[f] if !b { panic(fmt.Sprintf("tuple existence decl: mfi[%d]\n", i)) } _, b = mfi[f] if !b { panic(fmt.Sprintf("tuple existence assign: mfi[%d]\n", i)) } } { _, b := mif[i] if !b { panic(fmt.Sprintf("tuple existence decl: mif[%d]\n", i)) } _, b = mif[i] if !b { panic(fmt.Sprintf("tuple existence assign: mif[%d]\n", i)) } } { _, b := mis[i] if !b { panic(fmt.Sprintf("tuple existence decl: mis[%d]\n", i)) } _, b = mis[i] if !b { panic(fmt.Sprintf("tuple existence assign: mis[%d]\n", i)) } } { _, b := msi[s] if !b { panic(fmt.Sprintf("tuple existence decl: msi[%d]\n", i)) } _, b = msi[s] if !b { panic(fmt.Sprintf("tuple existence assign: msi[%d]\n", i)) } } { _, b := mss[s] if !b { panic(fmt.Sprintf("tuple existence decl: mss[%d]\n", i)) } _, b = mss[s] if !b { panic(fmt.Sprintf("tuple existence assign: mss[%d]\n", i)) } } { _, b := mspa[s] if !b { panic(fmt.Sprintf("tuple existence decl: mspa[%d]\n", i)) } _, b = mspa[s] if !b { panic(fmt.Sprintf("tuple existence assign: mspa[%d]\n", i)) } } { _, b := mipT[i] if !b { panic(fmt.Sprintf("tuple existence decl: mipT[%d]\n", i)) } _, b = mipT[i] if !b { panic(fmt.Sprintf("tuple existence assign: mipT[%d]\n", i)) } } { _, b := mpTi[apT[i]] if !b { panic(fmt.Sprintf("tuple existence decl: mpTi[apT[%d]]\n", i)) } _, b = mpTi[apT[i]] if !b { panic(fmt.Sprintf("tuple existence assign: mpTi[apT[%d]]\n", i)) } } { _, b := mipM[i] if !b { panic(fmt.Sprintf("tuple existence decl: mipM[%d]\n", i)) } _, b = mipM[i] if !b { panic(fmt.Sprintf("tuple existence assign: mipM[%d]\n", i)) } } { _, b := mit[i] if !b { panic(fmt.Sprintf("tuple existence decl: mit[%d]\n", i)) } _, b = mit[i] if !b { panic(fmt.Sprintf("tuple existence assign: mit[%d]\n", i)) } } // { // _, b := mti[t] // if !b { // panic(fmt.Sprintf("tuple existence decl: mti[%d]\n", i)) // } // _, b = mti[t] // if !b { // panic(fmt.Sprintf("tuple existence assign: mti[%d]\n", i)) // } // } } // test nonexistence with tuple check // failed lookups yield a false value for the boolean. for i := count; i < 2*count; i++ { s := strconv.Itoa(i) f := float32(i) { _, b := mib[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mib[%d]", i)) } _, b = mib[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mib[%d]", i)) } } { _, b := mii[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mii[%d]", i)) } _, b = mii[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mii[%d]", i)) } } { _, b := mfi[f] if b { panic(fmt.Sprintf("tuple nonexistence decl: mfi[%d]", i)) } _, b = mfi[f] if b { panic(fmt.Sprintf("tuple nonexistence assign: mfi[%d]", i)) } } { _, b := mif[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mif[%d]", i)) } _, b = mif[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mif[%d]", i)) } } { _, b := mis[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mis[%d]", i)) } _, b = mis[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mis[%d]", i)) } } { _, b := msi[s] if b { panic(fmt.Sprintf("tuple nonexistence decl: msi[%d]", i)) } _, b = msi[s] if b { panic(fmt.Sprintf("tuple nonexistence assign: msi[%d]", i)) } } { _, b := mss[s] if b { panic(fmt.Sprintf("tuple nonexistence decl: mss[%d]", i)) } _, b = mss[s] if b { panic(fmt.Sprintf("tuple nonexistence assign: mss[%d]", i)) } } { _, b := mspa[s] if b { panic(fmt.Sprintf("tuple nonexistence decl: mspa[%d]", i)) } _, b = mspa[s] if b { panic(fmt.Sprintf("tuple nonexistence assign: mspa[%d]", i)) } } { _, b := mipT[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mipT[%d]", i)) } _, b = mipT[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mipT[%d]", i)) } } { _, b := mpTi[apT[i]] if b { panic(fmt.Sprintf("tuple nonexistence decl: mpTi[apt[%d]]", i)) } _, b = mpTi[apT[i]] if b { panic(fmt.Sprintf("tuple nonexistence assign: mpTi[apT[%d]]", i)) } } { _, b := mipM[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mipM[%d]", i)) } _, b = mipM[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mipM[%d]", i)) } } // { // _, b := mti[t] // if b { // panic(fmt.Sprintf("tuple nonexistence decl: mti[%d]", i)) // } // _, b = mti[t] // if b { // panic(fmt.Sprintf("tuple nonexistence assign: mti[%d]", i)) // } // } { _, b := mit[i] if b { panic(fmt.Sprintf("tuple nonexistence decl: mit[%d]", i)) } _, b = mit[i] if b { panic(fmt.Sprintf("tuple nonexistence assign: mit[%d]", i)) } } } // tests for structured map element updates for i := 0; i < count; i++ { s := strconv.Itoa(i) mspa[s][i%2] = "deleted" if mspa[s][i%2] != "deleted" { panic(fmt.Sprintf("update mspa[%s][%d] = %s\n", s, i%2, mspa[s][i%2])) } mipT[i].i += 1 if mipT[i].i != int64(i)+1 { panic(fmt.Sprintf("update mipT[%d].i = %d\n", i, mipT[i].i)) } mipT[i].f = float32(i + 1) if mipT[i].f != float32(i+1) { panic(fmt.Sprintf("update mipT[%d].f = %g\n", i, mipT[i].f)) } mipM[i][i]++ if mipM[i][i] != (i+1)+1 { panic(fmt.Sprintf("update mipM[%d][%d] = %d\n", i, i, mipM[i][i])) } } // test range on nil map var mnil map[string]int for _, _ = range mnil { panic("range mnil") } } func testfloat() { // Test floating point numbers in maps. // Two map keys refer to the same entry if the keys are ==. // The special cases, then, are that +0 == -0 and that NaN != NaN. { var ( pz = float32(0) nz = math.Float32frombits(1 << 31) nana = float32(math.NaN()) nanb = math.Float32frombits(math.Float32bits(nana) ^ 2) ) m := map[float32]string{ pz: "+0", nana: "NaN", nanb: "NaN", } if m[pz] != "+0" { panic(fmt.Sprintln("float32 map cannot read back m[+0]:", m[pz])) } if m[nz] != "+0" { fmt.Sprintln("float32 map does not treat", pz, "and", nz, "as equal for read") panic(fmt.Sprintln("float32 map does not treat -0 and +0 as equal for read")) } m[nz] = "-0" if m[pz] != "-0" { panic(fmt.Sprintln("float32 map does not treat -0 and +0 as equal for write")) } if _, ok := m[nana]; ok { panic(fmt.Sprintln("float32 map allows NaN lookup (a)")) } if _, ok := m[nanb]; ok { panic(fmt.Sprintln("float32 map allows NaN lookup (b)")) } if len(m) != 3 { panic(fmt.Sprintln("float32 map should have 3 entries:", m)) } m[nana] = "NaN" m[nanb] = "NaN" if len(m) != 5 { panic(fmt.Sprintln("float32 map should have 5 entries:", m)) } } { var ( pz = float64(0) nz = math.Float64frombits(1 << 63) nana = float64(math.NaN()) nanb = math.Float64frombits(math.Float64bits(nana) ^ 2) ) m := map[float64]string{ pz: "+0", nana: "NaN", nanb: "NaN", } if m[nz] != "+0" { panic(fmt.Sprintln("float64 map does not treat -0 and +0 as equal for read")) } m[nz] = "-0" if m[pz] != "-0" { panic(fmt.Sprintln("float64 map does not treat -0 and +0 as equal for write")) } if _, ok := m[nana]; ok { panic(fmt.Sprintln("float64 map allows NaN lookup (a)")) } if _, ok := m[nanb]; ok { panic(fmt.Sprintln("float64 map allows NaN lookup (b)")) } if len(m) != 3 { panic(fmt.Sprintln("float64 map should have 3 entries:", m)) } m[nana] = "NaN" m[nanb] = "NaN" if len(m) != 5 { panic(fmt.Sprintln("float64 map should have 5 entries:", m)) } } { var ( pz = complex64(0) nz = complex(0, math.Float32frombits(1<<31)) nana = complex(5, float32(math.NaN())) nanb = complex(5, math.Float32frombits(math.Float32bits(float32(math.NaN()))^2)) ) m := map[complex64]string{ pz: "+0", nana: "NaN", nanb: "NaN", } if m[nz] != "+0" { panic(fmt.Sprintln("complex64 map does not treat -0 and +0 as equal for read")) } m[nz] = "-0" if m[pz] != "-0" { panic(fmt.Sprintln("complex64 map does not treat -0 and +0 as equal for write")) } if _, ok := m[nana]; ok { panic(fmt.Sprintln("complex64 map allows NaN lookup (a)")) } if _, ok := m[nanb]; ok { panic(fmt.Sprintln("complex64 map allows NaN lookup (b)")) } if len(m) != 3 { panic(fmt.Sprintln("complex64 map should have 3 entries:", m)) } m[nana] = "NaN" m[nanb] = "NaN" if len(m) != 5 { panic(fmt.Sprintln("complex64 map should have 5 entries:", m)) } } { var ( pz = complex128(0) nz = complex(0, math.Float64frombits(1<<63)) nana = complex(5, float64(math.NaN())) nanb = complex(5, math.Float64frombits(math.Float64bits(float64(math.NaN()))^2)) ) m := map[complex128]string{ pz: "+0", nana: "NaN", nanb: "NaN", } if m[nz] != "+0" { panic(fmt.Sprintln("complex128 map does not treat -0 and +0 as equal for read")) } m[nz] = "-0" if m[pz] != "-0" { panic(fmt.Sprintln("complex128 map does not treat -0 and +0 as equal for write")) } if _, ok := m[nana]; ok { panic(fmt.Sprintln("complex128 map allows NaN lookup (a)")) } if _, ok := m[nanb]; ok { panic(fmt.Sprintln("complex128 map allows NaN lookup (b)")) } if len(m) != 3 { panic(fmt.Sprintln("complex128 map should have 3 entries:", m)) } m[nana] = "NaN" m[nanb] = "NaN" if len(m) != 5 { panic(fmt.Sprintln("complex128 map should have 5 entries:", m)) } } } func testnan() { n := 500 m := map[float64]int{} nan := math.NaN() for i := 0; i < n; i++ { m[nan] = 1 } if len(m) != n { panic("wrong size map after nan insertion") } iters := 0 for k, v := range m { iters++ if !math.IsNaN(k) { panic("not NaN") } if v != 1 { panic("wrong value") } } if iters != n { panic("wrong number of nan range iters") } } PK ! � N�� � linkx.gonu �[��� // skip // Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test the -X facility of the gc linker (6l etc.). // This test is run by linkx_run.go. package main import "fmt" var tbd string var overwrite string = "dibs" var tbdcopy = tbd var overwritecopy = overwrite var arraycopy = [2]string{tbd, overwrite} var b bool var x int func main() { fmt.Println(tbd) fmt.Println(tbdcopy) fmt.Println(arraycopy[0]) fmt.Println(overwrite) fmt.Println(overwritecopy) fmt.Println(arraycopy[1]) // Check non-string symbols are not overwritten. // This also make them used. if b || x != 0 { panic("b or x overwritten") } } PK ! �I� � initexp.gonu �[��� // errorcheck -t 10 // Copyright 2021 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package p // The init cycle diagnosis used to take exponential time // to traverse the call graph paths. This test case takes // at least two minutes on a modern laptop with the bug // and runs in a fraction of a second without it. // 10 seconds (-t 10 above) should be plenty if the code is working. var x = f() + z() // ERROR "initialization cycle" func f() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func z() int { return x } func a1() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a2() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a3() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a4() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a5() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a6() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a7() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func a8() int { return b1() + b2() + b3() + b4() + b5() + b6() + b7() } func b1() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b2() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b3() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b4() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b5() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b6() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b7() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } func b8() int { return a1() + a2() + a3() + a4() + a5() + a6() + a7() } PK ! ۞U� � stack.gonu �[��� // run // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test stack splitting code. // Try to tickle stack splitting bugs by doing // go, defer, and closure calls at different stack depths. package main type T [20]int func g(c chan int, t T) { s := 0 for i := 0; i < len(t); i++ { s += t[i] } c <- s } func d(t T) { s := 0 for i := 0; i < len(t); i++ { s += t[i] } if s != len(t) { println("bad defer", s) panic("fail") } } func f0() { // likely to make a new stack for f0, // because the call to f1 puts 3000 bytes // in our frame. f1() } func f1() [3000]byte { // likely to make a new stack for f1, // because 3000 bytes were used by f0 // and we need 3000 more for the call // to f2. if the call to morestack in f1 // does not pass the frame size, the new // stack (default size 5k) will not be big // enough for the frame, and the morestack // check in f2 will die, if we get that far // without faulting. f2() return [3000]byte{} } func f2() [3000]byte { // just take up space return [3000]byte{} } var c = make(chan int) var t T var b = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} func recur(n int) { ss := string(b) if len(ss) != len(b) { panic("bad []byte -> string") } go g(c, t) f0() s := <-c if s != len(t) { println("bad go", s) panic("fail") } f := func(t T) int { s := 0 for i := 0; i < len(t); i++ { s += t[i] } s += n return s } s = f(t) if s != len(t)+n { println("bad func", s, "at level", n) panic("fail") } if n > 0 { recur(n - 1) } defer d(t) } func main() { for i := 0; i < len(t); i++ { t[i] = 1 } recur(8000) } PK ! �ߘ � recover5.gonu �[��� // errorcheck // Copyright 2017 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Verify that recover arguments requirements are enforced by the // compiler. package main func main() { _ = recover() // OK _ = recover(1) // ERROR "too many arguments" _ = recover(1, 2) // ERROR "too many arguments" } PK ! >�˂� � escape4.gonu �[��� // errorcheck -0 -m //go:build !goexperiment.newinliner // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test, using compiler diagnostic flags, that the escape analysis is working. // Compiles but does not run. Inlining is enabled. package foo var p *int func alloc(x int) *int { // ERROR "can inline alloc" "moved to heap: x" return &x } var f func() func f1() { p = alloc(2) // ERROR "inlining call to alloc" "moved to heap: x" // Escape analysis used to miss inlined code in closures. func() { // ERROR "can inline f1.func1" p = alloc(3) // ERROR "inlining call to alloc" }() // ERROR "inlining call to f1.func1" "inlining call to alloc" "moved to heap: x" f = func() { // ERROR "func literal escapes to heap" "can inline f1.func2" p = alloc(3) // ERROR "inlining call to alloc" "moved to heap: x" } f() } func f2() {} // ERROR "can inline f2" // No inline for recover; panic now allowed to inline. func f3() { panic(1) } // ERROR "can inline f3" "1 escapes to heap" func f4() { recover() } func f5() *byte { // ERROR "can inline f5" type T struct { x [1]byte } t := new(T) // ERROR "new.T. escapes to heap" return &t.x[0] } func f6() *byte { // ERROR "can inline f6" type T struct { x struct { y byte } } t := new(T) // ERROR "new.T. escapes to heap" return &t.x.y } PK ! �\Bښ � solitaire.gonu �[��� // build // Copyright 2010 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test general operation by solving a peg solitaire game. // A version of this is in the Go playground. // Don't run it - produces too much output. // This program solves the (English) peg solitaire board game. // See also: http://en.wikipedia.org/wiki/Peg_solitaire package main const N = 11 + 1 // length of a board row (+1 for newline) // The board must be surrounded by 2 illegal fields in each direction // so that move() doesn't need to check the board boundaries. Periods // represent illegal fields, ● are pegs, and ○ are holes. var board = []rune( `........... ........... ....●●●.... ....●●●.... ..●●●●●●●.. ..●●●○●●●.. ..●●●●●●●.. ....●●●.... ....●●●.... ........... ........... `) // center is the position of the center hole if there is a single one; // otherwise it is -1. var center int func init() { n := 0 for pos, field := range board { if field == '○' { center = pos n++ } } if n != 1 { center = -1 // no single hole } } var moves int // number of times move is called // move tests if there is a peg at position pos that can jump over another peg // in direction dir. If the move is valid, it is executed and move returns true. // Otherwise, move returns false. func move(pos, dir int) bool { moves++ if board[pos] == '●' && board[pos+dir] == '●' && board[pos+2*dir] == '○' { board[pos] = '○' board[pos+dir] = '○' board[pos+2*dir] = '●' return true } return false } // unmove reverts a previously executed valid move. func unmove(pos, dir int) { board[pos] = '●' board[pos+dir] = '●' board[pos+2*dir] = '○' } // solve tries to find a sequence of moves such that there is only one peg left // at the end; if center is >= 0, that last peg must be in the center position. // If a solution is found, solve prints the board after each move in a backward // fashion (i.e., the last board position is printed first, all the way back to // the starting board position). func solve() bool { var last, n int for pos, field := range board { // try each board position if field == '●' { // found a peg for _, dir := range [...]int{-1, -N, +1, +N} { // try each direction if move(pos, dir) { // a valid move was found and executed, // see if this new board has a solution if solve() { unmove(pos, dir) println(string(board)) return true } unmove(pos, dir) } } last = pos n++ } } // tried each possible move if n == 1 && (center < 0 || last == center) { // there's only one peg left println(string(board)) return true } // no solution found for this board return false } func main() { if !solve() { println("no solution found") } println(moves, "moves tried") } PK ! q N�1 1 interface/embed3.dir/embed0.gonu �[��� // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package p type I1 interface { Foo(int) } type I2 interface { foo(int) } type M1 int func (M1) foo() {} type M2 int func (M2) foo(int) {} PK ! ���f< < interface/embed3.dir/embed1.gonu �[��� // Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import "./embed0" type X1 struct{} func (X1) Foo() {} type X2 struct{} func (X2) foo() {} type X3 struct{} func (X3) foo(int) {} type X4 struct{ p.M1 } type X5 struct{ p.M1 } func (X5) foo(int) {} type X6 struct{ p.M2 } type X7 struct{ p.M2 } func (X7) foo() {} type X8 struct{ p.M2 } func (X8) foo(int) {} func main() { var i1 interface{} = X1{} check(func() { _ = i1.(p.I1) }, "interface conversion: main.X1 is not p.I1: missing method Foo") var i2 interface{} = X2{} check(func() { _ = i2.(p.I2) }, "interface conversion: main.X2 is not p.I2: missing method foo") var i3 interface{} = X3{} check(func() { _ = i3.(p.I2) }, "interface conversion: main.X3 is not p.I2: missing method foo") var i4 interface{} = X4{} check(func() { _ = i4.(p.I2) }, "interface conversion: main.X4 is not p.I2: missing method foo") var i5 interface{} = X5{} check(func() { _ = i5.(p.I2) }, "interface conversion: main.X5 is not p.I2: missing method foo") var i6 interface{} = X6{} check(func() { _ = i6.(p.I2) }, "") var i7 interface{} = X7{} check(func() { _ = i7.(p.I2) }, "") var i8 interface{} = X8{} check(func() { _ = i8.(p.I2) }, "") } func check(f func(), msg string) { defer func() { v := recover() if v == nil { if msg == "" { return } panic("did not panic") } got := v.(error).Error() if msg != got { panic("want '" + msg + "', got '" + got + "'") } }() f() } PK ! _$ٛ � interface/embed1.dir/embed0.gonu �[��� // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that embedded interface types can have local methods. package p type T int func (t T) m() {} type I interface{ m() } type J interface{ I } func main() { var i I var j J var t T i = t j = t _ = i _ = j i = j _ = i j = i _ = j } PK ! Bt�PM M interface/embed1.dir/embed1.gonu �[��� // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Test that embedded interface types can have local methods. package main import "./embed0" type T int func (t T) m() {} type I interface { m() } type J interface { I } type PI interface { p.I } type PJ interface { p.J } func main() { var i I var j J var t T i = t j = t _ = i _ = j i = j _ = i j = i _ = j var pi PI var pj PJ var pt p.T pi = pt pj = pt _ = pi _ = pj pi = pj _ = pi pj = pi _ = pj } PK ! A��� � &