1
0
mirror of https://github.com/golang/go synced 2024-11-20 06:04:52 -07:00

more precise error message

package main

func main() {
       var x interface {} = 42;
       switch x := x.(type) {
       case int:
       case foo:
       }
}

before:
x.go:7: non-type case in type switch
x.go:7: inappropriate case for a type switch

now:
x.go:7: foo: undefined

R=ken
OCL=31221
CL=31221
This commit is contained in:
Russ Cox 2009-07-06 16:29:28 -07:00
parent a7b4e9f03e
commit 53ebd163c6
2 changed files with 12 additions and 2 deletions

View File

@ -468,6 +468,7 @@ case:
// done in casebody() // done in casebody()
poptodcl(); poptodcl();
if(typeswvar != N && typeswvar->right != N) { if(typeswvar != N && typeswvar->right != N) {
int e;
if($2->op == OLITERAL && $2->val.ctype == CTNIL) { if($2->op == OLITERAL && $2->val.ctype == CTNIL) {
// this version in type switch case nil // this version in type switch case nil
$$ = nod(OTYPESW, N, N); $$ = nod(OTYPESW, N, N);
@ -481,7 +482,16 @@ case:
addtotop($$); addtotop($$);
break; break;
} }
e = nerrors;
gettype($2, N);
// maybe gettype found problems that keep
// e from being valid even outside a type switch.
// only complain if gettype didn't print new errors.
if(nerrors == e)
yyerror("non-type case in type switch"); yyerror("non-type case in type switch");
$$ = nod(OXCASE, N, N);
$$->diag = 1;
break;
} }
$$ = nod(OXCASE, $2, N); $$ = nod(OXCASE, $2, N);
} }

View File

@ -326,7 +326,7 @@ loop:
if(n->op != OCASE) if(n->op != OCASE)
fatal("walkcases: not case %O\n", n->op); fatal("walkcases: not case %O\n", n->op);
if(n->left != N) { if(n->left != N && !n->diag) {
setlineno(n); setlineno(n);
place = call(n->left, place, arg); place = call(n->left, place, arg);
} }