]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: Friendlier errors on oversized arrays.
authorDaniel Morsing <daniel.morsing@gmail.com>
Sun, 21 Oct 2012 17:22:51 +0000 (19:22 +0200)
committerDaniel Morsing <daniel.morsing@gmail.com>
Sun, 21 Oct 2012 17:22:51 +0000 (19:22 +0200)
Someone new to the language may not know the connection between ints and arrays, which was the only thing that the previous error told you anything about.

Fixes #4256.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6739048

src/cmd/gc/const.c
src/cmd/gc/go.h
src/cmd/gc/typecheck.c
test/fixedbugs/bug255.go

index e4c1a784ab5ffe79d5e5c8c79db08242d03ab2d8..5720efc8e92f3dd3264276c83cf3744e1040bb99 100644 (file)
@@ -348,13 +348,9 @@ toint(Val v)
        return v;
 }
 
-void
-overflow(Val v, Type *t)
+int
+doesoverflow(Val v, Type *t)
 {
-       // v has already been converted
-       // to appropriate form for t.
-       if(t == T || t->etype == TIDEAL)
-               return;
        switch(v.ctype) {
        case CTINT:
        case CTRUNE:
@@ -362,14 +358,14 @@ overflow(Val v, Type *t)
                        fatal("overflow: %T integer constant", t);
                if(mpcmpfixfix(v.u.xval, minintval[t->etype]) < 0 ||
                   mpcmpfixfix(v.u.xval, maxintval[t->etype]) > 0)
-                       yyerror("constant %B overflows %T", v.u.xval, t);
+                       return 1;
                break;
        case CTFLT:
                if(!isfloat[t->etype])
                        fatal("overflow: %T floating-point constant", t);
                if(mpcmpfltflt(v.u.fval, minfltval[t->etype]) <= 0 ||
                   mpcmpfltflt(v.u.fval, maxfltval[t->etype]) >= 0)
-                       yyerror("constant %#F overflows %T", v.u.fval, t);
+                       return 1;
                break;
        case CTCPLX:
                if(!iscomplex[t->etype])
@@ -378,7 +374,33 @@ overflow(Val v, Type *t)
                   mpcmpfltflt(&v.u.cval->real, maxfltval[t->etype]) >= 0 ||
                   mpcmpfltflt(&v.u.cval->imag, minfltval[t->etype]) <= 0 ||
                   mpcmpfltflt(&v.u.cval->imag, maxfltval[t->etype]) >= 0)
-                       yyerror("constant %#F overflows %T", v.u.fval, t);
+                       return 1;
+               break;
+       }
+       return 0;
+}
+
+void
+overflow(Val v, Type *t)
+{
+       // v has already been converted
+       // to appropriate form for t.
+       if(t == T || t->etype == TIDEAL)
+               return;
+
+       if(!doesoverflow(v, t))
+               return;
+
+       switch(v.ctype) {
+       case CTINT:
+       case CTRUNE:
+               yyerror("constant %B overflows %T", v.u.xval, t);
+               break;
+       case CTFLT:
+               yyerror("constant %#F overflows %T", v.u.fval, t);
+               break;
+       case CTCPLX:
+               yyerror("constant %#F overflows %T", v.u.fval, t);
                break;
        }
 }
index eb2345d6da3543e56cb2e562c9a9c4e9c58ad0fc..2fa6d95bef190f7b352a05ec62cd0d9e35249261 100644 (file)
@@ -178,7 +178,7 @@ struct      Type
        Strlit* note;           // literal string annotation
 
        // TARRAY
-       int32   bound;          // negative is dynamic array
+       vlong   bound;          // negative is dynamic array
 
        int32   maplineno;      // first use of TFORW as map key
        int32   embedlineno;    // first use of TFORW as embedded type
@@ -977,6 +977,7 @@ int isconst(Node *n, int ct);
 Node*  nodcplxlit(Val r, Val i);
 Node*  nodlit(Val v);
 long   nonnegconst(Node *n);
+int    doesoverflow(Val v, Type *t);
 void   overflow(Val v, Type *t);
 int    smallintconst(Node *n);
 Val    toint(Val v);
index ebc43e759834455a1b01221501b03aebc5e875ab..9869d560cb92516798a50d8270de69744d7880ad 100644 (file)
@@ -390,8 +390,10 @@ reswitch:
                        if(t->bound < 0) {
                                yyerror("array bound must be non-negative");
                                goto error;
-                       } else
-                               overflow(v, types[TINT]);
+                       } else if(doesoverflow(v, types[TINT])) {
+                               yyerror("array bound is too large"); 
+                               goto error;
+                       }
                }
                typecheck(&r, Etype);
                if(r->type == T)
index dbd41cc6ab39413b26cd8280317038e550054c1c..acf4f23910d69f2a48f530c1744cd2aef6446c89 100644 (file)
@@ -12,4 +12,4 @@ var c [1.5]int        // ERROR "truncated"
 var d ["abc"]int       // ERROR "invalid array bound|not numeric"
 var e [nil]int // ERROR "invalid array bound|not numeric"
 var f [e]int   // ERROR "invalid array bound|not constant"
-var g [1<<65]int       // ERROR "overflows"
+var g [1<<65]int       // ERROR "array bound is too large|overflows"