]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/typecheck: simplify tcSlice
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 8 Sep 2024 14:45:36 +0000 (21:45 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 11 Sep 2024 22:58:05 +0000 (22:58 +0000)
types2 handles all constant-related bounds checks in user Go code now,
so it's safe to remove all constants checking in tcSlice function.

Change-Id: Ia0dd4928d7122a6f62df10436bb4a3675ebf3357
Reviewed-on: https://go-review.googlesource.com/c/go/+/611676
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Tim King <taking@google.com>
src/cmd/compile/internal/typecheck/expr.go
src/cmd/compile/internal/typecheck/typecheck.go

index eb92797259187134efb57469fe882fb81ae7f6fe..2eec8d41ad18567f1fa0b4a1cb8dd802f83166c2 100644 (file)
@@ -788,19 +788,15 @@ func tcSlice(n *ir.SliceExpr) ir.Node {
                return n
        }
 
-       if n.Low != nil && !checksliceindex(l, n.Low, tp) {
+       if n.Low != nil && !checksliceindex(n.Low) {
                n.SetType(nil)
                return n
        }
-       if n.High != nil && !checksliceindex(l, n.High, tp) {
+       if n.High != nil && !checksliceindex(n.High) {
                n.SetType(nil)
                return n
        }
-       if n.Max != nil && !checksliceindex(l, n.Max, tp) {
-               n.SetType(nil)
-               return n
-       }
-       if !checksliceconst(n.Low, n.High) || !checksliceconst(n.Low, n.Max) || !checksliceconst(n.High, n.Max) {
+       if n.Max != nil && !checksliceindex(n.Max) {
                n.SetType(nil)
                return n
        }
index 5d041b1939f983b3d3194b8565d40943954218d5..cb48bfd7e3562a01230186d05c8ac49a654a4805 100644 (file)
@@ -7,7 +7,6 @@ package typecheck
 import (
        "fmt"
        "go/constant"
-       "go/token"
        "strings"
 
        "cmd/compile/internal/base"
@@ -681,7 +680,7 @@ func RewriteMultiValueCall(n ir.InitNode, call ir.Node) {
        }
 }
 
-func checksliceindex(l ir.Node, r ir.Node, tp *types.Type) bool {
+func checksliceindex(r ir.Node) bool {
        t := r.Type()
        if t == nil {
                return false
@@ -690,33 +689,6 @@ func checksliceindex(l ir.Node, r ir.Node, tp *types.Type) bool {
                base.Errorf("invalid slice index %v (type %v)", r, t)
                return false
        }
-
-       if r.Op() == ir.OLITERAL {
-               x := r.Val()
-               if constant.Sign(x) < 0 {
-                       base.Errorf("invalid slice index %v (index must be non-negative)", r)
-                       return false
-               } else if tp != nil && tp.NumElem() >= 0 && constant.Compare(x, token.GTR, constant.MakeInt64(tp.NumElem())) {
-                       base.Errorf("invalid slice index %v (out of bounds for %d-element array)", r, tp.NumElem())
-                       return false
-               } else if ir.IsConst(l, constant.String) && constant.Compare(x, token.GTR, constant.MakeInt64(int64(len(ir.StringVal(l))))) {
-                       base.Errorf("invalid slice index %v (out of bounds for %d-byte string)", r, len(ir.StringVal(l)))
-                       return false
-               } else if ir.ConstOverflow(x, types.Types[types.TINT]) {
-                       base.Errorf("invalid slice index %v (index too large)", r)
-                       return false
-               }
-       }
-
-       return true
-}
-
-func checksliceconst(lo ir.Node, hi ir.Node) bool {
-       if lo != nil && hi != nil && lo.Op() == ir.OLITERAL && hi.Op() == ir.OLITERAL && constant.Compare(lo.Val(), token.GTR, hi.Val()) {
-               base.Errorf("invalid slice index: %v > %v", lo, hi)
-               return false
-       }
-
        return true
 }