Skip to content

Commit

Permalink
cmd/compile: allow huge rsh in constants arithmetic
Browse files Browse the repository at this point in the history
Currently an expression like

var v = 0 >> 1000

is rejected by gc with a "stupid shift" error, while gotype
compiles it successfully.

As suggested by gri on the issue tracker, allow an rsh right
operand to be any valid uint value.

Fixes golang#11328

Change-Id: I6ccb3b7f842338d91fd26ae37dd4fa279d7fc440
Reviewed-on: https://go-review.googlesource.com/13777
Reviewed-by: Robert Griesemer <[email protected]>
  • Loading branch information
ALTree authored and griesemer committed Aug 21, 2015
1 parent 548041e commit 85de30e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/gc/mparith2.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func mprshfixfix(a, b *Mpint) {
}

s := Mpgetfix(b)
if s < 0 || s >= Mpprec {
if s < 0 {
Yyerror("stupid shift: %d", s)
if a.Val.Sign() < 0 {
Mpmovecfix(a, -1)
Expand Down
5 changes: 5 additions & 0 deletions test/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const (
c3div2 = 3 / 2
c1e3 = 1e3

rsh1 = 1e100 >> 1000
rsh2 = 1e302 >> 1000

ctrue = true
cfalse = !ctrue
)
Expand Down Expand Up @@ -48,6 +51,8 @@ func ints() {
assert(c3div2 == 1, "3/2")
assert(c1e3 == 1000, "c1e3 int")
assert(c1e3 == 1e3, "c1e3 float")
assert(rsh1 == 0, "rsh1")
assert(rsh2 == 9, "rsh2")

// verify that all (in range) are assignable as ints
var i int
Expand Down

0 comments on commit 85de30e

Please sign in to comment.