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

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

index 66ef167d35894ddc482e94d29c9b02c3145c2901..4f01abc45e3dc66568fd8e92fd2c67e4c747a044 100644 (file)
@@ -487,9 +487,6 @@ func (s *Schedule) initplan(n ir.Node) {
                        if a.Op() == ir.OKEY {
                                kv := a.(*ir.KeyExpr)
                                k = typecheck.IndexConst(kv.Key)
-                               if k < 0 {
-                                       base.Fatalf("initplan arraylit: invalid index %v", kv.Key)
-                               }
                                a = kv.Value
                        }
                        s.addvalue(p, k*n.Type().Elem().Size(), a)
index e7f9ec5cd81a31cd6aa8869cf9e119c2f5dd32c9..fc6e799e747bb2640225a55f9972f43e6c933d66 100644 (file)
@@ -425,27 +425,9 @@ func defaultType(t *types.Type) *types.Type {
        return nil
 }
 
-// IndexConst checks if Node n contains a constant expression
-// representable as a non-negative int and returns its value.
-// If n is not a constant expression, not representable as an
-// integer, or negative, it returns -1. If n is too large, it
-// returns -2.
+// IndexConst returns the index value of constant Node n.
 func IndexConst(n ir.Node) int64 {
-       if n.Op() != ir.OLITERAL {
-               return -1
-       }
-       if !n.Type().IsInteger() && n.Type().Kind() != types.TIDEAL {
-               return -1
-       }
-
-       v := toint(n.Val())
-       if v.Kind() != constant.Int || constant.Sign(v) < 0 {
-               return -1
-       }
-       if ir.ConstOverflow(v, types.Types[types.TINT]) {
-               return -2
-       }
-       return ir.IntVal(types.Types[types.TINT], v)
+       return ir.IntVal(types.Types[types.TINT], toint(n.Val()))
 }
 
 // callOrChan reports whether n is a call or channel operation.
index ec849e315400c38f0420c08f9e6dd3ac3e63f611..5d041b1939f983b3d3194b8565d40943954218d5 100644 (file)
@@ -1116,9 +1116,6 @@ func typecheckarraylit(elemType *types.Type, bound int64, elts []ir.Node, ctx st
                        elt := elt.(*ir.KeyExpr)
                        elt.Key = Expr(elt.Key)
                        key = IndexConst(elt.Key)
-                       if key < 0 {
-                               base.Fatalf("invalid index: %v", elt.Key)
-                       }
                        kv = elt
                        r = elt.Value
                }
index c4147b2e2e7802b23cfe65b78d934484ef7dd8ba..19ec8d30faffc544931b64eb4112e6f083c6772c 100644 (file)
@@ -527,9 +527,6 @@ func walkMakeSlice(n *ir.MakeExpr, init *ir.Nodes) ir.Node {
                // var arr [r]T
                // n = arr[:l]
                i := typecheck.IndexConst(r)
-               if i < 0 {
-                       base.Fatalf("walkExpr: invalid index %v", r)
-               }
 
                // cap is constrained to [0,2^31) or [0,2^63) depending on whether
                // we're in 32-bit or 64-bit systems. So it's safe to do:
index adc44ca49d07e5b0d93cc2c758b750e727f9344c..cfdc8becfe1154864461b269b0e95d5cb3c7750e 100644 (file)
@@ -199,9 +199,6 @@ func fixedlit(ctxt initContext, kind initKind, n *ir.CompLitExpr, var_ ir.Node,
                        if r.Op() == ir.OKEY {
                                kv := r.(*ir.KeyExpr)
                                k = typecheck.IndexConst(kv.Key)
-                               if k < 0 {
-                                       base.Fatalf("fixedlit: invalid index %v", kv.Key)
-                               }
                                r = kv.Value
                        }
                        a := ir.NewIndexExpr(base.Pos, var_, ir.NewInt(base.Pos, k))
@@ -372,9 +369,6 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes)
                if value.Op() == ir.OKEY {
                        kv := value.(*ir.KeyExpr)
                        index = typecheck.IndexConst(kv.Key)
-                       if index < 0 {
-                               base.Fatalf("slicelit: invalid index %v", kv.Key)
-                       }
                        value = kv.Value
                }
                a := ir.NewIndexExpr(base.Pos, vauto, ir.NewInt(base.Pos, index))