Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(811)

Unified Diff: src/pkg/go/types/conversions.go

Issue 7537043: code review 7537043: go/types: implement constant string(x) conversions (Closed)
Patch Set: diff -r a688fd4f607d https://code.google.com/p/go Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | src/pkg/go/types/operand.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/go/types/conversions.go
===================================================================
--- a/src/pkg/go/types/conversions.go
+++ b/src/pkg/go/types/conversions.go
@@ -30,12 +30,32 @@
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 - let
+ // string(codepoint) do the work
+ 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 +65,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 {
« no previous file with comments | « no previous file | src/pkg/go/types/operand.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b