]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: permit indices of certain non-constant shifts
authorRobert Griesemer <gri@golang.org>
Thu, 30 Nov 2017 23:47:46 +0000 (15:47 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 1 Dec 2017 20:39:50 +0000 (20:39 +0000)
Per the decision for #14844, index expressions that are non-constant
shifts where the LHS operand is representable as an int are now valid.

Fixes #21693.

Change-Id: Ifafad2c0c65975e0200ce7e28d1db210e0eacd9d
Reviewed-on: https://go-review.googlesource.com/81277
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/typecheck.go
test/shift1.go

index ec4db17b1c56065ef1cba528a77eb8b7874c4cae..5285cb22d9ffbee9d5b648108e2221f1a8b40e60 100644 (file)
@@ -243,21 +243,15 @@ func callrecvlist(l Nodes) bool {
 }
 
 // indexlit implements typechecking of untyped values as
-// array/slice indexes. It is equivalent to defaultlit
-// except for constants of numerical kind, which are acceptable
-// whenever they can be represented by a value of type int.
+// array/slice indexes. It is almost equivalent to defaultlit
+// but also accepts untyped numeric values representable as
+// value of type int (see also checkmake for comparison).
 // The result of indexlit MUST be assigned back to n, e.g.
 //     n.Left = indexlit(n.Left)
 func indexlit(n *Node) *Node {
-       if n == nil || !n.Type.IsUntyped() {
-               return n
+       if n != nil && n.Type != nil && n.Type.Etype == TIDEAL {
+               return defaultlit(n, types.Types[TINT])
        }
-       switch consttype(n) {
-       case CTINT, CTRUNE, CTFLT, CTCPLX:
-               n = defaultlit(n, types.Types[TINT])
-       }
-
-       n = defaultlit(n, nil)
        return n
 }
 
@@ -3783,6 +3777,10 @@ func checkmake(t *types.Type, arg string, n *Node) bool {
        }
 
        // defaultlit is necessary for non-constants too: n might be 1.1<<k.
+       // TODO(gri) The length argument requirements for (array/slice) make
+       // are the same as for index expressions. Factor the code better;
+       // for instance, indexlit might be called here and incorporate some
+       // of the bounds checks done for make.
        n = defaultlit(n, types.Types[TINT])
 
        return true
index c81ee5154dc70fef7927a0670bb595ef3e3a2021..01ecbed53a0b588e423199233a1b7a9bdc64d1e2 100644 (file)
@@ -152,8 +152,7 @@ func _() {
        var a []int
        _ = a[1<<s]
        _ = a[1.]
-       // For now, the spec disallows these. We may revisit past Go 1.1.
-       _ = a[1.<<s]  // ERROR "integer|shift of type float64"
+       _ = a[1.<<s]
        _ = a[1.1<<s] // ERROR "integer|shift of type float64"
 
        _ = make([]int, 1)