]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: fix incorrect string(int) conversion (regression)
authorRobert Griesemer <gri@golang.org>
Mon, 23 Nov 2020 23:48:37 +0000 (15:48 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 24 Nov 2020 00:50:11 +0000 (00:50 +0000)
The bug was introduced by https://golang.org/cl/220844.

Fixes #42790.

Change-Id: I44d619a1a4d3f2aee1c5575d5cfddcc4ba10895f
Reviewed-on: https://go-review.googlesource.com/c/go/+/272666
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/go/types/conversions.go
test/fixedbugs/issue42790.go [new file with mode: 0644]

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):
diff --git a/test/fixedbugs/issue42790.go b/test/fixedbugs/issue42790.go
new file mode 100644 (file)
index 0000000..d83a022
--- /dev/null
@@ -0,0 +1,9 @@
+// compile
+
+// Copyright 2020 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
+
+const _ = -uint(len(string(1<<32)) - len("\uFFFD"))