aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2012-02-06 12:35:29 -0500
committerRuss Cox <rsc@golang.org>2012-02-06 12:35:29 -0500
commit74ee51ee92d35ccc6486b9126265bd2c62be2c3f (patch)
treee6ae64b5e543989a3cc2a505ae456d530a12581e
parent98257750f483238f2895d6077bf42e7c91cd8f3d (diff)
downloadgo-74ee51ee92d35ccc6486b9126265bd2c62be2c3f.tar.gz
go-74ee51ee92d35ccc6486b9126265bd2c62be2c3f.zip
cmd/gc: disallow switch _ := v.(type)
Fixes #2827. R=ken2 CC=golang-dev https://golang.org/cl/5638045
-rw-r--r--src/cmd/gc/go.y2
-rw-r--r--src/cmd/gc/y.tab.c2
-rw-r--r--test/typeswitch3.go4
3 files changed, 6 insertions, 2 deletions
diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y
index c44aabf398..3190963587 100644
--- a/src/cmd/gc/go.y
+++ b/src/cmd/gc/go.y
@@ -423,7 +423,7 @@ simple_stmt:
yyerror("expr.(type) must be alone in list");
if($1->next != nil)
yyerror("argument count mismatch: %d = %d", count($1), 1);
- else if($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME)
+ else if(($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME) || isblank($1->n))
yyerror("invalid variable name %N in type switch", $1->n);
else
$$->left = dclname($1->n->sym); // it's a colas, so must not re-use an oldname.
diff --git a/src/cmd/gc/y.tab.c b/src/cmd/gc/y.tab.c
index 9bf1019e9d..2ad3d89b34 100644
--- a/src/cmd/gc/y.tab.c
+++ b/src/cmd/gc/y.tab.c
@@ -2714,7 +2714,7 @@ yyreduce:
yyerror("expr.(type) must be alone in list");
if((yyvsp[(1) - (3)].list)->next != nil)
yyerror("argument count mismatch: %d = %d", count((yyvsp[(1) - (3)].list)), 1);
- else if((yyvsp[(1) - (3)].list)->n->op != ONAME && (yyvsp[(1) - (3)].list)->n->op != OTYPE && (yyvsp[(1) - (3)].list)->n->op != ONONAME)
+ else if(((yyvsp[(1) - (3)].list)->n->op != ONAME && (yyvsp[(1) - (3)].list)->n->op != OTYPE && (yyvsp[(1) - (3)].list)->n->op != ONONAME) || isblank((yyvsp[(1) - (3)].list)->n))
yyerror("invalid variable name %N in type switch", (yyvsp[(1) - (3)].list)->n);
else
(yyval.node)->left = dclname((yyvsp[(1) - (3)].list)->n->sym); // it's a colas, so must not re-use an oldname.
diff --git a/test/typeswitch3.go b/test/typeswitch3.go
index 078980146f..e11da7d747 100644
--- a/test/typeswitch3.go
+++ b/test/typeswitch3.go
@@ -30,6 +30,10 @@ func main(){
switch r.(type) {
case io.Writer:
}
+
+ // Issue 2827.
+ switch _ := r.(type) { // ERROR "invalid variable name _"
+ }
}