]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: add missing conversion from bool to interface in switches.
authorDaniel Morsing <daniel.morsing@gmail.com>
Mon, 17 Sep 2012 19:29:10 +0000 (21:29 +0200)
committerDaniel Morsing <daniel.morsing@gmail.com>
Mon, 17 Sep 2012 19:29:10 +0000 (21:29 +0200)
In switches without an expression, the compiler would not convert the implicit true to an interface, causing codegen errors.

Fixes #3980.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6497147

src/cmd/gc/swt.c
test/switch.go

index aff1b5ea878f9c6cec105581bb86c69925df5563..860fed84a9626c2a45159018f26a967fd2914455 100644 (file)
@@ -442,6 +442,10 @@ exprbsw(Case *c0, int ncase, int arg)
                        n = c0->node;
                        lno = setlineno(n);
 
+                       if(assignop(n->left->type, exprname->type, nil) == OCONVIFACE ||
+                          assignop(exprname->type, n->left->type, nil) == OCONVIFACE)
+                               goto snorm;
+
                        switch(arg) {
                        case Strue:
                                a = nod(OIF, N, N);
@@ -457,6 +461,7 @@ exprbsw(Case *c0, int ncase, int arg)
                                break;
 
                        default:
+                       snorm:
                                a = nod(OIF, N, N);
                                a->ntest = nod(OEQ, exprname, n->left); // if name == val
                                typecheck(&a->ntest, Erv);
@@ -520,6 +525,8 @@ exprswitch(Node *sw)
                exprname = temp(sw->ntest->type);
                cas = list1(nod(OAS, exprname, sw->ntest));
                typechecklist(cas, Etop);
+       } else {
+               exprname = nodbool(arg == Strue);
        }
 
        c0 = mkcaselist(sw, arg);
index a4242f25718d4ad276a99ca2fb584ed688f7346b..fd8748b9bceb257f21d9bded8646808e6f2f3caf 100644 (file)
@@ -294,6 +294,17 @@ func main() {
                assert(false, `i should be "hello"`)
        }
 
+       // switch on implicit bool converted to interface
+       // was broken: see issue 3980
+       switch i := interface{}(true); {
+       case i:
+               assert(true, "true")
+       case false:
+               assert(false, "i should be true")
+       default:
+               assert(false, "i should be true")
+       }
+
        // switch on array.
        switch ar := [3]int{1, 2, 3}; ar {
        case [3]int{1,2,3}: