github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/test/fixedbugs/issue4085b.go (about) 1 // run 2 3 // Copyright 2013 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package main 8 9 import ( 10 "strings" 11 "unsafe" 12 ) 13 14 type T []int 15 16 func main() { 17 n := -1 18 shouldPanic("len out of range", func() { _ = make(T, n) }) 19 shouldPanic("cap out of range", func() { _ = make(T, 0, n) }) 20 shouldPanic("len out of range", func() { _ = make(T, int64(n)) }) 21 shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) }) 22 var t *byte 23 if unsafe.Sizeof(t) == 8 { 24 var n2 int64 = 1 << 50 25 shouldPanic("len out of range", func() { _ = make(T, int(n2)) }) 26 shouldPanic("cap out of range", func() { _ = make(T, 0, int(n2)) }) 27 n2 = 1<<63 - 1 28 shouldPanic("len out of range", func() { _ = make(T, int(n2)) }) 29 shouldPanic("cap out of range", func() { _ = make(T, 0, int(n2)) }) 30 } else { 31 n = 1<<31 - 1 32 shouldPanic("len out of range", func() { _ = make(T, n) }) 33 shouldPanic("cap out of range", func() { _ = make(T, 0, n) }) 34 shouldPanic("len out of range", func() { _ = make(T, int64(n)) }) 35 shouldPanic("cap out of range", func() { _ = make(T, 0, int64(n)) }) 36 } 37 38 // Test make in append panics since the gc compiler optimizes makes in appends. 39 shouldPanic("len out of range", func() { _ = append(T{}, make(T, n)...) }) 40 shouldPanic("cap out of range", func() { _ = append(T{}, make(T, 0, n)...) }) 41 shouldPanic("len out of range", func() { _ = append(T{}, make(T, int64(n))...) }) 42 shouldPanic("cap out of range", func() { _ = append(T{}, make(T, 0, int64(n))...) }) 43 } 44 45 func shouldPanic(str string, f func()) { 46 defer func() { 47 err := recover() 48 if err == nil { 49 panic("did not panic") 50 } 51 s := err.(error).Error() 52 if !strings.Contains(s, str) { 53 panic("got panic " + s + ", want " + str) 54 } 55 }() 56 57 f() 58 }