]> Cypherpunks repositories - gostls13.git/commitdiff
more precise error message
authorRuss Cox <rsc@golang.org>
Mon, 6 Jul 2009 23:29:28 +0000 (16:29 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 6 Jul 2009 23:29:28 +0000 (16:29 -0700)
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

src/cmd/gc/go.y
src/cmd/gc/swt.c

index fa6e1c752f7666451b49c5aef05841a57d733930..9c9d6441d06ca1e3935934a66eea7a76134ef688 100644 (file)
@@ -468,6 +468,7 @@ case:
                // done in casebody()
                poptodcl();
                if(typeswvar != N && typeswvar->right != N) {
+                       int e;
                        if($2->op == OLITERAL && $2->val.ctype == CTNIL) {
                                // this version in type switch case nil
                                $$ = nod(OTYPESW, N, N);
@@ -481,7 +482,16 @@ case:
                                addtotop($$);
                                break;
                        }
-                       yyerror("non-type case in type switch");
+                       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");
+                       $$ = nod(OXCASE, N, N);
+                       $$->diag = 1;
+                       break;
                }
                $$ = nod(OXCASE, $2, N);
        }
index b320475ec9368028446942028a6421423b9bdb54..6ea8c96288f2f701531234b2dfce896b4752cfce 100644 (file)
@@ -326,7 +326,7 @@ loop:
        if(n->op != OCASE)
                fatal("walkcases: not case %O\n", n->op);
 
-       if(n->left != N) {
+       if(n->left != N && !n->diag) {
                setlineno(n);
                place = call(n->left, place, arg);
        }