]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: use correct shift value when typechecking constant shift
authorRobert Griesemer <gri@golang.org>
Wed, 26 Oct 2022 04:07:11 +0000 (21:07 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 26 Oct 2022 14:34:44 +0000 (14:34 +0000)
Fixes #56425.

Change-Id: Ieae3fdb5326d4b6f6ec1cdcd579051559e34b35b
Reviewed-on: https://go-review.googlesource.com/c/go/+/445515
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/expr.go
src/go/types/expr.go
src/internal/types/testdata/fixedbugs/issue56425.go [new file with mode: 0644]

index 17e120f94883fa317e10bac63e9e17f994133a37..40d6e5da695228dd7fbc613624bae6e202cc2eb9 100644 (file)
@@ -943,9 +943,10 @@ func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
 
        // Check that constants are representable by uint, but do not convert them
        // (see also issue #47243).
+       var yval constant.Value
        if y.mode == constant_ {
                // Provide a good error message for negative shift counts.
-               yval := constant.ToInt(y.val) // consider -1, 1.0, but not -1.1
+               yval = constant.ToInt(y.val) // consider -1, 1.0, but not -1.1
                if yval.Kind() == constant.Int && constant.Sign(yval) < 0 {
                        check.errorf(y, InvalidShiftCount, invalidOp+"negative shift count %s", y)
                        x.mode = invalid
@@ -998,7 +999,7 @@ func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
                        }
                        // rhs must be within reasonable bounds in constant shifts
                        const shiftBound = 1023 - 1 + 52 // so we can express smallestFloat64 (see issue #44057)
-                       s, ok := constant.Uint64Val(y.val)
+                       s, ok := constant.Uint64Val(yval)
                        if !ok || s > shiftBound {
                                check.errorf(y, InvalidShiftCount, invalidOp+"invalid shift count %s", y)
                                x.mode = invalid
index f11632fd6b5aa38d1988ade22990dbc61cc11576..da9cd67826f6db59326833e157e68229ec87dd80 100644 (file)
@@ -920,9 +920,10 @@ func (check *Checker) shift(x, y *operand, e ast.Expr, op token.Token) {
 
        // Check that constants are representable by uint, but do not convert them
        // (see also issue #47243).
+       var yval constant.Value
        if y.mode == constant_ {
                // Provide a good error message for negative shift counts.
-               yval := constant.ToInt(y.val) // consider -1, 1.0, but not -1.1
+               yval = constant.ToInt(y.val) // consider -1, 1.0, but not -1.1
                if yval.Kind() == constant.Int && constant.Sign(yval) < 0 {
                        check.errorf(y, InvalidShiftCount, invalidOp+"negative shift count %s", y)
                        x.mode = invalid
@@ -975,7 +976,7 @@ func (check *Checker) shift(x, y *operand, e ast.Expr, op token.Token) {
                        }
                        // rhs must be within reasonable bounds in constant shifts
                        const shiftBound = 1023 - 1 + 52 // so we can express smallestFloat64 (see issue #44057)
-                       s, ok := constant.Uint64Val(y.val)
+                       s, ok := constant.Uint64Val(yval)
                        if !ok || s > shiftBound {
                                check.errorf(y, InvalidShiftCount, invalidOp+"invalid shift count %s", y)
                                x.mode = invalid
diff --git a/src/internal/types/testdata/fixedbugs/issue56425.go b/src/internal/types/testdata/fixedbugs/issue56425.go
new file mode 100644 (file)
index 0000000..d85733f
--- /dev/null
@@ -0,0 +1,8 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+const s float32 = 0
+var _ = 0 << s