FAQ
Reviewers: golang-dev_googlegroups.com,

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

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


Description:
cmd/gc: add missing conversion from bool to interface in switches.

In switches without an expression, the compiler would not convert the
implicit true to an interface, causing codegen errors.

Fixes issue 3980.

Please review this at http://codereview.appspot.com/6497147/

Affected files:
M src/cmd/gc/swt.c
M test/switch.go


Index: src/cmd/gc/swt.c
===================================================================
--- a/src/cmd/gc/swt.c
+++ b/src/cmd/gc/swt.c
@@ -442,6 +442,10 @@
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 @@
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 @@
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: test/switch.go
===================================================================
--- a/test/switch.go
+++ b/test/switch.go
@@ -294,6 +294,16 @@
assert(false, `i should be "hello"`)
}

+ // switch on implicit bool converted to interface
+ 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}:

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedSep 17, '12 at 6:17p
activeSep 17, '12 at 7:29p
posts5
users2
websitegolang.org

2 users in discussion

Daniel Morsing: 3 posts Rsc: 2 posts

People

Translate

site design / logo © 2022 Grokbase