]> Cypherpunks repositories - gostls13.git/commitdiff
better error message + line numbers
authorRuss Cox <rsc@golang.org>
Mon, 6 Jul 2009 23:05:48 +0000 (16:05 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 6 Jul 2009 23:05:48 +0000 (16:05 -0700)
package main
func main() {
       var x interface{};
       switch x {
       case 41:
       case "b":
       }
}

before:
x.go:5: fatal error: exprcmp

now:
x.go:5: illegal types for operand: EQ
interface { }
int
x.go:6: illegal types for operand: EQ
interface { }
string

R=ken
OCL=31217
CL=31219

src/cmd/gc/swt.c

index 486e181b14ec83ca80cc11cb9a920e9efbae028d..b320475ec9368028446942028a6421423b9bdb54 100644 (file)
@@ -124,8 +124,12 @@ exprcmp(Case *c1, Case *c2)
        n2 = c2->node->left;
 
        ct = n1->val.ctype;
-       if(ct != n2->val.ctype)
-               fatal("exprcmp");
+       if(ct != n2->val.ctype) {
+               // invalid program, but return a sort
+               // order so that we can give a better
+               // error later.
+               return ct - n2->val.ctype;
+       }
 
        // sort by constant value
        n = 0;
@@ -323,7 +327,7 @@ loop:
                fatal("walkcases: not case %O\n", n->op);
 
        if(n->left != N) {
-               setlineno(n->left);
+               setlineno(n);
                place = call(n->left, place, arg);
        }
        n = listnext(&save);
@@ -527,12 +531,13 @@ exprbsw(Case *c0, int ncase, int arg)
        Node *cas;
        Node *a, *n;
        Case *c;
-       int i, half;
+       int i, half, lno;
 
        cas = N;
        if(ncase < Ncase) {
                for(i=0; i<ncase; i++) {
                        n = c0->node;
+                       lno = setlineno(n);
 
                        switch(arg) {
                        case Strue:
@@ -556,6 +561,7 @@ exprbsw(Case *c0, int ncase, int arg)
 
                        cas = list(cas, a);
                        c0 = c0->link;
+                       lineno = lno;
                }
                return cas;
        }