FAQ
Reviewers: adonovan,

Message:
Hello adonovan (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go


Description:
go/types: implement constant string(x) conversions

Fixes issue 4982.

Please review this at https://codereview.appspot.com/7537043/

Affected files:
M src/pkg/go/types/conversions.go
M src/pkg/go/types/operand.go
M src/pkg/go/types/testdata/conversions.src


Index: src/pkg/go/types/conversions.go
===================================================================
--- a/src/pkg/go/types/conversions.go
+++ b/src/pkg/go/types/conversions.go
@@ -30,12 +30,31 @@

if x.mode == constant && isConstType(typ) {
// constant conversion
- // TODO(gri) implement this
+ typ := underlying(typ).(*Basic)
+ // For now just implement string(x) where x is an integer,
+ // as a temporary work-around for issue 4982, which is a
+ // common issue.
+ if typ.Kind == String {
+ switch {
+ case x.isInteger(check.ctxt):
+ codepoint, ok := x.val.(int64)
+ if !ok {
+ // absolute value too large (or unknown) for conversion;
+ // same as converting any other out-of-range value
+ codepoint = -1
+ }
+ x.val = string(codepoint)
+ case isString(x.typ):
+ // nothing to do
+ default:
+ goto ErrorMsg
+ }
+ }
+ // TODO(gri) verify the remaining conversions.
} else {
// non-constant conversion
if !x.isConvertible(check.ctxt, typ) {
- check.invalidOp(conv.Pos(), "cannot convert %s to %s", x, typ)
- goto Error
+ goto ErrorMsg
}
x.mode = value
}
@@ -45,8 +64,11 @@
x.typ = typ
return

+ErrorMsg:
+ check.invalidOp(conv.Pos(), "cannot convert %s to %s", x, typ)
Error:
x.mode = invalid
+ x.expr = conv
}

func (x *operand) isConvertible(ctxt *Context, T Type) bool {
Index: src/pkg/go/types/operand.go
===================================================================
--- a/src/pkg/go/types/operand.go
+++ b/src/pkg/go/types/operand.go
@@ -205,6 +205,7 @@
}

// isInteger reports whether x is a (typed or untyped) integer value.
+// TODO(gri) remove ctxt argument - it is not required for UntypedInt.
func (x *operand) isInteger(ctxt *Context) bool {
return x.mode == invalid ||
isInteger(x.typ) ||
Index: src/pkg/go/types/testdata/conversions.src
===================================================================
--- a/src/pkg/go/types/testdata/conversions.src
+++ b/src/pkg/go/types/testdata/conversions.src
@@ -8,11 +8,29 @@

// argument count
var (
- _v0 = int /* ERROR "one argument" */ ()
- _v1 = int /* ERROR "one argument" */ (1, 2)
+ _ = int /* ERROR "one argument" */ ()
+ _ = int /* ERROR "one argument" */ (1, 2)
)

-//
+func string_conversions() {
+ const A = string(65)
+ assert(A == "A")
+ const E = string(-1)
+ assert(E == "\uFFFD")
+ assert(E == string(1234567890))
+
+ type myint int
+ assert(A == string(myint(65)))
+
+ type mystring string
+ const _ mystring = mystring("foo")
+
+ const _ = string /* ERROR "cannot convert" */ (true)
+ const _ = string /* ERROR "cannot convert" */ (1.2)
+ const _ = string /* ERROR "cannot convert" */ (nil)
+}
+
+//
var (
- _v2 = int8(0)
+ _ = int8(0)
)
\ No newline at end of file


--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Adonovan at Mar 6, 2013 at 9:56 pm

    On 2013/03/06 21:49:04, gri wrote:
    Hello adonovan (cc: mailto:golang-dev@googlegroups.com),
    I'd like you to review this change to
    https://code.google.com/p/go
    LGTM

    https://codereview.appspot.com/7537043/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • R at Mar 6, 2013 at 10:02 pm
    LGTM


    https://codereview.appspot.com/7537043/diff/1/src/pkg/go/types/conversions.go
    File src/pkg/go/types/conversions.go (right):

    https://codereview.appspot.com/7537043/diff/1/src/pkg/go/types/conversions.go#newcode46
    src/pkg/go/types/conversions.go:46: x.val = string(codepoint)
    i doubt it matters in this context, but for the record string(codepoint)
    may return "\uFFFD" if codepoint is not a valid rune. given that point,
    it would be honest to make the -1 above utf8.RuneError

    https://codereview.appspot.com/7537043/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedMar 6, '13 at 9:49p
activeMar 6, '13 at 10:02p
posts3
users3
websitegolang.org

3 users in discussion

R: 1 post Adonovan: 1 post Gri: 1 post

People

Translate

site design / logo © 2021 Grokbase