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