diff --git a/src/cmd/compile/internal/types2/conversions.go b/src/cmd/compile/internal/types2/conversions.go index 9ff548593f5..0f6a9909353 100644 --- a/src/cmd/compile/internal/types2/conversions.go +++ b/src/cmd/compile/internal/types2/conversions.go @@ -7,7 +7,10 @@ package types2 -import "go/constant" +import ( + "go/constant" + "unicode" +) // Conversion type-checks the conversion T(x). // The result is in x. @@ -22,14 +25,11 @@ func (check *Checker) conversion(x *operand, T Type) { case representableConst(x.val, check, t, &x.val): ok = true case isInteger(x.typ) && isString(t): - codepoint := int64(-1) - if i, ok := constant.Int64Val(x.val); ok { - codepoint = i + codepoint := unicode.ReplacementChar + if i, ok := constant.Uint64Val(x.val); ok && i <= unicode.MaxRune { + codepoint = rune(i) } - // If codepoint < 0 the absolute value is too large (or unknown) for - // conversion. This is the same as converting any other out-of-range - // value - let string(codepoint) do the work. - x.val = constant.MakeString(string(rune(codepoint))) + x.val = constant.MakeString(string(codepoint)) ok = true } case x.convertibleTo(check, T): diff --git a/src/go/types/conversions.go b/src/go/types/conversions.go index 0955391d7bd..1cab1cc70f0 100644 --- a/src/go/types/conversions.go +++ b/src/go/types/conversions.go @@ -6,7 +6,10 @@ package types -import "go/constant" +import ( + "go/constant" + "unicode" +) // Conversion type-checks the conversion T(x). // The result is in x. @@ -21,14 +24,11 @@ func (check *Checker) conversion(x *operand, T Type) { case representableConst(x.val, check, t, &x.val): ok = true case isInteger(x.typ) && isString(t): - codepoint := int64(-1) - if i, ok := constant.Int64Val(x.val); ok { - codepoint = i + codepoint := unicode.ReplacementChar + if i, ok := constant.Uint64Val(x.val); ok && i <= unicode.MaxRune { + codepoint = rune(i) } - // If codepoint < 0 the absolute value is too large (or unknown) for - // conversion. This is the same as converting any other out-of-range - // value - let string(codepoint) do the work. - x.val = constant.MakeString(string(rune(codepoint))) + x.val = constant.MakeString(string(codepoint)) ok = true } case x.convertibleTo(check, T):