Skip to content

Commit ef4a17b

Browse files
committed
go/types: add missing test for constant shifts
Port of https://go-review.googlesource.com/11344 to std repo. Fixes #11325. Change-Id: I634beaf77cbaeb09de50aa1410e8c53fc37b19df Reviewed-on: https://go-review.googlesource.com/11317 Reviewed-by: Alan Donovan <[email protected]>
1 parent 989b372 commit ef4a17b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/go/types/expr.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
619619

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

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

src/go/types/testdata/shifts.src

+12
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)