From: Cuong Manh Le Date: Sun, 8 Sep 2024 14:45:36 +0000 (+0700) Subject: cmd/compile/internal/typecheck: simplify tcSlice X-Git-Tag: go1.24rc1~925 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=d7521704cebccd36c16df6f4ac229e3a757ed055;p=gostls13.git cmd/compile/internal/typecheck: simplify tcSlice 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 Reviewed-by: Keith Randall Reviewed-by: Keith Randall Auto-Submit: Cuong Manh Le Reviewed-by: Tim King --- diff --git a/src/cmd/compile/internal/typecheck/expr.go b/src/cmd/compile/internal/typecheck/expr.go index eb92797259..2eec8d41ad 100644 --- a/src/cmd/compile/internal/typecheck/expr.go +++ b/src/cmd/compile/internal/typecheck/expr.go @@ -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 } diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index 5d041b1939..cb48bfd7e3 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -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 }