Skip to content

Commit

Permalink
go/types, types2: report error for invalid string(1 << s)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
griesemer committed Jan 18, 2022
1 parent fa4df65 commit 50869f3
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/cmd/compile/internal/types2/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`},
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/types2/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/testdata/check/shifts.src
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion src/go/types/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`},
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/testdata/check/shifts.src
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 8 additions & 0 deletions src/go/types/testdata/fixedbugs/issue45114.go
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 1 addition & 3 deletions test/fixedbugs/bug193.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 50869f3

Please sign in to comment.