1// run
2
3// Copyright 2020 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// Make sure runtime.panicmakeslice* are called.
8
9package main
10
11import "strings"
12
13func main() {
14	// Test typechecking passes if len is valid
15	// but cap is out of range for len's type.
16	var x byte
17	_ = make([]int, x, 300)
18
19	capOutOfRange := func() {
20		i := 2
21		s := make([]int, i, 1)
22		s[0] = 1
23	}
24	lenOutOfRange := func() {
25		i := -1
26		s := make([]int, i, 3)
27		s[0] = 1
28	}
29
30	tests := []struct {
31		f        func()
32		panicStr string
33	}{
34		{capOutOfRange, "cap out of range"},
35		{lenOutOfRange, "len out of range"},
36	}
37
38	for _, tc := range tests {
39		shouldPanic(tc.panicStr, tc.f)
40	}
41
42}
43
44func shouldPanic(str string, f func()) {
45	defer func() {
46		err := recover()
47		runtimeErr := err.(error).Error()
48		if !strings.Contains(runtimeErr, str) {
49			panic("got panic " + runtimeErr + ", want " + str)
50		}
51	}()
52
53	f()
54}
55