github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/test/fixedbugs/issue21655.go (about)

     1  // compile
     2  
     3  // Copyright 2017 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  // Make sure assembly offsets don't get too large.
     8  
     9  // To trigger issue21655, the index offset needs to be small
    10  // enough to fit into an int32 (to get rewritten to an ADDQconst)
    11  // but large enough to overflow an int32 after multiplying by the stride.
    12  
    13  package main
    14  
    15  func f1(a []int64, i int64) int64 {
    16  	return a[i+1<<30]
    17  }
    18  func f2(a []int32, i int64) int32 {
    19  	return a[i+1<<30]
    20  }
    21  func f3(a []int16, i int64) int16 {
    22  	return a[i+1<<30]
    23  }
    24  func f4(a []int8, i int64) int8 {
    25  	return a[i+1<<31]
    26  }
    27  func f5(a []float64, i int64) float64 {
    28  	return a[i+1<<30]
    29  }
    30  func f6(a []float32, i int64) float32 {
    31  	return a[i+1<<30]
    32  }
    33  
    34  // Note: Before the fix for issue 21655, f{1,2,5,6} made
    35  // the compiler crash. f3 silently generated the wrong
    36  // code, using an offset of -1<<31 instead of 1<<31.
    37  // (This is due to the assembler accepting offsets
    38  // like 0x80000000 and silently using them as
    39  // signed 32 bit offsets.)
    40  // f4 was ok, but testing it can't hurt.