blob: 530898985721ebe0fc22191ff3ce1034678779cd (
plain)
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
|
// 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.
package main
import (
"strings"
"unsafe"
)
func shift[T any]() int64 {
return 1 << unsafe.Sizeof(*new(T))
}
func div[T any]() uintptr {
return 1 / unsafe.Sizeof(*new(T))
}
func add[T any]() int64 {
return 1<<63 - 1 + int64(unsafe.Sizeof(*new(T)))
}
func main() {
shift[[62]byte]()
shift[[63]byte]()
shift[[64]byte]()
shift[[100]byte]()
shift[[1e6]byte]()
add[[1]byte]()
shouldPanic("divide by zero", func() { div[[0]byte]() })
}
func shouldPanic(str string, f func()) {
defer func() {
err := recover()
if err == nil {
panic("did not panic")
}
s := err.(error).Error()
if !strings.Contains(s, str) {
panic("got panic " + s + ", want " + str)
}
}()
f()
}
|