aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/noder/reader.go
diff options
context:
space:
mode:
authorCuong Manh Le <cuong.manhle.vn@gmail.com>2023-10-05 11:11:51 +0700
committerGopher Robot <gobot@golang.org>2023-10-09 18:09:34 +0000
commit778880b00888066212864f95877c0febbebf7e69 (patch)
treeb1e315e14f31cc4f8cbacacc3c4d35fb14b0d04d /src/cmd/compile/internal/noder/reader.go
parentede27fb4aca9ab103413aff71b254f10db2b302a (diff)
downloadgo-778880b00888066212864f95877c0febbebf7e69.tar.xz
cmd/compile: fix typecheck range over negative integer
Before range over integer, types2 leaves constant expression in RHS of non-constant shift untyped, so idealType do the validation to ensure that constant value must be an int >= 0. With range over int, the range expression can also be left untyped, and can be an negative integer, causing the validation false. Fixing this by relaxing the validation in idealType, and moving the check to Unified IR reader. Fixes #63378 Change-Id: I43042536c09afd98d52c5981adff5dbc5e7d882a Reviewed-on: https://go-review.googlesource.com/c/go/+/532835 Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Keith Randall <khr@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/noder/reader.go')
-rw-r--r--src/cmd/compile/internal/noder/reader.go7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go
index 1c0d0a9acc..3cd7b7c683 100644
--- a/src/cmd/compile/internal/noder/reader.go
+++ b/src/cmd/compile/internal/noder/reader.go
@@ -2233,6 +2233,13 @@ func (r *reader) expr() (res ir.Node) {
switch op {
case ir.OANDAND, ir.OOROR:
return typecheck.Expr(ir.NewLogicalExpr(pos, op, x, y))
+ case ir.OLSH, ir.ORSH:
+ // Untyped rhs of non-constant shift, e.g. x << 1.0.
+ // If we have a constant value, it must be an int >= 0.
+ if ir.IsConstNode(y) {
+ val := constant.ToInt(y.Val())
+ assert(val.Kind() == constant.Int && constant.Sign(val) >= 0)
+ }
}
return typecheck.Expr(ir.NewBinaryExpr(pos, op, x, y))