1
0
mirror of https://github.com/golang/go synced 2024-11-07 14:36:17 -07:00

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 <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2018-10-24 10:23:43 -07:00
parent 6761b1eb1b
commit 9f7b1a8259
2 changed files with 17 additions and 1 deletions

View File

@ -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)

View File

@ -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" */ <<s] // example from issue 22969
_ = make([]int, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
_ = make([]int, 0, 0xffffffffffffffff /* ERROR "overflows int" */ << s)
var _ byte = 0x100 /* ERROR "overflows byte" */ << s
var _ int8 = 0xff /* ERROR "overflows int8" */ << s
var _ int16 = 0xffff /* ERROR "overflows int16" */ << s
var _ int32 = 0x80000000 /* ERROR "overflows int32" */ << s
}