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