]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: diagnose "make([]T, non-integer)" correctly.
authorShenghou Ma <minux.ma@gmail.com>
Sun, 23 Feb 2014 21:31:48 +0000 (16:31 -0500)
committerShenghou Ma <minux.ma@gmail.com>
Sun, 23 Feb 2014 21:31:48 +0000 (16:31 -0500)
Fixes #7223.

LGTM=rsc
R=golang-codereviews, gobot, rsc
CC=golang-codereviews
https://golang.org/cl/63040043

src/cmd/gc/typecheck.c
test/fixedbugs/issue7223.go [new file with mode: 0644]

index ac2e8a45595d6c3f118cf3094e854d7ba2ecc602..05efab4040af39cf422a736a9707e2ecbd8b96b1 100644 (file)
@@ -3231,29 +3231,38 @@ static int
 checkmake(Type *t, char *arg, Node *n)
 {
        if(n->op == OLITERAL) {
-               n->val = toint(n->val);
-               if(mpcmpfixc(n->val.u.xval, 0) < 0) {
-                       yyerror("negative %s argument in make(%T)", arg, t);
-                       return -1;
-               }
-               if(mpcmpfixfix(n->val.u.xval, maxintval[TINT]) > 0) {
-                       yyerror("%s argument too large in make(%T)", arg, t);
-                       return -1;
+               switch(n->val.ctype) {
+               case CTINT:
+               case CTRUNE:
+               case CTFLT:
+               case CTCPLX:
+                       n->val = toint(n->val);
+                       if(mpcmpfixc(n->val.u.xval, 0) < 0) {
+                               yyerror("negative %s argument in make(%T)", arg, t);
+                               return -1;
+                       }
+                       if(mpcmpfixfix(n->val.u.xval, maxintval[TINT]) > 0) {
+                               yyerror("%s argument too large in make(%T)", arg, t);
+                               return -1;
+                       }
+                       
+                       // Delay defaultlit until after we've checked range, to avoid
+                       // a redundant "constant NNN overflows int" error.
+                       defaultlit(&n, types[TINT]);
+                       return 0;
+               default:
+                       break;
                }
-               
-               // Delay defaultlit until after we've checked range, to avoid
-               // a redundant "constant NNN overflows int" error.
-               defaultlit(&n, types[TINT]);
-               return 0;
        }
-       
-       // Defaultlit still necessary for non-constant: n might be 1<<k.
-       defaultlit(&n, types[TINT]);
 
-       if(!isint[n->type->etype]) {
+       if(!isint[n->type->etype] && n->type->etype != TIDEAL) {
                yyerror("non-integer %s argument in make(%T) - %T", arg, t, n->type);
                return -1;
        }
+
+       // Defaultlit still necessary for non-constant: n might be 1<<k.
+       defaultlit(&n, types[TINT]);
+
        return 0;
 }
 
diff --git a/test/fixedbugs/issue7223.go b/test/fixedbugs/issue7223.go
new file mode 100644 (file)
index 0000000..c5955d5
--- /dev/null
@@ -0,0 +1,20 @@
+// errorcheck
+
+// Copyright 2014 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+var bits1 uint = 10
+const bits2 uint = 10
+
+func main() {
+       _ = make([]byte, 1<<bits1)
+       _ = make([]byte, 1<<bits2)
+       _ = make([]byte, nil) // ERROR "non-integer.*len"
+       _ = make([]byte, nil, 2) // ERROR "non-integer.*len"
+       _ = make([]byte, 1, nil) // ERROR "non-integer.*cap"
+       _ = make([]byte, true) // ERROR "non-integer.*len"
+       _ = make([]byte, "abc") // ERROR "non-integer.*len"
+}