cmd/compile/internal/typecheck: simplify IndexConst

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>
This commit is contained in:
Cuong Manh Le 2024-09-08 21:33:55 +07:00 committed by Gopher Robot
parent 820329508a
commit 2d9255b0ea
5 changed files with 2 additions and 35 deletions

View 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)

View 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.

View 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
}

View 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:

View 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))