cmd/gc: print x++ (not x += 1) in errors about x++

Fixes #8311.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews, r
https://golang.org/cl/146270043
This commit is contained in:
Russ Cox 2014-09-25 13:13:02 -04:00
parent 53c66543e0
commit 52e9bcafe1
5 changed files with 293 additions and 262 deletions

View file

@ -810,6 +810,13 @@ stmtfmt(Fmt *f, Node *n)
break;
case OASOP:
if(n->implicit) {
if(n->etype == OADD)
fmtprint(f, "%N++", n->left);
else
fmtprint(f, "%N--", n->left);
break;
}
fmtprint(f, "%N %#O= %N", n->left, n->etype, n->right);
break;

View file

@ -460,11 +460,13 @@ simple_stmt:
| expr LINC
{
$$ = nod(OASOP, $1, nodintconst(1));
$$->implicit = 1;
$$->etype = OADD;
}
| expr LDEC
{
$$ = nod(OASOP, $1, nodintconst(1));
$$->implicit = 1;
$$->etype = OSUB;
}

View file

@ -600,6 +600,10 @@ reswitch:
}
if(t->etype != TIDEAL && !eqtype(l->type, r->type)) {
defaultlit2(&l, &r, 1);
if(n->op == OASOP && n->implicit) {
yyerror("invalid operation: %N (non-numeric type %T)", n, l->type);
goto error;
}
yyerror("invalid operation: %N (mismatched types %T and %T)", n, l->type, r->type);
goto error;
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,16 @@
// 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.
// issue 8311.
// error for x++ should say x++ not x += 1
package p
func f() {
var x []byte
x++ // ERROR "invalid operation: x[+][+]"
}