github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/types/testdata/check/shifts.go (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 "negative shift count" */ 1
    14  		// For the test below we may decide to convert to int
    15  		// rather than uint and then report a negative shift
    16  		// count instead, which might be a better error. The
    17  		// (minor) difference is that this would restrict the
    18  		// shift count range by half (from all uint values to
    19  		// the positive int values).
    20  		// This depends on the exact spec wording which is not
    21  		// done yet.
    22  		// TODO(gri) revisit and adjust when spec change is done
    23  		_ = 1<<- /* ERROR "negative shift count" */ 1.0
    24  		_ = 1<<1075 /* ERROR "invalid shift" */
    25  		_ = 2.0<<1
    26  		_ = 1<<1.0
    27  		_ = 1<<(1+0i)
    28  
    29  		_ int = 2<<s
    30  		_ float32 = 2<<s
    31  		_ complex64 = 2<<s
    32  
    33  		_ int = 2.0<<s
    34  		_ float32 = 2.0<<s
    35  		_ complex64 = 2.0<<s
    36  
    37  		_ int = 'a'<<s
    38  		_ float32 = 'a'<<s
    39  		_ complex64 = 'a'<<s
    40  	)
    41  }
    42  
    43  func shifts1() {
    44  	// basic non-constant shifts
    45  	var (
    46  		i int
    47  		u uint
    48  
    49  		_ = 1<<0
    50  		_ = 1<<i
    51  		_ = 1<<u
    52  		_ = 1<<"foo" /* ERROR "cannot convert" */
    53  		_ = i<<0
    54  		_ = i<<- /* ERROR "negative shift count" */ 1
    55  		_ = i<<1.0
    56  		_ = 1<<(1+0i)
    57  		_ = 1 /* ERROR "overflows" */ <<100
    58  
    59  		_ uint = 1 << 0
    60  		_ uint = 1 << u
    61  		_ float32 = 1 /* ERROR "must be integer" */ << u
    62  
    63  		// issue #14822
    64  		_ = 1<<( /* ERROR "overflows uint" */ 1<<64)
    65  		_ = 1<<( /* ERROR "invalid shift count" */ 1<<64-1)
    66  
    67  		// issue #43697
    68  		_ = u<<( /* ERROR "overflows uint" */ 1<<64)
    69  		_ = u<<(1<<64-1)
    70  	)
    71  }
    72  
    73  func shifts2() {
    74  	// from the spec
    75  	var (
    76  		s uint = 33
    77  		i = 1<<s           // 1 has type int
    78  		j int32 = 1<<s     // 1 has type int32; j == 0
    79  		k = uint64(1<<s)   // 1 has type uint64; k == 1<<33
    80  		m int = 1.0<<s     // 1.0 has type int
    81  		n = 1.0<<s != i    // 1.0 has type int; n == false if ints are 32bits in size
    82  		o = 1<<s == 2<<s   // 1 and 2 have type int; o == true if ints are 32bits in size
    83  		p = 1<<s == 1<<33  // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
    84  		u = 1.0 /* ERROR "must be integer" */ <<s         // illegal: 1.0 has type float64, cannot shift
    85  		u1 = 1.0 /* ERROR "must be integer" */ <<s != 0   // illegal: 1.0 has type float64, cannot shift
    86  		u2 = 1 /* ERROR "must be integer" */ <<s != 1.0   // illegal: 1 has type float64, cannot shift
    87  		v float32 = 1 /* ERROR "must be integer" */ <<s   // illegal: 1 has type float32, cannot shift
    88  		w int64 = 1.0<<33  // 1.0<<33 is a constant shift expression
    89  	)
    90  	_, _, _, _, _, _, _, _, _, _, _, _ = i, j, k, m, n, o, p, u, u1, u2, v, w
    91  }
    92  
    93  func shifts3(a int16, b float32) {
    94  	// random tests
    95  	var (
    96  		s uint = 11
    97  		u = 1 /* ERROR "must be integer" */ <<s + 1.0
    98  		v complex128 = 1 /* ERROR "must be integer" */ << s + 1.0 /* ERROR "must be integer" */ << s + 1
    99  	)
   100  	x := 1.0 /* ERROR "must be integer" */ <<s + 1
   101  	shifts3(1.0 << s, 1 /* ERROR "must be integer" */ >> s)
   102  	_, _, _ = u, v, x
   103  }
   104  
   105  func shifts4() {
   106  	// shifts in comparisons w/ untyped operands
   107  	var s uint
   108  
   109  	_ = 1<<s == 1
   110  	_ = 1 /* ERROR "integer" */ <<s == 1.
   111  	_ = 1. /* ERROR "integer" */ <<s == 1
   112  	_ = 1. /* ERROR "integer" */ <<s == 1.
   113  
   114  	_ = 1<<s + 1 == 1
   115  	_ = 1 /* ERROR "integer" */ <<s + 1 == 1.
   116  	_ = 1 /* ERROR "integer" */ <<s + 1. == 1
   117  	_ = 1 /* ERROR "integer" */ <<s + 1. == 1.
   118  	_ = 1. /* ERROR "integer" */ <<s + 1 == 1
   119  	_ = 1. /* ERROR "integer" */ <<s + 1 == 1.
   120  	_ = 1. /* ERROR "integer" */ <<s + 1. == 1
   121  	_ = 1. /* ERROR "integer" */ <<s + 1. == 1.
   122  
   123  	_ = 1<<s == 1<<s
   124  	_ = 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
   125  	_ = 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s
   126  	_ = 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s
   127  
   128  	_ = 1<<s + 1<<s == 1
   129  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
   130  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
   131  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
   132  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1
   133  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1.
   134  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1
   135  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1.
   136  
   137  	_ = 1<<s + 1<<s == 1<<s + 1<<s
   138  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   139  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   140  	_ = 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   141  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   142  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   143  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   144  	_ = 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   145  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   146  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   147  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   148  	_ = 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   149  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   150  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1 /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   151  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1 /* ERROR "integer" */ <<s
   152  	_ = 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s == 1. /* ERROR "integer" */ <<s + 1. /* ERROR "integer" */ <<s
   153  }
   154  
   155  func shifts5() {
   156  	// shifts in comparisons w/ typed operands
   157  	var s uint
   158  	var x int
   159  
   160  	_ = 1<<s == x
   161  	_ = 1.<<s == x
   162  	_ = 1.1 /* ERROR "int" */ <<s == x
   163  
   164  	_ = 1<<s + x == 1
   165  	_ = 1<<s + x == 1.
   166  	_ = 1<<s + x == 1.1 /* ERROR "int" */
   167  	_ = 1.<<s + x == 1
   168  	_ = 1.<<s + x == 1.
   169  	_ = 1.<<s + x == 1.1 /* ERROR "int" */
   170  	_ = 1.1 /* ERROR "int" */ <<s + x == 1
   171  	_ = 1.1 /* ERROR "int" */ <<s + x == 1.
   172  	_ = 1.1 /* ERROR "int" */ <<s + x == 1.1
   173  
   174  	_ = 1<<s == x<<s
   175  	_ = 1.<<s == x<<s
   176  	_ = 1.1  /* ERROR "int" */ <<s == x<<s
   177  }
   178  
   179  func shifts6() {
   180  	// shifts as operands in non-arithmetic operations and as arguments
   181  	var a [10]int
   182  	var s uint
   183  
   184  	_ = a[1<<s]
   185  	_ = a[1.0]
   186  	_ = a[1.0<<s]
   187  
   188  	_ = make([]int, 1.0)
   189  	_ = make([]int, 1.0<<s)
   190  	_ = make([]int, 1.1 /* ERROR "must be integer" */ <<s)
   191  
   192  	_ = float32(1)
   193  	_ = float32(1 /* ERROR "must be integer" */ <<s)
   194  	_ = float32(1.0)
   195  	_ = float32(1.0 /* ERROR "must be integer" */ <<s)
   196  	_ = float32(1.1 /* ERROR "must be integer" */ <<s)
   197  
   198  	// TODO(gri) Re-enable these tests once types2 has the go/types fixes.
   199  	//           Issue #52080.
   200  	// _ = int32(0x80000000 /* ERROR "overflows int32" */ << s)
   201  	// TODO(rfindley) Eliminate the redundant error here.
   202  	// _ = int32(( /* ERROR "truncated to int32" */ 0x80000000 /* ERROR "truncated to int32" */ + 0i) << s)
   203  
   204  	_ = int(1+0i<<0)
   205  	// _ = int((1+0i)<<s)
   206  	// _ = int(1.0<<s)
   207  	// _ = int(complex(1, 0)<<s)
   208  	_ = int(float32/* ERROR "must be integer" */(1.0) <<s)
   209  	_ = int(1.1 /* ERROR must be integer */ <<s)
   210  	_ = int(( /* ERROR "must be integer" */ 1+1i)  <<s)
   211  
   212  	_ = complex(1 /* ERROR "must be integer" */ <<s, 0)
   213  
   214  	var b []int
   215  	_ = append(b, 1<<s)
   216  	_ = append(b, 1.0<<s)
   217  	_ = append(b, (1+0i)<<s)
   218  	_ = append(b, 1.1 /* ERROR "must be integer" */ <<s)
   219  	_ = append(b, (1 + 0i) <<s)
   220  	_ = append(b, ( /* ERROR "must be integer" */ 1 + 1i)  <<s)
   221  
   222  	_ = complex(1.0 /* ERROR "must be integer" */ <<s, 0)
   223  	_ = complex(1.1 /* ERROR "must be integer" */ <<s, 0)
   224  	_ = complex(0, 1.0 /* ERROR "must be integer" */ <<s)
   225  	_ = complex(0, 1.1 /* ERROR "must be integer" */ <<s)
   226  
   227  	// TODO(gri) The delete below is not type-checked correctly yet.
   228  	// var m1 map[int]string
   229  	// delete(m1, 1<<s)
   230  }
   231  
   232  func shifts7() {
   233  	// shifts of shifts
   234  	var s uint
   235  	var x int
   236  	_ = x
   237  
   238  	_ = 1<<(1<<s)
   239  	_ = 1<<(1.<<s)
   240  	_ = 1. /* ERROR "integer" */ <<(1<<s)
   241  	_ = 1. /* ERROR "integer" */ <<(1.<<s)
   242  
   243  	x = 1<<(1<<s)
   244  	x = 1<<(1.<<s)
   245  	x = 1.<<(1<<s)
   246  	x = 1.<<(1.<<s)
   247  
   248  	_ = (1<<s)<<(1<<s)
   249  	_ = (1<<s)<<(1.<<s)
   250  	_ = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
   251  	_ = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
   252  
   253  	x = (1<<s)<<(1<<s)
   254  	x = (1<<s)<<(1.<<s)
   255  	x = ( /* ERROR "integer" */ 1.<<s)<<(1<<s)
   256  	x = ( /* ERROR "integer" */ 1.<<s)<<(1.<<s)
   257  }
   258  
   259  func shifts8() {
   260  	// shift examples from shift discussion: better error messages
   261  	var s uint
   262  	_ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1
   263  	_ = 1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s == 1.0
   264  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s == 1.0
   265  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.0 == 1
   266  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1.1 == 1
   267  	_ = 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s + 1 == 1.0
   268  
   269  	// additional cases
   270  	_ = complex(1.0 /* ERROR "shifted operand 1.0 \(type float64\) must be integer" */ <<s, 1)
   271  	_ = complex(1.0, 1 /* ERROR "shifted operand 1 \(type float64\) must be integer" */ <<s)
   272  
   273  	_ = int(1.<<s)
   274  	_ = int(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   275  	_ = float32(1 /* ERROR "shifted operand .* must be integer" */ <<s)
   276  	_ = float32(1. /* ERROR "shifted operand .* must be integer" */ <<s)
   277  	_ = float32(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   278  	// TODO(gri) the error messages for these two are incorrect - disabled for now
   279  	// _ = complex64(1<<s)
   280  	// _ = complex64(1.<<s)
   281  	_ = complex64(1.1 /* ERROR "shifted operand .* must be integer" */ <<s)
   282  }
   283  
   284  func shifts9() {
   285  	// various originally failing snippets of code from the std library
   286  	// from src/compress/lzw/reader.go:90
   287  	{
   288  		var d struct {
   289  			bits     uint32
   290  			width    uint
   291  		}
   292  		_ = uint16(d.bits & (1<<d.width - 1))
   293  	}
   294  
   295  	// from src/debug/dwarf/buf.go:116
   296  	{
   297  		var ux uint64
   298  		var bits uint
   299  		x := int64(ux)
   300  		if x&(1<<(bits-1)) != 0 {}
   301  	}
   302  
   303  	// from src/encoding/asn1/asn1.go:160
   304  	{
   305  		var bytes []byte
   306  		if bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {}
   307  	}
   308  
   309  	// from src/math/big/rat.go:140
   310  	{
   311  		var exp int
   312  		var mantissa uint64
   313  		shift := uint64(-1022 - (exp - 1)) // [1..53)
   314  		_ = mantissa & (1<<shift - 1)
   315  	}
   316  
   317  	// from src/net/interface.go:51
   318  	{
   319  		type Flags uint
   320  		var f Flags
   321  		var i int
   322  		if f&(1<<uint(i)) != 0 {}
   323  	}
   324  
   325  	// from src/runtime/softfloat64.go:234
   326  	{
   327  		var gm uint64
   328  		var shift uint
   329  		_ = gm & (1<<shift - 1)
   330  	}
   331  
   332  	// from src/strconv/atof.go:326
   333  	{
   334  		var mant uint64
   335  		var mantbits uint
   336  		if mant == 2<<mantbits {}
   337  	}
   338  
   339  	// from src/route_bsd.go:82
   340  	{
   341  		var Addrs int32
   342  		const rtaRtMask = 1
   343  		var i uint
   344  		if Addrs&rtaRtMask&(1<<i) == 0 {}
   345  	}
   346  
   347  	// from src/text/scanner/scanner.go:540
   348  	{
   349  		var s struct { Whitespace uint64 }
   350  		var ch rune
   351  		for s.Whitespace&(1<<uint(ch)) != 0 {}
   352  	}
   353  }
   354  
   355  func issue5895() {
   356  	var x = 'a' << 1 // type of x must be rune
   357  	var _ rune = x
   358  }
   359  
   360  func issue11325() {
   361  	var _ = 0 >> 1.1 /* ERROR "truncated to uint" */ // example from issue 11325
   362  	_ = 0 >> 1.1 /* ERROR "truncated to uint" */
   363  	_ = 0 << 1.1 /* ERROR "truncated to uint" */
   364  	_ = 0 >> 1.
   365  	_ = 1 >> 1.1 /* ERROR "truncated to uint" */
   366  	_ = 1 >> 1.
   367  	_ = 1. >> 1
   368  	_ = 1. >> 1.
   369  	_ = 1.1 /* ERROR "must be integer" */ >> 1
   370  }
   371  
   372  func issue11594() {
   373  	var _ = complex64 /* ERROR "must be integer" */ (1) << 2 // example from issue 11594
   374  	_ = float32 /* ERROR "must be integer" */ (0) << 1
   375  	_ = float64 /* ERROR "must be integer" */ (0) >> 2
   376  	_ = complex64 /* ERROR "must be integer" */ (0) << 3
   377  	_ = complex64 /* ERROR "must be integer" */ (0) >> 4
   378  }
   379  
   380  func issue21727() {
   381  	var s uint
   382  	var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
   383  	var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
   384  	var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
   385  	var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
   386  	var _ = string(1.0 /* ERROR "cannot convert" */ << s)
   387  }
   388  
   389  func issue22969() {
   390  	var s uint
   391  	var a []byte
   392  	_ = a[0xffffffffffffffff /* ERROR "overflows int" */ <<s] // example from issue 22969
   393  	_ = make([]int, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
   394  	_ = make([]int, 0, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
   395  	var _ byte = 0x100 /* ERROR "overflows byte" */ << s
   396  	var _ int8 = 0xff /* ERROR "overflows int8" */ << s
   397  	var _ int16 = 0xffff /* ERROR "overflows int16" */ << s
   398  	var _ int32 = 0x80000000 /* ERROR "overflows int32" */ << s
   399  }