github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/cmd/vet/testdata/shift.go (about)

     1  // Copyright 2014 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  // This file contains tests for the suspicious shift checker.
     6  
     7  package testdata
     8  
     9  import "unsafe"
    10  
    11  func ShiftTest() {
    12  	var i8 int8
    13  	_ = i8 << 7
    14  	_ = (i8 + 1) << 8 // ERROR ".i8 . 1. .8 bits. too small for shift of 8"
    15  	_ = i8 << (7 + 1) // ERROR "i8 .8 bits. too small for shift of 8"
    16  	_ = i8 >> 8       // ERROR "i8 .8 bits. too small for shift of 8"
    17  	i8 <<= 8          // ERROR "i8 .8 bits. too small for shift of 8"
    18  	i8 >>= 8          // ERROR "i8 .8 bits. too small for shift of 8"
    19  	var i16 int16
    20  	_ = i16 << 15
    21  	_ = i16 << 16 // ERROR "i16 .16 bits. too small for shift of 16"
    22  	_ = i16 >> 16 // ERROR "i16 .16 bits. too small for shift of 16"
    23  	i16 <<= 16    // ERROR "i16 .16 bits. too small for shift of 16"
    24  	i16 >>= 16    // ERROR "i16 .16 bits. too small for shift of 16"
    25  	var i32 int32
    26  	_ = i32 << 31
    27  	_ = i32 << 32 // ERROR "i32 .32 bits. too small for shift of 32"
    28  	_ = i32 >> 32 // ERROR "i32 .32 bits. too small for shift of 32"
    29  	i32 <<= 32    // ERROR "i32 .32 bits. too small for shift of 32"
    30  	i32 >>= 32    // ERROR "i32 .32 bits. too small for shift of 32"
    31  	var i64 int64
    32  	_ = i64 << 63
    33  	_ = i64 << 64 // ERROR "i64 .64 bits. too small for shift of 64"
    34  	_ = i64 >> 64 // ERROR "i64 .64 bits. too small for shift of 64"
    35  	i64 <<= 64    // ERROR "i64 .64 bits. too small for shift of 64"
    36  	i64 >>= 64    // ERROR "i64 .64 bits. too small for shift of 64"
    37  	var u8 uint8
    38  	_ = u8 << 7
    39  	_ = u8 << 8 // ERROR "u8 .8 bits. too small for shift of 8"
    40  	_ = u8 >> 8 // ERROR "u8 .8 bits. too small for shift of 8"
    41  	u8 <<= 8    // ERROR "u8 .8 bits. too small for shift of 8"
    42  	u8 >>= 8    // ERROR "u8 .8 bits. too small for shift of 8"
    43  	var u16 uint16
    44  	_ = u16 << 15
    45  	_ = u16 << 16 // ERROR "u16 .16 bits. too small for shift of 16"
    46  	_ = u16 >> 16 // ERROR "u16 .16 bits. too small for shift of 16"
    47  	u16 <<= 16    // ERROR "u16 .16 bits. too small for shift of 16"
    48  	u16 >>= 16    // ERROR "u16 .16 bits. too small for shift of 16"
    49  	var u32 uint32
    50  	_ = u32 << 31
    51  	_ = u32 << 32 // ERROR "u32 .32 bits. too small for shift of 32"
    52  	_ = u32 >> 32 // ERROR "u32 .32 bits. too small for shift of 32"
    53  	u32 <<= 32    // ERROR "u32 .32 bits. too small for shift of 32"
    54  	u32 >>= 32    // ERROR "u32 .32 bits. too small for shift of 32"
    55  	var u64 uint64
    56  	_ = u64 << 63
    57  	_ = u64 << 64  // ERROR "u64 .64 bits. too small for shift of 64"
    58  	_ = u64 >> 64  // ERROR "u64 .64 bits. too small for shift of 64"
    59  	u64 <<= 64     // ERROR "u64 .64 bits. too small for shift of 64"
    60  	u64 >>= 64     // ERROR "u64 .64 bits. too small for shift of 64"
    61  	_ = u64 << u64 // Non-constant shifts should succeed.
    62  
    63  	var i int
    64  	_ = i << 31
    65  	const in = 8 * unsafe.Sizeof(i)
    66  	_ = i << in // ERROR "too small for shift"
    67  	_ = i >> in // ERROR "too small for shift"
    68  	i <<= in    // ERROR "too small for shift"
    69  	i >>= in    // ERROR "too small for shift"
    70  	const ix = 8*unsafe.Sizeof(i) - 1
    71  	_ = i << ix
    72  	_ = i >> ix
    73  	i <<= ix
    74  	i >>= ix
    75  
    76  	var u uint
    77  	_ = u << 31
    78  	const un = 8 * unsafe.Sizeof(u)
    79  	_ = u << un // ERROR "too small for shift"
    80  	_ = u >> un // ERROR "too small for shift"
    81  	u <<= un    // ERROR "too small for shift"
    82  	u >>= un    // ERROR "too small for shift"
    83  	const ux = 8*unsafe.Sizeof(u) - 1
    84  	_ = u << ux
    85  	_ = u >> ux
    86  	u <<= ux
    87  	u >>= ux
    88  
    89  	var p uintptr
    90  	_ = p << 31
    91  	const pn = 8 * unsafe.Sizeof(p)
    92  	_ = p << pn // ERROR "too small for shift"
    93  	_ = p >> pn // ERROR "too small for shift"
    94  	p <<= pn    // ERROR "too small for shift"
    95  	p >>= pn    // ERROR "too small for shift"
    96  	const px = 8*unsafe.Sizeof(p) - 1
    97  	_ = p << px
    98  	_ = p >> px
    99  	p <<= px
   100  	p >>= px
   101  
   102  	const oneIf64Bit = ^uint(0) >> 63 // allow large shifts of constants; they are used for 32/64 bit compatibility tricks
   103  
   104  	var h uintptr
   105  	h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1)))
   106  	h <<= 8 * unsafe.Sizeof(h) // ERROR "too small for shift"
   107  	h >>= 7 * unsafe.Alignof(h)
   108  	h >>= 8 * unsafe.Alignof(h) // ERROR "too small for shift"
   109  }
   110  
   111  func ShiftDeadCode() {
   112  	var i int
   113  	const iBits = 8 * unsafe.Sizeof(i)
   114  
   115  	if iBits <= 32 {
   116  		if iBits == 16 {
   117  			_ = i >> 8
   118  		} else {
   119  			_ = i >> 16
   120  		}
   121  	} else {
   122  		_ = i >> 32
   123  	}
   124  
   125  	if iBits >= 64 {
   126  		_ = i << 32
   127  		if iBits == 128 {
   128  			_ = i << 64
   129  		}
   130  	} else {
   131  		_ = i << 16
   132  	}
   133  
   134  	if iBits == 64 {
   135  		_ = i << 32
   136  	}
   137  
   138  	switch iBits {
   139  	case 128, 64:
   140  		_ = i << 32
   141  	default:
   142  		_ = i << 16
   143  	}
   144  
   145  	switch {
   146  	case iBits < 32:
   147  		_ = i << 16
   148  	case iBits > 64:
   149  		_ = i << 64
   150  	default:
   151  		_ = i << 64 // ERROR "too small for shift"
   152  	}
   153  
   154  	// Make sure other vet checks work in dead code.
   155  	if iBits == 1024 {
   156  		_ = i << 512                  // OK
   157  		fmt.Printf("foo %s bar", 123) // ERROR "arg 123 for printf verb %s of wrong type: untyped int"
   158  	}
   159  }