reject_index_pointer_to_slice.mx raw

   1  // expected: cannot index
   2  package main
   3  
   4  // Only a pointer-to-ARRAY is indexable: p[i] selects an element of the array p
   5  // points at. A pointer-to-slice is not, and both compilers used to accept it
   6  // because the indexable check recursed into the pointee.
   7  //
   8  // The cost of accepting it is not hypothetical: image/jpeg's
   9  // `qt := &d.quant[d.comp[i].tq]; ...; b[unzig[zig]] *= qt[zig]` was a pointer to
  10  // an array in Go and became a pointer to a slice header when block became a
  11  // slice. stage4 emitted `mul i32 %value, %slice` and clang rejected the module
  12  // with "'%t35' defined with type '{ ptr, i64, i64 }' but expected 'i32'".
  13  func f(p *[]byte) (out byte) {
  14  	return p[0]
  15  }
  16  
  17  func main() {
  18  	b := []byte{:4}
  19  	println(f(&b))
  20  }
  21