Commit a7a3fe72 authored by Daniel Morsing's avatar Daniel Morsing

cmd/gc: Friendlier errors on oversized arrays.

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
parent c1b7ddc6
...@@ -348,13 +348,9 @@ toint(Val v) ...@@ -348,13 +348,9 @@ toint(Val v)
return v; return v;
} }
void int
overflow(Val v, Type *t) 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) { switch(v.ctype) {
case CTINT: case CTINT:
case CTRUNE: case CTRUNE:
...@@ -362,14 +358,14 @@ overflow(Val v, Type *t) ...@@ -362,14 +358,14 @@ overflow(Val v, Type *t)
fatal("overflow: %T integer constant", t); fatal("overflow: %T integer constant", t);
if(mpcmpfixfix(v.u.xval, minintval[t->etype]) < 0 || if(mpcmpfixfix(v.u.xval, minintval[t->etype]) < 0 ||
mpcmpfixfix(v.u.xval, maxintval[t->etype]) > 0) mpcmpfixfix(v.u.xval, maxintval[t->etype]) > 0)
yyerror("constant %B overflows %T", v.u.xval, t); return 1;
break; break;
case CTFLT: case CTFLT:
if(!isfloat[t->etype]) if(!isfloat[t->etype])
fatal("overflow: %T floating-point constant", t); fatal("overflow: %T floating-point constant", t);
if(mpcmpfltflt(v.u.fval, minfltval[t->etype]) <= 0 || if(mpcmpfltflt(v.u.fval, minfltval[t->etype]) <= 0 ||
mpcmpfltflt(v.u.fval, maxfltval[t->etype]) >= 0) mpcmpfltflt(v.u.fval, maxfltval[t->etype]) >= 0)
yyerror("constant %#F overflows %T", v.u.fval, t); return 1;
break; break;
case CTCPLX: case CTCPLX:
if(!iscomplex[t->etype]) if(!iscomplex[t->etype])
...@@ -378,7 +374,33 @@ overflow(Val v, Type *t) ...@@ -378,7 +374,33 @@ overflow(Val v, Type *t)
mpcmpfltflt(&v.u.cval->real, maxfltval[t->etype]) >= 0 || mpcmpfltflt(&v.u.cval->real, maxfltval[t->etype]) >= 0 ||
mpcmpfltflt(&v.u.cval->imag, minfltval[t->etype]) <= 0 || mpcmpfltflt(&v.u.cval->imag, minfltval[t->etype]) <= 0 ||
mpcmpfltflt(&v.u.cval->imag, maxfltval[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; break;
} }
} }
......
...@@ -178,7 +178,7 @@ struct Type ...@@ -178,7 +178,7 @@ struct Type
Strlit* note; // literal string annotation Strlit* note; // literal string annotation
// TARRAY // TARRAY
int32 bound; // negative is dynamic array vlong bound; // negative is dynamic array
int32 maplineno; // first use of TFORW as map key int32 maplineno; // first use of TFORW as map key
int32 embedlineno; // first use of TFORW as embedded type int32 embedlineno; // first use of TFORW as embedded type
...@@ -977,6 +977,7 @@ int isconst(Node *n, int ct); ...@@ -977,6 +977,7 @@ int isconst(Node *n, int ct);
Node* nodcplxlit(Val r, Val i); Node* nodcplxlit(Val r, Val i);
Node* nodlit(Val v); Node* nodlit(Val v);
long nonnegconst(Node *n); long nonnegconst(Node *n);
int doesoverflow(Val v, Type *t);
void overflow(Val v, Type *t); void overflow(Val v, Type *t);
int smallintconst(Node *n); int smallintconst(Node *n);
Val toint(Val v); Val toint(Val v);
......
...@@ -390,8 +390,10 @@ reswitch: ...@@ -390,8 +390,10 @@ reswitch:
if(t->bound < 0) { if(t->bound < 0) {
yyerror("array bound must be non-negative"); yyerror("array bound must be non-negative");
goto error; goto error;
} else } else if(doesoverflow(v, types[TINT])) {
overflow(v, types[TINT]); yyerror("array bound is too large");
goto error;
}
} }
typecheck(&r, Etype); typecheck(&r, Etype);
if(r->type == T) if(r->type == T)
......
...@@ -12,4 +12,4 @@ var c [1.5]int // ERROR "truncated" ...@@ -12,4 +12,4 @@ var c [1.5]int // ERROR "truncated"
var d ["abc"]int // ERROR "invalid array bound|not numeric" var d ["abc"]int // ERROR "invalid array bound|not numeric"
var e [nil]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 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"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment