]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: report error for invalid string(1 << s)
authorRobert Griesemer <gri@golang.org>
Tue, 18 Jan 2022 23:21:37 +0000 (15:21 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 18 Jan 2022 23:59:40 +0000 (23:59 +0000)
For #45114.
Fixes #45117.

Change-Id: I71d6650ae2c4c06952fce19959120f15f13c08a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/379256
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/api_test.go
src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/testdata/check/shifts.src
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45114.go [new file with mode: 0644]
src/go/types/api_test.go
src/go/types/conversions.go
src/go/types/testdata/check/shifts.src
src/go/types/testdata/fixedbugs/issue45114.go [new file with mode: 0644]
test/fixedbugs/bug193.go

index 28c1f97e873663a71066ac327b0450715ffe5b8f..2493bfb200e706fd4fa2901b12558766aeca5a3a 100644 (file)
@@ -109,7 +109,6 @@ func TestValuesInfo(t *testing.T) {
                {`package c5d; var _ = string(65)`, `65`, `untyped int`, `65`},
                {`package c5e; var _ = string('A')`, `'A'`, `untyped rune`, `65`},
                {`package c5f; type T string; var _ = T('A')`, `'A'`, `untyped rune`, `65`},
-               {`package c5g; var s uint; var _ = string(1 << s)`, `1 << s`, `untyped int`, ``},
 
                {`package d0; var _ = []byte("foo")`, `"foo"`, `string`, `"foo"`},
                {`package d1; var _ = []byte(string("foo"))`, `"foo"`, `string`, `"foo"`},
index 253868cf931b20adbd7861ad21b094c35a0c245c..7fe1d5056b0241b0087e1ea64c9edda37f9a5fa9 100644 (file)
@@ -98,13 +98,13 @@ func (check *Checker) conversion(x *operand, T Type) {
                // - For conversions of untyped constants to non-constant types, also
                //   use the default type (e.g., []byte("foo") should report string
                //   not []byte as type for the constant "foo").
-               // - For integer to string conversions, keep the argument type.
+               // - For constant integer to string conversions, keep the argument type.
                //   (See also the TODO below.)
                if x.typ == Typ[UntypedNil] {
                        // ok
                } else if IsInterface(T) && !isTypeParam(T) || constArg && !isConstType(T) {
                        final = Default(x.typ)
-               } else if isInteger(x.typ) && allString(T) {
+               } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
                        final = x.typ
                }
                check.updateExprType(x.expr, final, true)
index 60db731cf4e03d3d2ee9c02bc1fb623ab89cb6c4..37bc84c0f6e0cd6c38d82584febfd0ff7e0fbe8e 100644 (file)
@@ -381,7 +381,7 @@ func issue21727() {
        var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
        var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
        var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
-       var _ = string(1 << s)
+       var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
        var _ = string(1.0 /* ERROR "cannot convert" */ << s)
 }
 
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45114.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue45114.go
new file mode 100644 (file)
index 0000000..0093660
--- /dev/null
@@ -0,0 +1,8 @@
+// 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 p
+
+var s uint
+var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
index 6a1bf269845ad64e6a16a0df588db33ce18f2b0a..8c80494de73e4e790c8d9f7bc1fd7bb87b53fcf1 100644 (file)
@@ -127,7 +127,6 @@ func TestValuesInfo(t *testing.T) {
                {`package c5d; var _ = string(65)`, `65`, `untyped int`, `65`},
                {`package c5e; var _ = string('A')`, `'A'`, `untyped rune`, `65`},
                {`package c5f; type T string; var _ = T('A')`, `'A'`, `untyped rune`, `65`},
-               {`package c5g; var s uint; var _ = string(1 << s)`, `1 << s`, `untyped int`, ``},
 
                {`package d0; var _ = []byte("foo")`, `"foo"`, `string`, `"foo"`},
                {`package d1; var _ = []byte(string("foo"))`, `"foo"`, `string`, `"foo"`},
index a5b359e5394c39148002209533df65e8cbb1bae8..84741359c0ffe762a1f9bc46a69519a944bbacfb 100644 (file)
@@ -96,11 +96,11 @@ func (check *Checker) conversion(x *operand, T Type) {
                //   use the default type (e.g., []byte("foo") should report string
                //   not []byte as type for the constant "foo").
                // - Keep untyped nil for untyped nil arguments.
-               // - For integer to string conversions, keep the argument type.
+               // - For constant integer to string conversions, keep the argument type.
                //   (See also the TODO below.)
                if IsInterface(T) && !isTypeParam(T) || constArg && !isConstType(T) || x.isNil() {
                        final = Default(x.typ) // default type of untyped nil is untyped nil
-               } else if isInteger(x.typ) && allString(T) {
+               } else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
                        final = x.typ
                }
                check.updateExprType(x.expr, final, true)
index 4d3c59a50fd9bd59f7a056da8fd1ba0b09e04d99..16a67aee631d5d0afaed62fb0c2b6905507875a9 100644 (file)
@@ -380,7 +380,7 @@ func issue21727() {
        var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
        var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
        var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
-       var _ = string(1 << s)
+       var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
        var _ = string(1.0 /* ERROR "cannot convert" */ << s)
 }
 
diff --git a/src/go/types/testdata/fixedbugs/issue45114.go b/src/go/types/testdata/fixedbugs/issue45114.go
new file mode 100644 (file)
index 0000000..0093660
--- /dev/null
@@ -0,0 +1,8 @@
+// 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 p
+
+var s uint
+var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
index 36073220f9b2c404a8c3472c2e77f08ea33b53f4..64e06da897516c359a0493c725e5fd926eac046b 100644 (file)
@@ -11,8 +11,6 @@ func main() {
        ss := 1 << s
        y1 := float64(ss)
        y2 := float64(1 << s) // ERROR "shift"
-       // see issues #45114, #45117
-       // y3 := string(1 << s)  // DISABLED "shift"
-       y3 := 0
+       y3 := string(1 << s)  // ERROR "shift"
        _, _, _, _, _ = s, ss, y1, y2, y3
 }