From: Robert Griesemer Date: Tue, 18 Jan 2022 23:21:37 +0000 (-0800) Subject: go/types, types2: report error for invalid string(1 << s) X-Git-Tag: go1.18beta2~77 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=50869f377fd72a921d27e3522a05604b7753b3ab;p=gostls13.git 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 Reviewed-by: Robert Findley --- diff --git a/src/cmd/compile/internal/types2/api_test.go b/src/cmd/compile/internal/types2/api_test.go index 28c1f97e87..2493bfb200 100644 --- a/src/cmd/compile/internal/types2/api_test.go +++ b/src/cmd/compile/internal/types2/api_test.go @@ -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"`}, diff --git a/src/cmd/compile/internal/types2/conversions.go b/src/cmd/compile/internal/types2/conversions.go index 253868cf93..7fe1d5056b 100644 --- a/src/cmd/compile/internal/types2/conversions.go +++ b/src/cmd/compile/internal/types2/conversions.go @@ -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) diff --git a/src/cmd/compile/internal/types2/testdata/check/shifts.src b/src/cmd/compile/internal/types2/testdata/check/shifts.src index 60db731cf4..37bc84c0f6 100644 --- a/src/cmd/compile/internal/types2/testdata/check/shifts.src +++ b/src/cmd/compile/internal/types2/testdata/check/shifts.src @@ -381,7 +381,7 @@ func issue21727() { var a = make([]int, 1<