Skip to content

Commit

Permalink
go/types: add missing test for constant shifts
Browse files Browse the repository at this point in the history
Fixes golang/go#11325.

Change-Id: Ic302098fffd337fcfa31274319cdbd78907a6d5d
Reviewed-on: https://go-review.googlesource.com/11344
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
griesemer committed Jun 22, 2015
1 parent af81789 commit e9a746d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion go/types/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {

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

if x.mode == constant {
if y.mode == constant {
// rhs must be an integer value
if !y.isInteger() {
check.invalidOp(y.pos(), "shift count %s must be unsigned integer", y)
x.mode = invalid
return
}
// rhs must be within reasonable bounds
const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64
s, ok := exact.Uint64Val(y.val)
Expand Down
12 changes: 12 additions & 0 deletions go/types/testdata/shifts.src
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,15 @@ func issue5895() {
var x = 'a' << 1 // type of x must be rune
var _ rune = x
}

func issue11325() {
var _ = 0 >> 1.1 /* ERROR "must be unsigned integer" */ // example from issue 11325
_ = 0 >> 1.1 /* ERROR "must be unsigned integer" */
_ = 0 << 1.1 /* ERROR "must be unsigned integer" */
_ = 0 >> 1.
_ = 1 >> 1.1 /* ERROR "must be unsigned integer" */
_ = 1 >> 1.
_ = 1. >> 1
_ = 1. >> 1.
_ = 1.1 /* ERROR "must be integer" */ >> 1
}

0 comments on commit e9a746d

Please sign in to comment.