1
0
mirror of https://github.com/golang/go synced 2024-11-21 20:44:39 -07:00

gc: diagnose (x) := 0

Fixes #1756.

R=ken2
CC=golang-dev
https://golang.org/cl/4810057
This commit is contained in:
Russ Cox 2011-07-27 17:39:30 -04:00
parent db3088a37c
commit 112267d55e
6 changed files with 40 additions and 2 deletions

View File

@ -457,17 +457,19 @@ colasname(Node *n)
void void
colasdefn(NodeList *left, Node *defn) colasdefn(NodeList *left, Node *defn)
{ {
int nnew; int nnew, nerr;
NodeList *l; NodeList *l;
Node *n; Node *n;
nnew = 0; nnew = 0;
nerr = 0;
for(l=left; l; l=l->next) { for(l=left; l; l=l->next) {
n = l->n; n = l->n;
if(isblank(n)) if(isblank(n))
continue; continue;
if(!colasname(n)) { if(!colasname(n)) {
yyerror("non-name %#N on left side of :=", n); yyerror("non-name %#N on left side of :=", n);
nerr++;
continue; continue;
} }
if(n->sym->block == block) if(n->sym->block == block)
@ -480,7 +482,7 @@ colasdefn(NodeList *left, Node *defn)
defn->ninit = list(defn->ninit, nod(ODCL, n, N)); defn->ninit = list(defn->ninit, nod(ODCL, n, N));
l->n = n; l->n = n;
} }
if(nnew == 0) if(nnew == 0 && nerr == 0)
yyerror("no new variables on left side of :="); yyerror("no new variables on left side of :=");
} }

View File

@ -393,6 +393,7 @@ enum
ONOT, OCOM, OPLUS, OMINUS, ONOT, OCOM, OPLUS, OMINUS,
OOROR, OOROR,
OPANIC, OPRINT, OPRINTN, OPANIC, OPRINT, OPRINTN,
OPAREN,
OSEND, OSEND,
OSLICE, OSLICEARR, OSLICESTR, OSLICE, OSLICEARR, OSLICESTR,
ORECOVER, ORECOVER,

View File

@ -916,6 +916,18 @@ pexpr:
| '(' expr_or_type ')' | '(' expr_or_type ')'
{ {
$$ = $2; $$ = $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: expr_or_type:

View File

@ -78,6 +78,7 @@ exprfmt(Fmt *f, Node *n, int prec)
case OTPAREN: case OTPAREN:
case OINDEX: case OINDEX:
case OINDEXMAP: case OINDEXMAP:
case OPAREN:
nprec = 7; nprec = 7;
break; break;
@ -134,6 +135,10 @@ exprfmt(Fmt *f, Node *n, int prec)
fmtprint(f, "(node %O)", n->op); fmtprint(f, "(node %O)", n->op);
break; break;
case OPAREN:
fmtprint(f, "(%#N)", n->left);
break;
case OREGISTER: case OREGISTER:
fmtprint(f, "%R", n->val.u.reg); fmtprint(f, "%R", n->val.u.reg);
break; break;

View File

@ -124,9 +124,16 @@ typecheck(Node **np, int top)
n = *np; n = *np;
if(n == N) if(n == N)
return 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. // Resolve definition of name and value of iota lazily.
n = resolve(n); n = resolve(n);
*np = n; *np = n;
// Skip typecheck if already done. // Skip typecheck if already done.

11
test/fixedbugs/bug351.go Normal file
View File

@ -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[)]"
}