Source file test/fixedbugs/issue83150.go

     1  // run
     2  
     3  // Copyright 2026 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  // Issue 83150: on arm64, ppc64, riscv64 and s390x, a bounds panic whose
     8  // index is a constant outside 0..31 and whose length is in a register
     9  // reported the wrong index.
    10  
    11  package main
    12  
    13  import "strings"
    14  
    15  var s = make([]int, 8)
    16  
    17  func at(i, c int) int { return s[i-c] } // inlined: i-c folds to a constant
    18  
    19  //go:noinline
    20  func neg() int { return at(-1, 2) } // index -3
    21  
    22  //go:noinline
    23  func big() int { return s[40] }
    24  
    25  //go:noinline
    26  func slice() []int { return s[:40] }
    27  
    28  func check(f func(), want string) {
    29  	defer func() {
    30  		r := recover()
    31  		if r == nil {
    32  			panic("no panic (bug)")
    33  		}
    34  		got := r.(error).Error()
    35  		if !strings.Contains(got, want) {
    36  			panic("got " + got + ", want " + want)
    37  		}
    38  	}()
    39  	f()
    40  }
    41  
    42  func main() {
    43  	check(func() { neg() }, "index out of range [-3]")
    44  	check(func() { big() }, "index out of range [40] with length 8")
    45  	check(func() { slice() }, "slice bounds out of range [:40] with capacity 8")
    46  }
    47  

View as plain text