]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: "abc"[1] is not an ideal constant
authorAnthony Canino <anthony.canino1@gmail.com>
Sat, 10 Oct 2015 19:24:34 +0000 (15:24 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 13 Oct 2016 17:41:04 +0000 (17:41 +0000)
"abc"[1] is not like 'b', in that -"abc"[1] is uint8 math, not ideal constant math.
Delay the constantification until after ideal constant folding is over.

Fixes #11370.

Change-Id: Iba2fc00ca2455959e7bab8f4b8b4aac14b1f9858
Reviewed-on: https://go-review.googlesource.com/15740
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/ssa.go
src/cmd/compile/internal/gc/walk.go
test/fixedbugs/issue11370.go [new file with mode: 0644]

index 1e653fe6191cb6680e4b88487fc9cf64e7cd3dd7..d8dae83b5c0bd72574cadcbdbf48716bc46a81f6 100644 (file)
@@ -1941,6 +1941,12 @@ func (s *state) expr(n *Node) *ssa.Value {
        case OINDEX:
                switch {
                case n.Left.Type.IsString():
+                       if n.Bounded && Isconst(n.Left, CTSTR) && Isconst(n.Right, CTINT) {
+                               // Replace "abc"[1] with 'b'.
+                               // Delayed until now because "abc"[1] is not an ideal constant.
+                               // See test/fixedbugs/issue11370.go.
+                               return s.newValue0I(ssa.OpConst8, Types[TUINT8], int64(int8(n.Left.Val().U.(string)[n.Right.Int64()])))
+                       }
                        a := s.expr(n.Left)
                        i := s.expr(n.Right)
                        i = s.extendIndex(i, panicindex)
index 25a1d72d823b7828d1935d704970d2a5b55cb1ca..9a03f1c95963ade98927fb29bb9726a49b90bd3f 100644 (file)
@@ -1263,18 +1263,8 @@ opswitch:
                        if Debug['m'] != 0 && n.Bounded && !Isconst(n.Right, CTINT) {
                                Warn("index bounds check elided")
                        }
-                       if smallintconst(n.Right) {
-                               if !n.Bounded {
-                                       yyerror("index out of bounds")
-                               } else {
-                                       // replace "abc"[1] with 'b'.
-                                       // delayed until now because "abc"[1] is not
-                                       // an ideal constant.
-                                       v := n.Right.Int64()
-
-                                       Nodconst(n, n.Type, int64(n.Left.Val().U.(string)[v]))
-                                       n.Typecheck = 1
-                               }
+                       if smallintconst(n.Right) && !n.Bounded {
+                               yyerror("index out of bounds")
                        }
                }
 
diff --git a/test/fixedbugs/issue11370.go b/test/fixedbugs/issue11370.go
new file mode 100644 (file)
index 0000000..30f2904
--- /dev/null
@@ -0,0 +1,13 @@
+// compile
+
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// issue 11370: cmd/compile: "0"[0] should not be a constant
+
+package p
+
+func main() {
+       println(-"abc"[1] >> 1)
+}