]> Cypherpunks repositories - gostls13.git/commitdiff
gc: diagnose (x) := 0
authorRuss Cox <rsc@golang.org>
Wed, 27 Jul 2011 21:39:30 +0000 (17:39 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 27 Jul 2011 21:39:30 +0000 (17:39 -0400)
Fixes #1756.

R=ken2
CC=golang-dev
https://golang.org/cl/4810057

src/cmd/gc/dcl.c
src/cmd/gc/go.h
src/cmd/gc/go.y
src/cmd/gc/print.c
src/cmd/gc/typecheck.c
test/fixedbugs/bug351.go [new file with mode: 0644]

index 7290f9d3bbddcf14c96b1d93aa967e21c5abfa23..827cd99d9c442a4c29ba5e0f76f8f3df48534edb 100644 (file)
@@ -457,17 +457,19 @@ colasname(Node *n)
 void
 colasdefn(NodeList *left, Node *defn)
 {
-       int nnew;
+       int nnew, nerr;
        NodeList *l;
        Node *n;
 
        nnew = 0;
+       nerr = 0;
        for(l=left; l; l=l->next) {
                n = l->n;
                if(isblank(n))
                        continue;
                if(!colasname(n)) {
                        yyerror("non-name %#N on left side of :=", n);
+                       nerr++;
                        continue;
                }
                if(n->sym->block == block)
@@ -480,7 +482,7 @@ colasdefn(NodeList *left, Node *defn)
                defn->ninit = list(defn->ninit, nod(ODCL, n, N));
                l->n = n;
        }
-       if(nnew == 0)
+       if(nnew == 0 && nerr == 0)
                yyerror("no new variables on left side of :=");
 }
 
index 604a1261b8240054e9af7510f1e2a8a14d5b5116..449127292678586ce345736ee944ef0c3310dd2a 100644 (file)
@@ -393,6 +393,7 @@ enum
        ONOT, OCOM, OPLUS, OMINUS,
        OOROR,
        OPANIC, OPRINT, OPRINTN,
+       OPAREN,
        OSEND,
        OSLICE, OSLICEARR, OSLICESTR,
        ORECOVER,
index ce1d4f5f58bb3a605cbe5ece25040de4e363104f..36b549ddea29373a6d6b526564d5aa2dd684e40d 100644 (file)
@@ -916,6 +916,18 @@ pexpr:
 |      '(' expr_or_type ')'
        {
                $$ = $2;
+               
+               // Need to know on lhs of := whether there are ( ).
+               // Don't bother with the OPAREN in other cases:
+               // it's just a waste of memory and time.
+               switch($$->op) {
+               case ONAME:
+               case ONONAME:
+               case OPACK:
+               case OTYPE:
+               case OLITERAL:
+                       $$ = nod(OPAREN, $$, N);
+               }
        }
 
 expr_or_type:
index 4858af5ce7bb0a3feec99242f612b8cb4a52b7fc..5913e848a25fb9b38750555802d526cf29a8de91 100644 (file)
@@ -78,6 +78,7 @@ exprfmt(Fmt *f, Node *n, int prec)
        case OTPAREN:
        case OINDEX:
        case OINDEXMAP:
+       case OPAREN:
                nprec = 7;
                break;
 
@@ -134,6 +135,10 @@ exprfmt(Fmt *f, Node *n, int prec)
                fmtprint(f, "(node %O)", n->op);
                break;
 
+       case OPAREN:
+               fmtprint(f, "(%#N)", n->left);
+               break;
+
        case OREGISTER:
                fmtprint(f, "%R", n->val.u.reg);
                break;
index 80af8201d8e5693e0067a971b81b2db17869f686..81b9dd2c86b23e57893f3d1ef00e6a60bf9bc6d4 100644 (file)
@@ -124,9 +124,16 @@ typecheck(Node **np, int top)
        n = *np;
        if(n == N)
                return N;
+       
+       lno = setlineno(n);
+
+       // Skip over parens.
+       while(n->op == OPAREN)
+               n = n->left;
 
        // Resolve definition of name and value of iota lazily.
        n = resolve(n);
+
        *np = n;
 
        // Skip typecheck if already done.
diff --git a/test/fixedbugs/bug351.go b/test/fixedbugs/bug351.go
new file mode 100644 (file)
index 0000000..c33e282
--- /dev/null
@@ -0,0 +1,11 @@
+// errchk $G $D/$F.go
+
+// Copyright 2011 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
+
+func main() {
+       (x) := 0  // ERROR "non-name [(]x[)]"
+}