]> Cypherpunks repositories - gostls13.git/commitdiff
bug197
authorRuss Cox <rsc@golang.org>
Mon, 24 Aug 2009 20:41:47 +0000 (13:41 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 24 Aug 2009 20:41:47 +0000 (13:41 -0700)
R=ken
OCL=33765
CL=33765

src/cmd/gc/const.c
src/cmd/gc/go.h
src/cmd/gc/print.c
src/cmd/gc/typecheck.c
test/fixedbugs/bug197.go [new file with mode: 0644]

index d774773a4378473277fbf051cef772dddc5754f3..1433eb9ba5a9c061a732c00f40031b1feba44882 100644 (file)
@@ -77,6 +77,8 @@ convlit1(Node **np, Type *t, int explicit)
                return;
        if(eqtype(t, n->type))
                return;
+       if(!explicit && n->type->etype != TIDEAL && n->type != idealstring && n->type->etype != TNIL)
+               return;
 
 //dump("convlit1", n);
        if(n->op == OLITERAL) {
index 09b8da76956b228fa5e9839dde4068213ebf1129..f7d6f83c97b1e7644725e6d19a27cfafbb7d4e36 100644 (file)
@@ -330,7 +330,7 @@ enum
        OCMPIFACE, OCMPSTR,
        OCOMPLIT, OMAPLIT, OSTRUCTLIT, OARRAYLIT,
        OCOMPSLICE, OCOMPMAP,
-       OCONV, OCONVNOP, OCONVA2S, OCONVIFACE, OCONVSLICE,
+       OCONV, OCONVNOP, OCONVIFACE, OCONVSLICE,
        ODCL, ODCLFUNC, ODCLFIELD, ODCLCONST, ODCLTYPE,
        ODOT, ODOTPTR, ODOTMETH, ODOTINTER, OXDOT,
        ODOTTYPE,
index bb79037258bcbf1662c42e0329aef170dcea058e..70d80c5e6a0cad7640d9298976ef03b026c2c28c 100644 (file)
@@ -258,6 +258,9 @@ exprfmt(Fmt *f, Node *n, int prec)
                break;
 
        case OCONV:
+       case OCONVNOP:
+       case OCONVSLICE:
+       case OCONVIFACE:
                fmtprint(f, "%T(", n->type);
                exprfmt(f, n->left, 0);
                fmtprint(f, ")");
index bca203ee643798290713125673c0655a8896edcd..43351796b7b2eee0876a661b23d6731693e3cb55 100644 (file)
@@ -87,6 +87,8 @@ reswitch:
                ok |= Erv;
                if(n->iota && !(top & Eiota))
                        yyerror("use of iota not in constant initializer");
+               if(n->val.ctype == CTSTR)
+                       n->type = idealstring;
                goto ret;
 
        case ONONAME:
@@ -1268,7 +1270,9 @@ typecheckconv(Node *nconv, Node *n, Type *t, int explicit)
        if(n->type == T)
                return n;
 
-       if(cvttype(t, n->type) && n->op == OLITERAL) {
+       if(n->op == OLITERAL)
+       if(explicit || n->type->etype == TIDEAL || n->type == idealstring || n->type->etype == TNIL)
+       if(cvttype(t, n->type)) {
                // can convert literal in place
                // TODO(rsc) is this needed?
                n1 = nod(OXXX, N, N);
@@ -1286,6 +1290,10 @@ typecheckconv(Node *nconv, Node *n, Type *t, int explicit)
                return n;
 
        case 0:
+               if(nconv) {
+                       nconv->op = OCONVNOP;
+                       return nconv;
+               }
                return n;
        }
 
diff --git a/test/fixedbugs/bug197.go b/test/fixedbugs/bug197.go
new file mode 100644 (file)
index 0000000..501b94c
--- /dev/null
@@ -0,0 +1,33 @@
+// errchk $G $D/$F.go
+
+// Copyright 2009 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 main
+
+type T int
+type U int
+
+var x int
+
+var t T = int(0)       // ERROR "cannot use"
+var t1 T = int(x)      // ERROR "cannot use"
+var u U = int(0)       // ERROR "cannot use"
+var u1 U = int(x)      // ERROR "cannot use"
+
+type S string
+var s S
+
+var s1 = s + "hello"
+var s2 = "hello" + s
+var s3 = s + string("hello")   // ERROR "invalid operation"
+var s4 = string("hello") + s   // ERROR "invalid operation"
+
+var r string
+
+var r1 = r + "hello"
+var r2 = "hello" + r
+var r3 = r + string("hello")
+var r4 = string("hello") + r
+