modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/sliceopt.go (about) 1 // errorcheck -0 -d=append,slice,ssa/prove/debug=1 2 3 // Copyright 2015 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 // Check optimization results for append and slicing. 8 9 package main 10 11 func a1(x []int, y int) []int { 12 x = append(x, y) // ERROR "append: len-only update \(in local slice\)$" 13 return x 14 } 15 16 func a2(x []int, y int) []int { 17 return append(x, y) 18 } 19 20 func a3(x *[]int, y int) { 21 *x = append(*x, y) // ERROR "append: len-only update$" 22 } 23 24 // s1_if_false_then_anything 25 func s1_if_false_then_anything(x **[]int, xs **string, i, j int) { 26 z := (**x)[0:i] 27 z = z[i : i+1] 28 println(z) // if we get here, then we have proven that i==i+1 (this cannot happen, but the program is still being analyzed...) 29 30 zs := (**xs)[0:i] // since i=i+1 is proven, i+1 is "in bounds", ha-ha 31 zs = zs[i : i+1] // ERROR "Proved boolean IsSliceInBounds$" 32 println(zs) 33 } 34 35 func s1(x **[]int, xs **string, i, j int) { 36 var z []int 37 z = (**x)[2:] 38 z = (**x)[2:len(**x)] // ERROR "Proved boolean IsSliceInBounds$" 39 z = (**x)[2:cap(**x)] // ERROR "Proved IsSliceInBounds$" 40 z = (**x)[i:i] // -ERROR "Proved IsSliceInBounds" 41 z = (**x)[1:i:i] // ERROR "Proved boolean IsSliceInBounds$" 42 z = (**x)[i:j:0] 43 z = (**x)[i:0:j] // ERROR "Disproved IsSliceInBounds$" 44 z = (**x)[0:i:j] // ERROR "Proved boolean IsSliceInBounds$" 45 z = (**x)[0:] // ERROR "slice: omit slice operation$" 46 z = (**x)[2:8] // ERROR "Proved slicemask not needed$" 47 println(z) 48 z = (**x)[2:2] 49 z = (**x)[0:i] 50 z = (**x)[2:i:8] // ERROR "Disproved IsSliceInBounds$" "Proved IsSliceInBounds$" 51 z = (**x)[i:2:i] // ERROR "Proved IsSliceInBounds$" "Proved boolean IsSliceInBounds$" 52 53 z = z[0:i] // ERROR "Proved boolean IsSliceInBounds" 54 z = z[0:i : i+1] 55 z = z[i : i+1] // ERROR "Proved boolean IsSliceInBounds$" 56 57 println(z) 58 59 var zs string 60 zs = (**xs)[2:] 61 zs = (**xs)[2:len(**xs)] // ERROR "Proved IsSliceInBounds$" "Proved boolean IsSliceInBounds$" 62 zs = (**xs)[i:i] // -ERROR "Proved boolean IsSliceInBounds" 63 zs = (**xs)[0:] // ERROR "slice: omit slice operation$" 64 zs = (**xs)[2:8] 65 zs = (**xs)[2:2] // ERROR "Proved boolean IsSliceInBounds$" 66 zs = (**xs)[0:i] // ERROR "Proved boolean IsSliceInBounds$" 67 68 zs = zs[0:i] // See s1_if_false_then_anything above to explain the counterfactual bounds check result below 69 zs = zs[i : i+1] // ERROR "Proved boolean IsSliceInBounds$" 70 println(zs) 71 }