]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] go/types, cmd/compile/internal/types2: fix incorrect string(int...
authorRobert Griesemer <gri@golang.org>
Tue, 24 Nov 2020 01:15:00 +0000 (17:15 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 24 Nov 2020 01:34:16 +0000 (01:34 +0000)
This is a 1:1 port of the go/types changes in
https://golang.org/cl/272666 (master branch).

Updates #42790.

Change-Id: I5da372961df48129b25777ed705b84d7201393ec
Reviewed-on: https://go-review.googlesource.com/c/go/+/272669
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

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

index 9ff548593f5733f0792f93aaf88ee138e1edcc6f..0f6a990935345ec590dd3c413da61f468302a259 100644 (file)
@@ -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):
index 0955391d7bd89e5b2a399eb051dc2fa0bd3c3b02..1cab1cc70f0e8bfc4206422136b1f5cb65b9f66a 100644 (file)
@@ -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):