]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: adjust type-checking of shifts to match compilers
authorRobert Griesemer <gri@golang.org>
Wed, 7 Jun 2017 18:32:42 +0000 (11:32 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 8 Jun 2017 16:46:35 +0000 (16:46 +0000)
For #14822.

Change-Id: Ia3f5558f3e0dcb8ee2dab54a6e9588eecc22511f
Reviewed-on: https://go-review.googlesource.com/45074
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/types/expr.go
src/go/types/testdata/shifts.src

index 7f54b0907192419f577c2b861fd3a0a798e36577..1624858329cbf5635b7c11d15f84b6f1acac30f6 100644 (file)
@@ -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
-       // integer type or be an untyped constant that can be converted to
-       // unsigned integer type."
+       // integer type or be an untyped constant representable by a value of
+       // type uint."
        switch {
        case isUnsigned(y.typ):
                // nothing to do
        case isUntyped(y.typ):
-               check.convertUntyped(y, Typ[UntypedInt])
+               check.convertUntyped(y, Typ[Uint])
                if y.mode == invalid {
                        x.mode = invalid
                        return
index 099c9ecc7cdc757ebc04323ae55b19295ee41dbf..dc029fc647aac53791aa6a3151c524e6e0cd071e 100644 (file)
@@ -10,7 +10,7 @@ func shifts0() {
                s = 10
                _ = 0<<0
                _ = 1<<s
-               _ = 1<<- /* ERROR "invalid shift" */ 1
+               _ = 1<<- /* ERROR "overflows uint" */ 1
                _ = 1<<1075 /* ERROR "invalid shift" */
                _ = 2.0<<1
 
@@ -39,12 +39,18 @@ func shifts1() {
                _ = 1<<u
                _ = 1<<"foo" /* ERROR "cannot convert" */
                _ = i<<0
-               _ = i<<- /* ERROR "must not be negative" */ 1
+               _ = i<<- /* ERROR "overflows uint" */ 1
                _ = 1 /* ERROR "overflows" */ <<100
 
                _ uint = 1 << 0
                _ uint = 1 << 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() {
 }
 
 func issue11325() {
-       var _ = 0 >> 1.1 /* ERROR "must be unsigned integer" */ // example from issue 11325
-       _ = 0 >> 1.1 /* ERROR "must be unsigned integer" */
-       _ = 0 << 1.1 /* ERROR "must be unsigned integer" */
+       var _ = 0 >> 1.1 /* ERROR "truncated to uint" */ // example from issue 11325
+       _ = 0 >> 1.1 /* ERROR "truncated to uint" */
+       _ = 0 << 1.1 /* ERROR "truncated to uint" */
        _ = 0 >> 1.
-       _ = 1 >> 1.1 /* ERROR "must be unsigned integer" */
+       _ = 1 >> 1.1 /* ERROR "truncated to uint" */
        _ = 1 >> 1.
        _ = 1. >> 1
        _ = 1. >> 1.