1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:20:11 -07:00
go/test/shift1.go
Dan Scales 29483b3dae test: re-enable a bunch of tests with types2
Enable a bunch of types2-related error tests to run successfully, so
they no longer have to be disabled in run.go.

 - directive.go: split it into directive.go and directive2.go, since the
   possible errors are now split across the parser and noder2, so they
   can't all be reported in one file.

 - linkname2.go: similarly, split it into linkname2.go and linkname3.go
   for the same reason.

 - issue16428.go, issue17645.go, issue47201.dir/bo.go: handle slightly
   different wording by types2

 - issue5609.go: handle slight different error (array length must be
   integer vs. array bound too large).

 - float_lit3.go: handle slightly different wording (overflows
   float vs cannot convert to float)

I purposely didn't try to fix tests yet where there are extra or missing
errors on different lines, since that is not easy to make work for both
-G=3 and -G=0. In a later change, will flip to make the types2 version
match correctly, vs. the -G=0 version.

Change-Id: I6079ff258e3b90146335b9995764e3b1b56cda59
Reviewed-on: https://go-review.googlesource.com/c/go/+/368455
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Dan Scales <danscales@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2021-12-03 16:24:32 +00:00

250 lines
9.4 KiB
Go

// errorcheck
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test illegal shifts.
// Issue 1708, illegal cases.
// Does not compile.
package p
func f(x int) int { return 0 }
func g(x interface{}) int { return 0 }
func h(x float64) int { return 0 }
// from the spec
var (
s uint = 33
u = 1.0 << s // ERROR "invalid operation|shift of non-integer operand"
v float32 = 1 << s // ERROR "invalid"
)
// non-constant shift expressions
var (
e1 = g(2.0 << s) // ERROR "invalid|shift of non-integer operand"
f1 = h(2 << s) // ERROR "invalid"
g1 int64 = 1.1 << s // ERROR "truncated|must be integer"
)
// constant shift expressions
const c uint = 65
var (
a2 int = 1.0 << c // ERROR "overflow"
b2 = 1.0 << c // ERROR "overflow"
d2 = f(1.0 << c) // ERROR "overflow"
)
var (
// issues 4882, 4936.
a3 = 1.0<<s + 0 // ERROR "invalid|shift of non-integer operand"
// issue 4937
b3 = 1<<s + 1 + 1.0 // ERROR "invalid|shift of non-integer operand"
// issue 5014
c3 = complex(1<<s, 0) // ERROR "invalid|shift of type float64"
d3 int = complex(1<<s, 3) // ERROR "non-integer|cannot use.*as type int" "shift of type float64|must be integer"
e3 = real(1 << s) // ERROR "invalid"
f3 = imag(1 << s) // ERROR "invalid"
)
// from the spec
func _() {
var (
s uint = 33
i = 1 << s // 1 has type int
j int32 = 1 << s // 1 has type int32; j == 0
k = uint64(1 << s) // 1 has type uint64; k == 1<<33
m int = 1.0 << s // 1.0 has type int
n = 1.0<<s != i // 1.0 has type int; n == false if ints are 32bits in size
o = 1<<s == 2<<s // 1 and 2 have type int; o == true if ints are 32bits in size
// next test only fails on 32bit systems
// p = 1<<s == 1<<33 // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
u = 1.0 << s // ERROR "non-integer|float64"
u1 = 1.0<<s != 0 // ERROR "non-integer|float64"
u2 = 1<<s != 1.0 // ERROR "non-integer|float64"
v float32 = 1 << s // ERROR "non-integer|float32"
w int64 = 1.0 << 33 // 1.0<<33 is a constant shift expression
_, _, _, _, _, _, _, _, _, _ = j, k, m, n, o, u, u1, u2, v, w
)
// non constants arguments trigger a different path
f2 := 1.2
s2 := "hi"
_ = f2 << 2 // ERROR "shift of type float64|non-integer|must be integer"
_ = s2 << 2 // ERROR "shift of type string|non-integer|must be integer"
}
// shifts in comparisons w/ untyped operands
var (
_ = 1<<s == 1
_ = 1<<s == 1. // ERROR "invalid|shift of type float64"
_ = 1.<<s == 1 // ERROR "invalid|shift of type float64"
_ = 1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"
_ = 1<<s+1 == 1
_ = 1<<s+1 == 1. // ERROR "invalid|shift of type float64"
_ = 1<<s+1. == 1 // ERROR "invalid|shift of type float64"
_ = 1<<s+1. == 1. // ERROR "invalid|shift of type float64"
_ = 1.<<s+1 == 1 // ERROR "invalid|shift of type float64"
_ = 1.<<s+1 == 1. // ERROR "invalid|shift of type float64"
_ = 1.<<s+1. == 1 // ERROR "invalid|shift of type float64"
_ = 1.<<s+1. == 1. // ERROR "invalid|non-integer|shift of type float64"
_ = 1<<s == 1<<s
_ = 1<<s == 1.<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s == 1<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s == 1.<<s // ERROR "invalid|non-integer|shift of type float64"
_ = 1<<s+1<<s == 1
_ = 1<<s+1<<s == 1. // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1 // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1. // ERROR "invalid|shift of type float64"
_ = 1.<<s+1<<s == 1 // ERROR "invalid|shift of type float64"
_ = 1.<<s+1<<s == 1. // ERROR "invalid|shift of type float64"
_ = 1.<<s+1.<<s == 1 // ERROR "invalid|shift of type float64"
_ = 1.<<s+1.<<s == 1. // ERROR "invalid|non-integer|shift of type float64"
_ = 1<<s+1<<s == 1<<s+1<<s
_ = 1<<s+1<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1<<s == 1.<<s+1.<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1<<s+1.<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
_ = 1.<<s+1<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s+1<<s == 1<<s+1.<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s+1<<s == 1.<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s+1<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
_ = 1.<<s+1.<<s == 1<<s+1<<s // ERROR "invalid|shift of type float64"
_ = 1.<<s+1.<<s == 1<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
_ = 1.<<s+1.<<s == 1.<<s+1<<s // ERROR "invalid|non-integer|shift of type float64"
_ = 1.<<s+1.<<s == 1.<<s+1.<<s // ERROR "invalid|non-integer|shift of type float64"
)
// shifts in comparisons w/ typed operands
var (
x int
_ = 1<<s == x
_ = 1.<<s == x
_ = 1.1<<s == x // ERROR "truncated|must be integer"
_ = 1<<s+x == 1
_ = 1<<s+x == 1.
_ = 1<<s+x == 1.1 // ERROR "truncated"
_ = 1.<<s+x == 1
_ = 1.<<s+x == 1.
_ = 1.<<s+x == 1.1 // ERROR "truncated"
_ = 1.1<<s+x == 1 // ERROR "truncated|must be integer"
_ = 1.1<<s+x == 1. // ERROR "truncated|must be integer"
_ = 1.1<<s+x == 1.1 // ERROR "truncated|must be integer"
_ = 1<<s == x<<s
_ = 1.<<s == x<<s
_ = 1.1<<s == x<<s // ERROR "truncated|must be integer"
)
// shifts as operands in non-arithmetic operations and as arguments
func _() {
var s uint
var a []int
_ = a[1<<s]
_ = a[1.]
_ = a[1.<<s]
_ = a[1.1<<s] // ERROR "integer|shift of type float64"
_ = make([]int, 1)
_ = make([]int, 1.)
_ = make([]int, 1.<<s)
_ = make([]int, 1.1<<s) // ERROR "non-integer|truncated|must be integer"
_ = float32(1)
_ = float32(1 << s) // ERROR "non-integer|shift of type float32|must be integer"
_ = float32(1.)
_ = float32(1. << s) // ERROR "non-integer|shift of type float32|must be integer"
_ = float32(1.1 << s) // ERROR "non-integer|shift of type float32|must be integer"
_ = append(a, 1<<s)
_ = append(a, 1.<<s)
_ = append(a, 1.1<<s) // ERROR "truncated|must be integer"
var b []float32
_ = append(b, 1<<s) // ERROR "non-integer|type float32"
_ = append(b, 1.<<s) // ERROR "non-integer|type float32"
_ = append(b, 1.1<<s) // ERROR "non-integer|type float32|must be integer"
_ = complex(1.<<s, 0) // ERROR "non-integer|shift of type float64|must be integer"
_ = complex(1.1<<s, 0) // ERROR "non-integer|shift of type float64|must be integer"
_ = complex(0, 1.<<s) // ERROR "non-integer|shift of type float64|must be integer"
_ = complex(0, 1.1<<s) // ERROR "non-integer|shift of type float64|must be integer"
var a4 float64
var b4 int
_ = complex(1<<s, a4) // ERROR "non-integer|shift of type float64|must be integer"
_ = complex(1<<s, b4) // ERROR "invalid|non-integer|"
var m1 map[int]string
delete(m1, 1<<s)
delete(m1, 1.<<s)
delete(m1, 1.1<<s) // ERROR "truncated|shift of type float64|must be integer"
var m2 map[float32]string
delete(m2, 1<<s) // ERROR "invalid|cannot use 1 << s as type float32"
delete(m2, 1.<<s) // ERROR "invalid|cannot use 1 << s as type float32"
delete(m2, 1.1<<s) // ERROR "invalid|cannot use 1.1 << s as type float32"
}
// shifts of shifts
func _() {
var s uint
_ = 1 << (1 << s)
_ = 1 << (1. << s)
_ = 1 << (1.1 << s) // ERROR "non-integer|truncated|must be integer"
_ = 1. << (1 << s) // ERROR "non-integer|shift of type float64|must be integer"
_ = 1. << (1. << s) // ERROR "non-integer|shift of type float64|must be integer"
_ = 1.1 << (1.1 << s) // ERROR "invalid|non-integer|truncated"
_ = (1 << s) << (1 << s)
_ = (1 << s) << (1. << s)
_ = (1 << s) << (1.1 << s) // ERROR "truncated|must be integer"
_ = (1. << s) << (1 << s) // ERROR "non-integer|shift of type float64|must be integer"
_ = (1. << s) << (1. << s) // ERROR "non-integer|shift of type float64|must be integer"
_ = (1.1 << s) << (1.1 << s) // ERROR "invalid|non-integer|truncated"
var x int
x = 1 << (1 << s)
x = 1 << (1. << s)
x = 1 << (1.1 << s) // ERROR "truncated|must be integer"
x = 1. << (1 << s)
x = 1. << (1. << s)
x = 1.1 << (1.1 << s) // ERROR "truncated|must be integer"
x = (1 << s) << (1 << s)
x = (1 << s) << (1. << s)
x = (1 << s) << (1.1 << s) // ERROR "truncated|must be integer"
x = (1. << s) << (1 << s)
x = (1. << s) << (1. << s)
x = (1.1 << s) << (1.1 << s) // ERROR "truncated|must be integer"
var y float32
y = 1 << (1 << s) // ERROR "non-integer|type float32"
y = 1 << (1. << s) // ERROR "non-integer|type float32"
y = 1 << (1.1 << s) // ERROR "invalid|truncated|float32"
y = 1. << (1 << s) // ERROR "non-integer|type float32"
y = 1. << (1. << s) // ERROR "non-integer|type float32"
y = 1.1 << (1.1 << s) // ERROR "invalid|truncated|float32"
var z complex128
z = (1 << s) << (1 << s) // ERROR "non-integer|type complex128"
z = (1 << s) << (1. << s) // ERROR "non-integer|type complex128"
z = (1 << s) << (1.1 << s) // ERROR "invalid|truncated|complex128"
z = (1. << s) << (1 << s) // ERROR "non-integer|type complex128|must be integer"
z = (1. << s) << (1. << s) // ERROR "non-integer|type complex128|must be integer"
z = (1.1 << s) << (1.1 << s) // ERROR "invalid|truncated|complex128"
_, _, _ = x, y, z
}