1
0
mirror of https://github.com/golang/go synced 2024-11-24 07:40:17 -07:00

gc: handle invalid name in type switch

Fixes #1453.

R=ken2
CC=golang-dev
https://golang.org/cl/4125043
This commit is contained in:
Russ Cox 2011-02-01 14:00:36 -05:00
parent 865d576702
commit 05b9050bda
2 changed files with 22 additions and 2 deletions

View File

@ -422,11 +422,18 @@ simple_stmt:
| expr_list LCOLAS expr_list
{
if($3->n->op == OTYPESW) {
Node *n;
n = N;
if($3->next != nil)
yyerror("expr.(type) must be alone in list");
else if($1->next != nil)
if($1->next != nil)
yyerror("argument count mismatch: %d = %d", count($1), 1);
$$ = nod(OTYPESW, $1->n, $3->n->right);
else if($1->n->op != ONAME && $1->n->op != OTYPE && $1->n->op != ONONAME)
yyerror("invalid variable name %#N in type switch", $1->n);
else
n = $1->n;
$$ = nod(OTYPESW, n, $3->n->right);
break;
}
$$ = colas($1, $3);

13
test/syntax/typesw.go Normal file
View File

@ -0,0 +1,13 @@
// errchk $G -e $D/$F.go
// Copyright 2011 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
func main() {
switch main() := interface{}(nil).(type) { // ERROR "invalid variable name"
default:
}
}