Commit ef4a17bc authored by Robert Griesemer's avatar Robert Griesemer

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/11317Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 989b372a
...@@ -619,7 +619,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -619,7 +619,7 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
// The lhs must be of integer type or be representable // The lhs must be of integer type or be representable
// as an integer; otherwise the shift has no chance. // 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) check.invalidOp(x.pos(), "shifted operand %s must be integer", x)
x.mode = invalid x.mode = invalid
return return
...@@ -645,6 +645,12 @@ func (check *Checker) shift(x, y *operand, op token.Token) { ...@@ -645,6 +645,12 @@ func (check *Checker) shift(x, y *operand, op token.Token) {
if x.mode == constant { if x.mode == constant {
if y.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 // rhs must be within reasonable bounds
const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64 const stupidShift = 1023 - 1 + 52 // so we can express smallestFloat64
s, ok := exact.Uint64Val(y.val) s, ok := exact.Uint64Val(y.val)
......
...@@ -319,3 +319,15 @@ func issue5895() { ...@@ -319,3 +319,15 @@ func issue5895() {
var x = 'a' << 1 // type of x must be rune var x = 'a' << 1 // type of x must be rune
var _ rune = x 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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment