mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
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 <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
fa4df6597e
commit
50869f377f
@ -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"`},
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
8
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45114.go
vendored
Normal file
8
src/cmd/compile/internal/types2/testdata/fixedbugs/issue45114.go
vendored
Normal file
@ -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)
|
@ -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"`},
|
||||
|
@ -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)
|
||||
|
2
src/go/types/testdata/check/shifts.src
vendored
2
src/go/types/testdata/check/shifts.src
vendored
@ -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)
|
||||
}
|
||||
|
||||
|
8
src/go/types/testdata/fixedbugs/issue45114.go
vendored
Normal file
8
src/go/types/testdata/fixedbugs/issue45114.go
vendored
Normal file
@ -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)
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user