Skip to content

Commit e9a746d

Browse files
committed
go/types: add missing test for constant shifts
Fixes golang/go#11325. Change-Id: Ic302098fffd337fcfa31274319cdbd78907a6d5d Reviewed-on: https://go-review.googlesource.com/11344 Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent af81789 commit e9a746d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

go/types/expr.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
620620

621621
// The lhs must be of integer type or be representable
622622
// as an integer; otherwise the shift has no chance.
623-
if !isInteger(x.typ) && (!untypedx || !representableConst(x.val, nil, UntypedInt, nil)) {
623+
if !x.isInteger() {
624624
check.invalidOp(x.pos(), "shifted operand %s must be integer", x)
625625
x.mode = invalid
626626
return
@@ -646,6 +646,12 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
646646

647647
if x.mode == constant {
648648
if y.mode == constant {
649+
// rhs must be an integer value
650+
if !y.isInteger() {
651+
check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y)
652+
x.mode = invalid
653+
return
654+
}
649655
// rhs must be within reasonable bounds
650656
const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64
651657
s, ok := exact.Uint64Val(y.val)

go/types/testdata/shifts.src

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,15 @@ func issue5895() {
319319
var x = 'a' << 1 // type of x must be rune
320320
var _ rune = x
321321
}
322+
323+
func issue11325() {
324+
var _ = 0 >> 1.1 /* ERROR "must be unsigned integer" */ // example from issue 11325
325+
_ = 0 >> 1.1 /* ERROR "must be unsigned integer" */
326+
_ = 0 << 1.1 /* ERROR "must be unsigned integer" */
327+
_ = 0 >> 1.
328+
_ = 1 >> 1.1 /* ERROR "must be unsigned integer" */
329+
_ = 1 >> 1.
330+
_ = 1. >> 1
331+
_ = 1. >> 1.
332+
_ = 1.1 /* ERROR "must be integer" */ >> 1
333+
}

0 commit comments

Comments
 (0)