]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/gc: prohibit short variable declarations containing duplicate symbols
authorEvan Kroske <evankroske@google.com>
Mon, 6 Oct 2014 21:16:39 +0000 (17:16 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 6 Oct 2014 21:16:39 +0000 (17:16 -0400)
Fixes #6764.
Fixes #8435.

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

src/cmd/gc/dcl.c
test/assign.go

index 73c2581beb63a0d7ab4c7825dee7190103bab3e9..cc010d901cb7f1e003d3c36ae6ba9a7ea93dc16b 100644 (file)
@@ -488,6 +488,10 @@ colasdefn(NodeList *left, Node *defn)
        NodeList *l;
        Node *n;
 
+       for(l=left; l; l=l->next)
+               if(l->n->sym != S)
+                       l->n->sym->flags |= SymUniq;
+
        nnew = 0;
        nerr = 0;
        for(l=left; l; l=l->next) {
@@ -499,6 +503,13 @@ colasdefn(NodeList *left, Node *defn)
                        nerr++;
                        continue;
                }
+               if((n->sym->flags & SymUniq) == 0) {
+                       yyerrorl(defn->lineno, "%S repeated on left side of :=", n->sym);
+                       n->diag++;
+                       nerr++;
+                       continue;
+               }
+               n->sym->flags &= ~SymUniq;
                if(n->sym->block == block)
                        continue;
 
index da0192f838d84c79199450be392c67eb50dac578..6611f8ce3ef17a41721adb12356a7dfba2077f7e 100644 (file)
@@ -53,4 +53,16 @@ func main() {
                _ = x
                _ = y
        }
+       {
+               var x = 1
+               {
+                       x, x := 2, 3 // ERROR "x repeated on left side of :="
+                       _ = x
+               }
+               _ = x
+       }
+       {
+               a, a := 1, 2 // ERROR "a repeated on left side of :="
+               _ = a
+       }
 }