From 2b9b2720b89d493dbf8725d0ae6664ac7835b3af Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 7 Oct 2020 11:33:11 -0700 Subject: [PATCH] spec: split shift examples into groups for 32- and 64-bit ints In the current (pre-CL) version of the spec, the 2nd last shift example appears to be using the array declared in the last example. On a 32-bit platform, that array would have length 0, which would lead to a panic in the 2nd last example. Also, if this code were inside a function, it wouldn't compile (array declared after use). Use an explicitly declared array for that specific shift example. Also, split out all cases that produce different results for 32- vs 64-bit ints. Fixes #41835. Change-Id: Ie45114224509e4999197226f91f7f6f934449abb Reviewed-on: https://go-review.googlesource.com/c/go/+/260398 Trust: Robert Griesemer Reviewed-by: Russ Cox Reviewed-by: Ian Lance Taylor --- doc/go_spec.html | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index e9e9e42130..676407f6f2 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -3594,23 +3594,33 @@ replaced by its left operand alone.

+var a [1024]byte
 var s uint = 33
-var i = 1<<s                  // 1 has type int
-var j int32 = 1<<s            // 1 has type int32; j == 0
-var k = uint64(1<<s)          // 1 has type uint64; k == 1<<33
-var m int = 1.0<<s            // 1.0 has type int; m == 0 if ints are 32bits in size
-var n = 1.0<<s == j           // 1.0 has type int32; n == true
-var o = 1<<s == 2<<s          // 1 and 2 have type int; o == true if ints are 32bits in size
-var p = 1<<s == 1<<33         // illegal if ints are 32bits in size: 1 has type int, but 1<<33 overflows int
-var u = 1.0<<s                // illegal: 1.0 has type float64, cannot shift
-var u1 = 1.0<<s != 0          // illegal: 1.0 has type float64, cannot shift
-var u2 = 1<<s != 1.0          // illegal: 1 has type float64, cannot shift
-var v float32 = 1<<s          // illegal: 1 has type float32, cannot shift
-var w int64 = 1.0<<33         // 1.0<<33 is a constant shift expression
-var x = a[1.0<<s]             // 1.0 has type int; x == a[0] if ints are 32bits in size
-var a = make([]byte, 1.0<<s)  // 1.0 has type int; len(a) == 0 if ints are 32bits in size
-
+// The results of the following examples are given for 64-bit ints. +var i = 1<<s // 1 has type int +var j int32 = 1<<s // 1 has type int32; j == 0 +var k = uint64(1<<s) // 1 has type uint64; k == 1<<33 +var m int = 1.0<<s // 1.0 has type int; m == 1<<33 +var n = 1.0<<s == j // 1.0 has type int; n == true +var o = 1<<s == 2<<s // 1 and 2 have type int; o == false +var p = 1<<s == 1<<33 // 1 has type int; p == true +var u = 1.0<<s // illegal: 1.0 has type float64, cannot shift +var u1 = 1.0<<s != 0 // illegal: 1.0 has type float64, cannot shift +var u2 = 1<<s != 1.0 // illegal: 1 has type float64, cannot shift +var v float32 = 1<<s // illegal: 1 has type float32, cannot shift +var w int64 = 1.0<<33 // 1.0<<33 is a constant shift expression; w == 1<<33 +var x = a[1.0<<s] // panics: 1.0 has type int, but 1<<33 overflows array bounds +var b = make([]byte, 1.0<<s) // 1.0 has type int; len(b) == 1<<33 + +// The results of the following examples are given for 32-bit ints, +// which means the shifts will overflow. +var mm int = 1.0<<s // 1.0 has type int; mm == 0 +var oo = 1<<s == 2<<s // 1 and 2 have type int; oo == true +var pp = 1<<s == 1<<33 // illegal: 1 has type int, but 1<<33 overflows int +var xx = a[1.0<<s] // 1.0 has type int; xx == a[0] +var bb = make([]byte, 1.0<<s) // 1.0 has type int; len(bb) == 0 +

Operator precedence