From 088a9ad543398fa6e656dd5e6f837fb07caada16 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 30 Nov 2017 15:47:46 -0800 Subject: [PATCH] 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/81277 Reviewed-by: Matthew Dempsky --- src/cmd/compile/internal/gc/typecheck.go | 20 +++++++++----------- test/shift1.go | 3 +-- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go index ec4db17b1c5..5285cb22d9f 100644 --- a/src/cmd/compile/internal/gc/typecheck.go +++ b/src/cmd/compile/internal/gc/typecheck.go @@ -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<