]> Cypherpunks repositories - gostls13.git/commitdiff
bug186 - f(iota)
authorRuss Cox <rsc@golang.org>
Wed, 12 Aug 2009 00:05:22 +0000 (17:05 -0700)
committerRuss Cox <rsc@golang.org>
Wed, 12 Aug 2009 00:05:22 +0000 (17:05 -0700)
R=ken
OCL=33051
CL=33051

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

index 73deb98263a893ac51757816cc548864bfee2a5b..72c6b745d4c0098f296b7750f379c5df9020a678 100644 (file)
@@ -470,6 +470,7 @@ enum
        Etype = 1<<3,
        Ecall = 1<<4,   // call-only expressions are ok
        Efnstruct = 1<<5,       // multivalue function returns are ok
+       Eiota = 1<<6,           // iota is ok
 };
 
 #define        BITS    5
index 983ff78356a7e011902d92fb5ee04a0e1ae73823..a7d5b8d40e5d9bc757a45d6feb5a33933492762f 100644 (file)
@@ -85,6 +85,8 @@ reswitch:
         */
        case OLITERAL:
                ok |= Erv;
+               if(n->iota && !(top & Eiota))
+                       yyerror("use of iota outside of constant initializer");
                goto ret;
 
        case ONONAME:
@@ -261,8 +263,8 @@ reswitch:
        case OSUB:
        case OXOR:
                ok |= Erv;
-               l = typecheck(&n->left, Erv);
-               r = typecheck(&n->right, Erv);
+               l = typecheck(&n->left, Erv | (top & Eiota));
+               r = typecheck(&n->right, Erv | (top & Eiota));
                if(l->type == T || r->type == T)
                        goto error;
                op = n->op;
@@ -339,7 +341,7 @@ reswitch:
        case ONOT:
        case OPLUS:
                ok |= Erv;
-               l = typecheck(&n->left, Erv);
+               l = typecheck(&n->left, Erv | (top & Eiota));
                if((t = l->type) == T)
                        goto error;
                if(!okfor[n->op][t->etype]) {
@@ -995,6 +997,8 @@ error:
 out:
        lineno = lno;
        n->typecheck = 1;
+       if(n->iota)
+               n->typecheck = 0;
        *np = n;
        return n;
 }
index d1e7f3a1beaa411fb29db16593f1d87d98ee98b1..fd1220f6186882c3aedd63fbb85a8d785381c993 100644 (file)
@@ -160,7 +160,7 @@ walkdef(Node *n)
                        dump("walkdef nil defn", n);
                        yyerror("xxx");
                }
-               typecheck(&e, Erv);
+               typecheck(&e, Erv | Eiota);
                if(e->op != OLITERAL) {
                        yyerror("const initializer must be constant");
                        goto ret;
diff --git a/test/fixedbugs/bug186.go b/test/fixedbugs/bug186.go
new file mode 100644 (file)
index 0000000..97c0947
--- /dev/null
@@ -0,0 +1,18 @@
+// errchk $G $D/$F.go
+
+// Copyright 2009 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
+
+const X = iota
+
+func f(x int) { }
+
+func main() {
+       f(X);
+       f(iota);        // ERROR "iota.*outside.*initializer"
+       f(X);
+       f(iota);        // ERROR "iota.*outside.*initializer"
+}