github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/types/testdata/shifts.src (about)

     1  // Copyright 2013 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  package shifts
     6  
     7  func shifts0() {
     8  	// basic constant shifts
     9  	const (
    10  		s = 10
    11  		_ = 0<<0
    12  		_ = 1<<s
    13  		_ = 1<<- /* ERROR "overflows uint" */ 1
    14  		_ = 1<<1075 /* ERROR "invalid shift" */
    15  		_ = 2.0<<1
    16  
    17  		_ int = 2<<s
    18  		_ float32 = 2<<s
    19  		_ complex64 = 2<<s
    20  
    21  		_ int = 2.0<<s
    22  		_ float32 = 2.0<<s
    23  		_ complex64 = 2.0<<s
    24  
    25  		_ int = 'a'<<s
    26  		_ float32 = 'a'<<s
    27  		_ complex64 = 'a'<<s
    28  	)
    29  }
    30  
    31  func shifts1() {
    32  	// basic non-constant shifts
    33  	var (
    34  		i int
    35  		u uint
    36  
    37  		_ = 1<<0
    38  		_ = 1<<i /* ERROR "must be unsigned" */
    39  		_ = 1<<u
    40  		_ = 1<<"foo" /* ERROR "cannot convert" */
    41  		_ = i<<0
    42  		_ = i<<- /* ERROR "overflows uint" */ 1
    43  		_ = 1 /* ERROR "overflows" */ <<100
    44  
    45  		_ uint = 1 << 0
    46  		_ uint = 1 << u
    47  		_ float32 = 1 /* ERROR "must be integer" */ << u
    48  
    49  		// for issue 14822
    50  		_ = 1<<( /* ERROR "invalid shift count" */ 1<<63)
    51  		_ = 1<<( /* ERROR "overflows uint" */ 1<<64)
    52  		_ = u<<(1<<63) // valid
    53  		_ = u<<( /* ERROR "overflows uint" */ 1<<64)
    54  	)
    55  }
    56  
    57  func shifts2() {
    58  	// from the spec
    59  	var (
    60  		s uint = 33
    61  		i = 1<<s           // 1 has type int
    62  		j int32 = 1<<s     // 1 has type int32; j == 0
    63  		k = uint64(1<<s)   // 1 has type uint64; k == 1<<33
    64  		m int = 1.0<<s     // 1.0 has type int
    65  		n = 1.0<<s != i    // 1.0 has type int; n == false if ints are 32bits in size
    66  		o = 1<<s == 2<<s   // 1 and 2 have type int; o == true if ints are 32bits in size
    67  		p = 1<<s == 1<<33  // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
    68  		u = 1.0 /* ERROR "must be integer" */ <<s         // illegal: 1.0 has type float64, cannot shift
    69  		u1 = 1.0 /* ERROR "must be integer" */ <<s != 0   // illegal: 1.0 has type float64, cannot shift
    70  		u2 = 1 /* ERROR "must be integer" */ <<s != 1.0   // illegal: 1 has type float64, cannot shift
    71  		v float32 = 1 /* ERROR "must be integer" */ <<s   // illegal: 1 has type float32, cannot shift
    72  		w int64 = 1.0<<33  // 1.0<<33 is a constant shift expression
    73  	)
    74  	_, _, _, _, _, _, _, _, _, _, _, _ = i, j, k, m, n, o, p, u, u1, u2, v, w
    75  }
    76  
    77  func shifts3(a int16, b float32) {
    78  	// random tests
    79  	var (
    80  		s uint = 11
    81  		u = 1 /* ERROR "must be integer" */ <<s + 1.0
    82  		v complex128 = 1 /* ERROR "must be integer" */ << s + 1.0 /* ERROR "must be integer" */ << s + 1
    83  	)
    84  	x := 1.0 /* ERROR "must be integer" */ <<s + 1
    85  	shifts3(1.0 << s, 1 /* ERROR "must be integer" */ >> s)
    86  	_, _, _ = u, v, x
    87  }
    88  
    89  func shifts4() {
    90  	// shifts in comparisons w/ untyped operands
    91  	var s uint
    92  
    93  	_ = 1<<s == 1
    94  	_ = 1 /* ERROR "integer" */ <<s == 1.
    95  	_ = 1. /* ERROR "integer" */ <<s == 1
    96  	_ = 1. /* ERROR "integer" */ <<s == 1.
    97  
    98  	_ = 1<<s + 1 == 1
    99  	_ = 1 /* ERROR "integer" */ <<s + 1 == 1.
   100  	_ = 1 /* ERROR "integer" */ <<s + 1. == 1
   101  	_ = 1 /* ERROR "integer" */ <<s + 1. == 1.
   102  	_ = 1. /* ERROR "integer" */ <<s + 1 == 1
   103  	_ = 1. /* ERROR "integer" */ <<s + 1 == 1.
   104  	_ = 1. /* ERROR "integer" */ <<s + 1. == 1
   105  	_ = 1. /* ERROR "integer" */ <<s + 1. == 1.
   106  
   107  	_ = 1<<s == 1<<s
   108  	_ = 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
   109  	_ = 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s
   110  	_ = 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
   111  
   112  	_ = 1<<s + 1<<s == 1
   113  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
   114  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
   115  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
   116  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1
   117  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
   118  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
   119  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
   120  
   121  	_ = 1<<s + 1<<s == 1<<s + 1<<s
   122  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   123  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   124  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   125  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   126  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   127  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   128  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   129  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   130  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   131  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   132  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   133  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   134  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   135  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   136  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   137  }
   138  
   139  func shifts5() {
   140  	// shifts in comparisons w/ typed operands
   141  	var s uint
   142  	var x int
   143  
   144  	_ = 1<<s == x
   145  	_ = 1.<<s == x
   146  	_ = 1.1 /* ERROR "int" */ <<s == x
   147  
   148  	_ = 1<<s + x == 1
   149  	_ = 1<<s + x == 1.
   150  	_ = 1<<s + x == 1.1 /* ERROR "int" */
   151  	_ = 1.<<s + x == 1
   152  	_ = 1.<<s + x == 1.
   153  	_ = 1.<<s + x == 1.1 /* ERROR "int" */
   154  	_ = 1.1 /* ERROR "int" */ <<s + x == 1
   155  	_ = 1.1 /* ERROR "int" */ <<s + x == 1.
   156  	_ = 1.1 /* ERROR "int" */ <<s + x == 1.1
   157  
   158  	_ = 1<<s == x<<s
   159  	_ = 1.<<s == x<<s
   160  	_ = 1.1  /* ERROR "int" */ <<s == x<<s
   161  }
   162  
   163  func shifts6() {
   164  	// shifts as operands in non-arithmetic operations and as arguments
   165  	var a [10]int
   166  	var s uint
   167  
   168  	_ = a[1<<s]
   169  	_ = a[1.0]
   170  	_ = a[1.0<<s]
   171  
   172  	_ = make([]int, 1.0)
   173  	_ = make([]int, 1.0<<s)
   174  	_ = make([]int, 1.1 /* ERROR "must be integer" */ <<s)
   175  
   176  	_ = float32(1)
   177  	_ = float32(1 /* ERROR "must be integer" */ <<s)
   178  	_ = float32(1.0)
   179  	_ = float32(1.0 /* ERROR "must be integer" */ <<s)
   180  	_ = float32(1.1 /* ERROR "must be integer" */ <<s)
   181  
   182  	var b []int
   183  	_ = append(b, 1<<s)
   184  	_ = append(b, 1.0<<s)
   185  	_ = append(b, 1.1 /* ERROR "must be integer" */ <<s)
   186  
   187  	_ = append(b, 1<<s)
   188  	_ = append(b, 1.0<<s) // should fail - see TODO in append code
   189  	_ = append(b, 1.1 /* ERROR "must be integer" */ <<s)
   190  
   191  	_ = complex(1.0 /* ERROR "must be integer" */ <<s, 0)
   192  	_ = complex(1.1 /* ERROR "must be integer" */ <<s, 0)
   193  	_ = complex(0, 1.0 /* ERROR "must be integer" */ <<s)
   194  	_ = complex(0, 1.1 /* ERROR "must be integer" */ <<s)
   195  
   196  	// TODO(gri) The delete below is not type-checked correctly yet.
   197  	// var m1 map[int]string
   198  	// delete(m1, 1<<s)
   199  }
   200  
   201  func shifts7() {
   202  	// shifts of shifts
   203  	var s uint
   204  	var x int
   205  	_ = x
   206  
   207  	_ = 1<<(1<<s)
   208  	_ = 1<<(1.<<s)
   209  	_ = 1. /* ERROR "integer" */ <<(1<<s)
   210  	_ = 1. /* ERROR "integer" */ <<(1.<<s)
   211  
   212  	x = 1<<(1<<s)
   213  	x = 1<<(1.<<s)
   214  	x = 1.<<(1<<s)
   215  	x = 1.<<(1.<<s)
   216  
   217  	_ = (1<<s)<<(1<<s)
   218  	_ = (1<<s)<<(1.<<s)
   219  	_ = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
   220  	_ = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
   221  
   222  	x = (1<<s)<<(1<<s)
   223  	x = (1<<s)<<(1.<<s)
   224  	x = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
   225  	x = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
   226  }
   227  
   228  func shifts8() {
   229  	// shift examples from shift discussion: better error messages
   230  	var s uint
   231  	_ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1
   232  	_ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1.0
   233  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s == 1.0
   234  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.0 == 1
   235  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.1 == 1
   236  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1 == 1.0
   237  
   238  	// additional cases
   239  	_ = complex(1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s, 1)
   240  	_ = complex(1.0, 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s)
   241  
   242  	_ = int(1.<<s)
   243  	_ = int(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   244  	_ = float32(1 /* ERROR "shifted operand .* must be integer" */ <<s)
   245  	_ = float32(1. /* ERROR "shifted operand .* must be integer" */ <<s)
   246  	_ = float32(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   247  	// TODO(gri) the error messages for these two are incorrect - disabled for now
   248  	// _ = complex64(1<<s)
   249  	// _ = complex64(1.<<s)
   250  	_ = complex64(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   251  }
   252  
   253  func shifts9() {
   254  	// various originally failing snippets of code from the std library
   255  	// from src/compress/lzw/reader.go:90
   256  	{
   257  		var d struct {
   258  			bits     uint32
   259  			width    uint
   260  		}
   261  		_ = uint16(d.bits & (1<<d.width - 1))
   262  	}
   263  
   264  	// from src/debug/dwarf/buf.go:116
   265  	{
   266  		var ux uint64
   267  		var bits uint
   268  		x := int64(ux)
   269  		if x&(1<<(bits-1)) != 0 {}
   270  	}
   271  
   272  	// from src/encoding/asn1/asn1.go:160
   273  	{
   274  		var bytes []byte
   275  		if bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {}
   276  	}
   277  
   278  	// from src/math/big/rat.go:140
   279  	{
   280  		var exp int
   281  		var mantissa uint64
   282  		shift := uint64(-1022 - (exp - 1)) // [1..53)
   283  		_ = mantissa & (1<<shift - 1)
   284  	}
   285  
   286  	// from src/net/interface.go:51
   287  	{
   288  		type Flags uint
   289  		var f Flags
   290  		var i int
   291  		if f&(1<<uint(i)) != 0 {}
   292  	}
   293  
   294  	// from src/runtime/softfloat64.go:234
   295  	{
   296  		var gm uint64
   297  		var shift uint
   298  		_ = gm & (1<<shift - 1)
   299  	}
   300  
   301  	// from src/strconv/atof.go:326
   302  	{
   303  		var mant uint64
   304  		var mantbits uint
   305  		if mant == 2<<mantbits {}
   306  	}
   307  
   308  	// from src/route_bsd.go:82
   309  	{
   310  		var Addrs int32
   311  		const rtaRtMask = 1
   312  		var i uint
   313  		if Addrs&rtaRtMask&(1<<i) == 0 {}
   314  	}
   315  
   316  	// from src/text/scanner/scanner.go:540
   317  	{
   318  		var s struct { Whitespace uint64 }
   319  		var ch rune
   320  		for s.Whitespace&(1<<uint(ch)) != 0 {}
   321  	}
   322  }
   323  
   324  func issue5895() {
   325  	var x = 'a' << 1 // type of x must be rune
   326  	var _ rune = x
   327  }
   328  
   329  func issue11325() {
   330  	var _ = 0 >> 1.1 /* ERROR "truncated to uint" */ // example from issue 11325
   331  	_ = 0 >> 1.1 /* ERROR "truncated to uint" */
   332  	_ = 0 << 1.1 /* ERROR "truncated to uint" */
   333  	_ = 0 >> 1.
   334  	_ = 1 >> 1.1 /* ERROR "truncated to uint" */
   335  	_ = 1 >> 1.
   336  	_ = 1. >> 1
   337  	_ = 1. >> 1.
   338  	_ = 1.1 /* ERROR "must be integer" */ >> 1
   339  }
   340  
   341  func issue11594() {
   342  	var _ = complex64 /* ERROR "must be integer" */ (1) << 2 // example from issue 11594
   343  	_ = float32 /* ERROR "must be integer" */ (0) << 1
   344  	_ = float64 /* ERROR "must be integer" */ (0) >> 2
   345  	_ = complex64 /* ERROR "must be integer" */ (0) << 3
   346  	_ = complex64 /* ERROR "must be integer" */ (0) >> 4
   347  }