github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/ssa/interp/testdata/convert.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Test conversion operations.
     6  
     7  package main
     8  
     9  func left(x int)  { _ = 1 << x }
    10  func right(x int) { _ = 1 >> x }
    11  
    12  func main() {
    13  	wantPanic(
    14  		func() {
    15  			left(-1)
    16  		},
    17  		"runtime error: negative shift amount",
    18  	)
    19  	wantPanic(
    20  		func() {
    21  			right(-1)
    22  		},
    23  		"runtime error: negative shift amount",
    24  	)
    25  	wantPanic(
    26  		func() {
    27  			const maxInt32 = 1<<31 - 1
    28  			var idx int64 = maxInt32*2 + 8
    29  			x := make([]int, 16)
    30  			_ = x[idx]
    31  		},
    32  		"runtime error: runtime error: index out of range [4294967302] with length 16",
    33  	)
    34  }
    35  
    36  func wantPanic(fn func(), s string) {
    37  	defer func() {
    38  		err := recover()
    39  		if err == nil {
    40  			panic("expected panic")
    41  		}
    42  		if got := err.(error).Error(); got != s {
    43  			panic("expected panic " + s + " got " + got)
    44  		}
    45  	}()
    46  	fn()
    47  }