src/go/types/api_test.go | 2 +- src/go/types/expr.go | 38 +++++++++++++++++++------------------- src/go/types/testdata/fixedbugs/issue52031.go | 33 +++++++++++++++++++++++++++++++++ diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index 6b05d66b10178af010fe1ef2752e5f10ecc43a33..78977ff2f05530344ff74265da12b567671fb3b8 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -330,7 +330,7 @@ }, // issue 47243 {`package issue47243_a; var x int32; var _ = x << 3`, `3`, `untyped int`}, - {`package issue47243_b; var x int32; var _ = x << 3.`, `3.`, `uint`}, // issue 47410: should be untyped float + {`package issue47243_b; var x int32; var _ = x << 3.`, `3.`, `untyped float`}, {`package issue47243_c; var x int32; var _ = 1 << x`, `1 << x`, `int`}, {`package issue47243_d; var x int32; var _ = 1 << x`, `1`, `int`}, {`package issue47243_e; var x int32; var _ = 1 << 2`, `1`, `untyped int`}, diff --git a/src/go/types/expr.go b/src/go/types/expr.go index e24bd60dc391a300885920770552ddcfb784db0a..23a735ce693b1f25c6f5d282951b4ee0329c36c1 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -934,28 +934,28 @@ x.mode = invalid return } } - } - - // Check that RHS is otherwise at least of integer type. - switch { - case allInteger(y.typ): - if !allUnsigned(y.typ) && !check.allowVersion(check.pkg, 1, 13) { - check.invalidOp(y, _InvalidShiftCount, "signed shift count %s requires go1.13 or later", y) - x.mode = invalid - return - } - case isUntyped(y.typ): - // This is incorrect, but preserves pre-existing behavior. - // See also bug #47410. - check.convertUntyped(y, Typ[Uint]) - if y.mode == invalid { + } else { + // Check that RHS is otherwise at least of integer type. + switch { + case allInteger(y.typ): + if !allUnsigned(y.typ) && !check.allowVersion(check.pkg, 1, 13) { + check.invalidOp(y, _InvalidShiftCount, "signed shift count %s requires go1.13 or later", y) + x.mode = invalid + return + } + case isUntyped(y.typ): + // This is incorrect, but preserves pre-existing behavior. + // See also bug #47410. + check.convertUntyped(y, Typ[Uint]) + if y.mode == invalid { + x.mode = invalid + return + } + default: + check.invalidOp(y, _InvalidShiftCount, "shift count %s must be integer", y) x.mode = invalid return } - default: - check.invalidOp(y, _InvalidShiftCount, "shift count %s must be integer", y) - x.mode = invalid - return } if x.mode == constant_ { diff --git a/src/go/types/testdata/fixedbugs/issue52031.go b/src/go/types/testdata/fixedbugs/issue52031.go new file mode 100644 index 0000000000000000000000000000000000000000..dff2abd667df53b9275a874b48fcc6058d2e2b49 --- /dev/null +++ b/src/go/types/testdata/fixedbugs/issue52031.go @@ -0,0 +1,33 @@ +// -lang=go1.12 + +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package go1_12 + +type resultFlags uint + +// Example from #52031. +// +// The following shifts should not produce errors on Go < 1.13, as their +// untyped constant operands are representable by type uint. +const ( + _ resultFlags = (1 << iota) / 2 + + reportEqual + reportUnequal + reportByIgnore + reportByMethod + reportByFunc + reportByCycle +) + +// Invalid cases. +var x int = 1 +var _ = (8 << x /* ERROR "signed shift count .* requires go1.13 or later" */) + +const _ = (1 << 1.2 /* ERROR "truncated to uint" */) + +var y float64 +var _ = (1 << y /* ERROR "must be integer" */)