From: Robert Griesemer Date: Wed, 24 Oct 2018 17:23:43 +0000 (-0700) Subject: go/types: untyped shifted constants must fit their expected int type X-Git-Tag: go1.12beta1~652 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9f7b1a8259fb5be07e41b5f74445f16a6a23e963;p=gostls13.git go/types: untyped shifted constants must fit their expected int type Fixes #22969. Change-Id: Ie9d1748c36864a81a633f0016594912ac7dfc005 Reviewed-on: https://go-review.googlesource.com/c/144385 Reviewed-by: Alan Donovan --- diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 35e9b36f31..0dc007069f 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -461,7 +461,11 @@ func (check *Checker) updateExprType(x ast.Expr, typ Type, final bool) { check.invalidOp(x.Pos(), "shifted operand %s (type %s) must be integer", x, typ) return } - } else if old.val != nil { + // Even if we have an integer, if the value is a constant we + // still must check that it is representable as the specific + // int type requested (was issue #22969). Fall through here. + } + if old.val != nil { // If x is a constant, it must be representable as a value of typ. c := operand{old.mode, x, old.typ, old.val, 0} check.convertUntyped(&c, typ) diff --git a/src/go/types/testdata/shifts.src b/src/go/types/testdata/shifts.src index ca288290d6..52e340ec65 100644 --- a/src/go/types/testdata/shifts.src +++ b/src/go/types/testdata/shifts.src @@ -354,3 +354,15 @@ func issue21727() { var _ = string(1 << s) var _ = string(1.0 /* ERROR "cannot convert" */ << s) } + +func issue22969() { + var s uint + var a []byte + _ = a[0xffffffffffffffff /* ERROR "overflows int" */ <