Skip to content

Commit 50869f3

Browse files
committed
go/types, types2: report error for invalid string(1 << s)
For #45114. Fixes #45117. Change-Id: I71d6650ae2c4c06952fce19959120f15f13c08a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/379256 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent fa4df65 commit 50869f3

File tree

9 files changed

+23
-11
lines changed

9 files changed

+23
-11
lines changed

src/cmd/compile/internal/types2/api_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ func TestValuesInfo(t *testing.T) {
109109
{`package c5d; var _ = string(65)`, `65`, `untyped int`, `65`},
110110
{`package c5e; var _ = string('A')`, `'A'`, `untyped rune`, `65`},
111111
{`package c5f; type T string; var _ = T('A')`, `'A'`, `untyped rune`, `65`},
112-
{`package c5g; var s uint; var _ = string(1 << s)`, `1 << s`, `untyped int`, ``},
113112

114113
{`package d0; var _ = []byte("foo")`, `"foo"`, `string`, `"foo"`},
115114
{`package d1; var _ = []byte(string("foo"))`, `"foo"`, `string`, `"foo"`},

src/cmd/compile/internal/types2/conversions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ func (check *Checker) conversion(x *operand, T Type) {
9898
// - For conversions of untyped constants to non-constant types, also
9999
// use the default type (e.g., []byte("foo") should report string
100100
// not []byte as type for the constant "foo").
101-
// - For integer to string conversions, keep the argument type.
101+
// - For constant integer to string conversions, keep the argument type.
102102
// (See also the TODO below.)
103103
if x.typ == Typ[UntypedNil] {
104104
// ok
105105
} else if IsInterface(T) && !isTypeParam(T) || constArg && !isConstType(T) {
106106
final = Default(x.typ)
107-
} else if isInteger(x.typ) && allString(T) {
107+
} else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
108108
final = x.typ
109109
}
110110
check.updateExprType(x.expr, final, true)

src/cmd/compile/internal/types2/testdata/check/shifts.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func issue21727() {
381381
var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
382382
var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
383383
var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
384-
var _ = string(1 << s)
384+
var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
385385
var _ = string(1.0 /* ERROR "cannot convert" */ << s)
386386
}
387387

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
var s uint
8+
var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)

src/go/types/api_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ func TestValuesInfo(t *testing.T) {
127127
{`package c5d; var _ = string(65)`, `65`, `untyped int`, `65`},
128128
{`package c5e; var _ = string('A')`, `'A'`, `untyped rune`, `65`},
129129
{`package c5f; type T string; var _ = T('A')`, `'A'`, `untyped rune`, `65`},
130-
{`package c5g; var s uint; var _ = string(1 << s)`, `1 << s`, `untyped int`, ``},
131130

132131
{`package d0; var _ = []byte("foo")`, `"foo"`, `string`, `"foo"`},
133132
{`package d1; var _ = []byte(string("foo"))`, `"foo"`, `string`, `"foo"`},

src/go/types/conversions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ func (check *Checker) conversion(x *operand, T Type) {
9696
// use the default type (e.g., []byte("foo") should report string
9797
// not []byte as type for the constant "foo").
9898
// - Keep untyped nil for untyped nil arguments.
99-
// - For integer to string conversions, keep the argument type.
99+
// - For constant integer to string conversions, keep the argument type.
100100
// (See also the TODO below.)
101101
if IsInterface(T) && !isTypeParam(T) || constArg && !isConstType(T) || x.isNil() {
102102
final = Default(x.typ) // default type of untyped nil is untyped nil
103-
} else if isInteger(x.typ) && allString(T) {
103+
} else if x.mode == constant_ && isInteger(x.typ) && allString(T) {
104104
final = x.typ
105105
}
106106
check.updateExprType(x.expr, final, true)

src/go/types/testdata/check/shifts.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func issue21727() {
380380
var a = make([]int, 1<<s + 1.2 /* ERROR "truncated to int" */ )
381381
var _ = a[1<<s - 2.3 /* ERROR "truncated to int" */ ]
382382
var _ int = 1<<s + 3.4 /* ERROR "truncated to int" */
383-
var _ = string(1 << s)
383+
var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)
384384
var _ = string(1.0 /* ERROR "cannot convert" */ << s)
385385
}
386386

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
var s uint
8+
var _ = string(1 /* ERROR shifted operand 1 .* must be integer */ << s)

test/fixedbugs/bug193.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ func main() {
1111
ss := 1 << s
1212
y1 := float64(ss)
1313
y2 := float64(1 << s) // ERROR "shift"
14-
// see issues #45114, #45117
15-
// y3 := string(1 << s) // DISABLED "shift"
16-
y3 := 0
14+
y3 := string(1 << s) // ERROR "shift"
1715
_, _, _, _, _ = s, ss, y1, y2, y3
1816
}

0 commit comments

Comments
 (0)