Commit 8073f99e authored by Robert Griesemer's avatar Robert Griesemer

go/types: adjust type-checking of shifts to match compilers

For #14822.

Change-Id: Ia3f5558f3e0dcb8ee2dab54a6e9588eecc22511f
Reviewed-on: https://go-review.googlesource.com/45074Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent e4ce08af
...@@ -633,13 +633,13 @@ func (check *Checker) shift(x, y *operand, e *ast.BinaryExpr, op token.Token) { ...@@ -633,13 +633,13 @@ func (check *Checker) shift(x, y *operand, e *ast.BinaryExpr, op token.Token) {
} }
// spec: "The right operand in a shift expression must have unsigned // spec: "The right operand in a shift expression must have unsigned
// integer type or be an untyped constant that can be converted to // integer type or be an untyped constant representable by a value of
// unsigned integer type." // type uint."
switch { switch {
case isUnsigned(y.typ): case isUnsigned(y.typ):
// nothing to do // nothing to do
case isUntyped(y.typ): case isUntyped(y.typ):
check.convertUntyped(y, Typ[UntypedInt]) check.convertUntyped(y, Typ[Uint])
if y.mode == invalid { if y.mode == invalid {
x.mode = invalid x.mode = invalid
return return
......
...@@ -10,7 +10,7 @@ func shifts0() { ...@@ -10,7 +10,7 @@ func shifts0() {
s = 10 s = 10
_ = 0<<0 _ = 0<<0
_ = 1<<s _ = 1<<s
_ = 1<<- /* ERROR "invalid shift" */ 1 _ = 1<<- /* ERROR "overflows uint" */ 1
_ = 1<<1075 /* ERROR "invalid shift" */ _ = 1<<1075 /* ERROR "invalid shift" */
_ = 2.0<<1 _ = 2.0<<1
...@@ -39,12 +39,18 @@ func shifts1() { ...@@ -39,12 +39,18 @@ func shifts1() {
_ = 1<<u _ = 1<<u
_ = 1<<"foo" /* ERROR "cannot convert" */ _ = 1<<"foo" /* ERROR "cannot convert" */
_ = i<<0 _ = i<<0
_ = i<<- /* ERROR "must not be negative" */ 1 _ = i<<- /* ERROR "overflows uint" */ 1
_ = 1 /* ERROR "overflows" */ <<100 _ = 1 /* ERROR "overflows" */ <<100
_ uint = 1 << 0 _ uint = 1 << 0
_ uint = 1 << u _ uint = 1 << u
_ float32 = 1 /* ERROR "must be integer" */ << u _ float32 = 1 /* ERROR "must be integer" */ << u
// for issue 14822
_ = 1<<( /* ERROR "invalid shift count" */ 1<<63)
_ = 1<<( /* ERROR "overflows uint" */ 1<<64)
_ = u<<(1<<63) // valid
_ = u<<( /* ERROR "overflows uint" */ 1<<64)
) )
} }
...@@ -321,11 +327,11 @@ func issue5895() { ...@@ -321,11 +327,11 @@ func issue5895() {
} }
func issue11325() { func issue11325() {
var _ = 0 >> 1.1 /* ERROR "must be unsigned integer" */ // example from issue 11325 var _ = 0 >> 1.1 /* ERROR "truncated to uint" */ // example from issue 11325
_ = 0 >> 1.1 /* ERROR "must be unsigned integer" */ _ = 0 >> 1.1 /* ERROR "truncated to uint" */
_ = 0 << 1.1 /* ERROR "must be unsigned integer" */ _ = 0 << 1.1 /* ERROR "truncated to uint" */
_ = 0 >> 1. _ = 0 >> 1.
_ = 1 >> 1.1 /* ERROR "must be unsigned integer" */ _ = 1 >> 1.1 /* ERROR "truncated to uint" */
_ = 1 >> 1. _ = 1 >> 1.
_ = 1. >> 1 _ = 1. >> 1
_ = 1. >> 1. _ = 1. >> 1.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment