Commit 9f7b1a82 authored by Robert Griesemer's avatar Robert Griesemer

go/types: untyped shifted constants must fit their expected int type

Fixes #22969.

Change-Id: Ie9d1748c36864a81a633f0016594912ac7dfc005
Reviewed-on: https://go-review.googlesource.com/c/144385Reviewed-by: 's avatarAlan Donovan <adonovan@google.com>
parent 6761b1eb
......@@ -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)
......
......@@ -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
}
\ No newline at end of file
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