]> Cypherpunks repositories - gostls13.git/commitdiff
go/types,types2: use allInteger to check type for shifted operand
authorzhouguangyuan <zhouguangyuan.xian@gmail.com>
Sun, 21 Nov 2021 13:06:27 +0000 (21:06 +0800)
committerEmmanuel Odeke <emmanuel@orijtech.com>
Mon, 22 Nov 2021 12:29:44 +0000 (12:29 +0000)
Fixes: #49705
Change-Id: I35a1c5f29b57f3facc5e89d33a8dec88e0ff4afa
Reviewed-on: https://go-review.googlesource.com/c/go/+/365895
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Emmanuel Odeke <emmanuel@orijtech.com>

src/cmd/compile/internal/types2/check_test.go
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue49705.go2 [new file with mode: 0644]
src/go/types/check_test.go
src/go/types/expr.go
src/go/types/testdata/fixedbugs/issue49705.go2 [new file with mode: 0644]

index a5ecdf8b8118ff430d72fee727d4ceff018c04ea..f13679d1e3685b8ddb9915df86ae5dfdc0f2796a 100644 (file)
@@ -99,6 +99,7 @@ func asGoVersion(s string) string {
 // TODO(gri) enable as soon as the unified build supports this.
 var excludedForUnifiedBuild = map[string]bool{
        "issue47818.go2": true,
+       "issue49705.go2": true,
 }
 
 func testFiles(t *testing.T, filenames []string, colDelta uint, manual bool) {
index b700716b0c6c7051f28cb3a8d7d9ae49500372fa..5961f32f37f58314b225d1809fdc2106c20806a9 100644 (file)
@@ -625,7 +625,7 @@ func (check *Checker) updateExprType(x syntax.Expr, typ Type, final bool) {
                // If x is the lhs of a shift, its final type must be integer.
                // We already know from the shift check that it is representable
                // as an integer if it is a constant.
-               if !isInteger(typ) {
+               if !allInteger(typ) {
                        check.errorf(x, invalidOp+"shifted operand %s (type %s) must be integer", x, typ)
                        return
                }
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49705.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue49705.go2
new file mode 100644 (file)
index 0000000..2b991b8
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 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 p
+
+import "constraints"
+
+func shl[I constraints.Integer](n int) I {
+       return 1 << n
+}
index 2f80d9b7b672c48971e4bca4261021077a35d8f9..a3be47e371c58e625cafe95c4ef82d6c0de11378 100644 (file)
@@ -205,6 +205,7 @@ func asGoVersion(s string) string {
 // TODO(gri) enable as soon as the unified build supports this.
 var excludedForUnifiedBuild = map[string]bool{
        "issue47818.go2": true,
+       "issue49705.go2": true,
 }
 
 func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, manual bool, imp Importer) {
index e93a2bc7c8b84a349fa9a3af36c7f8cd6f21b034..c49865aec6b1933beaa6d8252cb97b05d73e4aee 100644 (file)
@@ -565,7 +565,7 @@ func (check *Checker) updateExprType(x ast.Expr, typ Type, final bool) {
                // If x is the lhs of a shift, its final type must be integer.
                // We already know from the shift check that it is representable
                // as an integer if it is a constant.
-               if !isInteger(typ) {
+               if !allInteger(typ) {
                        check.invalidOp(x, _InvalidShiftOperand, "shifted operand %s (type %s) must be integer", x, typ)
                        return
                }
diff --git a/src/go/types/testdata/fixedbugs/issue49705.go2 b/src/go/types/testdata/fixedbugs/issue49705.go2
new file mode 100644 (file)
index 0000000..2b991b8
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2021 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 p
+
+import "constraints"
+
+func shl[I constraints.Integer](n int) I {
+       return 1 << n
+}