1
0
mirror of https://github.com/golang/go synced 2024-11-23 00:40:08 -07:00

go/types, types2: use correct shift value when typechecking constant shift

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>
This commit is contained in:
Robert Griesemer 2022-10-25 21:07:11 -07:00 committed by Gopher Robot
parent 49abdbccde
commit 51af904456
3 changed files with 14 additions and 4 deletions

View 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

View 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

View File

@ -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