]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/typecheck: remove constant bounds check
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Fri, 2 Feb 2024 02:35:07 +0000 (09:35 +0700)
committerGopher Robot <gobot@golang.org>
Tue, 20 Feb 2024 22:29:14 +0000 (22:29 +0000)
types2 handles all constant-related bounds checks in user Go code now,
so it's safe to remove the check from typecheck, avoid the inconsistency
with type parameter.

Fixes #65417

Change-Id: I82dd197b78e271725d132b5a20450ae3e90f9abc
Reviewed-on: https://go-review.googlesource.com/c/go/+/560575
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/compile/internal/typecheck/expr.go
test/fixedbugs/issue65417.go [new file with mode: 0644]

index 12d1743874caa06514d49dfcac8629a2bba3a3bd..eb9dc62d8aef9139c7adf80c9a82ed5e8bec3d21 100644 (file)
@@ -621,19 +621,6 @@ func tcIndex(n *ir.IndexExpr) ir.Node {
                        return n
                }
 
-               if !n.Bounded() && ir.IsConst(n.Index, constant.Int) {
-                       x := n.Index.Val()
-                       if constant.Sign(x) < 0 {
-                               base.Errorf("invalid %s index %v (index must be non-negative)", why, n.Index)
-                       } else if t.IsArray() && constant.Compare(x, token.GEQ, constant.MakeInt64(t.NumElem())) {
-                               base.Errorf("invalid array index %v (out of bounds for %d-element array)", n.Index, t.NumElem())
-                       } else if ir.IsConst(n.X, constant.String) && constant.Compare(x, token.GEQ, constant.MakeInt64(int64(len(ir.StringVal(n.X))))) {
-                               base.Errorf("invalid string index %v (out of bounds for %d-byte string)", n.Index, len(ir.StringVal(n.X)))
-                       } else if ir.ConstOverflow(x, types.Types[types.TINT]) {
-                               base.Errorf("invalid %s index %v (index too large)", why, n.Index)
-                       }
-               }
-
        case types.TMAP:
                n.Index = AssignConv(n.Index, t.Key(), "map index")
                n.SetType(t.Elem())
diff --git a/test/fixedbugs/issue65417.go b/test/fixedbugs/issue65417.go
new file mode 100644 (file)
index 0000000..15e84d8
--- /dev/null
@@ -0,0 +1,42 @@
+// run
+
+// Copyright 2024 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.
+
+package main
+
+import (
+       "strings"
+       "unsafe"
+)
+
+func main() {
+       shouldPanic("runtime error: index out of range", func() { f(0) })
+       shouldPanic("runtime error: index out of range", func() { g(0) })
+}
+
+func f[T byte](t T) {
+       const str = "a"
+       _ = str[unsafe.Sizeof(t)]
+}
+
+func g[T byte](t T) {
+       const str = "a"
+       _ = str[unsafe.Sizeof(t)+0]
+}
+
+func shouldPanic(str string, f func()) {
+       defer func() {
+               err := recover()
+               if err == nil {
+                       panic("did not panic")
+               }
+               s := err.(error).Error()
+               if !strings.Contains(s, str) {
+                       panic("got panic " + s + ", want " + str)
+               }
+       }()
+
+       f()
+}