Commit 088a9ad5 authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: permit indices of certain non-constant shifts

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/81277Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent aaccb383
...@@ -243,21 +243,15 @@ func callrecvlist(l Nodes) bool { ...@@ -243,21 +243,15 @@ func callrecvlist(l Nodes) bool {
} }
// indexlit implements typechecking of untyped values as // indexlit implements typechecking of untyped values as
// array/slice indexes. It is equivalent to defaultlit // array/slice indexes. It is almost equivalent to defaultlit
// except for constants of numerical kind, which are acceptable // but also accepts untyped numeric values representable as
// whenever they can be represented by a value of type int. // value of type int (see also checkmake for comparison).
// The result of indexlit MUST be assigned back to n, e.g. // The result of indexlit MUST be assigned back to n, e.g.
// n.Left = indexlit(n.Left) // n.Left = indexlit(n.Left)
func indexlit(n *Node) *Node { func indexlit(n *Node) *Node {
if n == nil || !n.Type.IsUntyped() { if n != nil && n.Type != nil && n.Type.Etype == TIDEAL {
return n 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 return n
} }
...@@ -3783,6 +3777,10 @@ func checkmake(t *types.Type, arg string, n *Node) bool { ...@@ -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. // 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]) n = defaultlit(n, types.Types[TINT])
return true return true
......
...@@ -152,8 +152,7 @@ func _() { ...@@ -152,8 +152,7 @@ func _() {
var a []int var a []int
_ = a[1<<s] _ = a[1<<s]
_ = a[1.] _ = a[1.]
// For now, the spec disallows these. We may revisit past Go 1.1. _ = a[1.<<s]
_ = a[1.<<s] // ERROR "integer|shift of type float64"
_ = a[1.1<<s] // ERROR "integer|shift of type float64" _ = a[1.1<<s] // ERROR "integer|shift of type float64"
_ = make([]int, 1) _ = make([]int, 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