]> Cypherpunks repositories - gostls13.git/commitdiff
gc: preserve original expression for errors
authorRuss Cox <rsc@golang.org>
Thu, 28 Apr 2011 17:14:35 +0000 (13:14 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 28 Apr 2011 17:14:35 +0000 (13:14 -0400)
Fixes #1722.

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

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

index f58b7678916468fac848b4d0c6b97d895884d091..359881e11ea0f99bf9c05ba42e072c48f18f51e9 100644 (file)
@@ -225,6 +225,7 @@ struct      Node
        Type*   realtype;       // as determined by typecheck
        NodeList*       list;
        NodeList*       rlist;
+       Node*   orig;           // original form, for printing
 
        // for-body
        NodeList*       ninit;
index bb250569406d5bc449614a190bc4b74facf49b5f..b6fc106ab88c8cdf946b48efe7b5ab1b9b17c4cb 100644 (file)
@@ -1454,6 +1454,8 @@ Nconv(Fmt *fp)
        }
 
        if(fp->flags & FmtSharp) {
+               if(n->orig != N)
+                       n = n->orig;
                exprfmt(fp, n, 0);
                goto out;
        }
index c48bf7a29b08c51211d1b150a56ad76dc8db731e..9aaf3e6efe02747ec90d044cc52e2e7413633b3e 100644 (file)
@@ -894,12 +894,20 @@ reswitch:
                // might be constant
                switch(t->etype) {
                case TSTRING:
-                       if(isconst(l, CTSTR))
-                               nodconst(n, types[TINT], l->val.u.sval->len);
+                       if(isconst(l, CTSTR)) {
+                               r = nod(OXXX, N, N);
+                               nodconst(r, types[TINT], l->val.u.sval->len);
+                               r->orig = n;
+                               n = r;
+                       }
                        break;
                case TARRAY:
-                       if(t->bound >= 0 && l->op == ONAME)
-                               nodconst(n, types[TINT], t->bound);
+                       if(t->bound >= 0 && l->op == ONAME) {
+                               r = nod(OXXX, N, N);
+                               nodconst(r, types[TINT], t->bound);
+                               r->orig = n;
+                               n = r;
+                       }
                        break;
                }
                n->type = types[TINT];
@@ -1357,7 +1365,10 @@ ret:
                goto error;
        }
        if((top & Etop) && !(top & (Ecall|Erv|Etype)) && !(ok & Etop)) {
-               yyerror("%#N not used", n);
+               if(n->diag == 0) {
+                       yyerror("%#N not used", n);
+                       n->diag = 1;
+               }
                goto error;
        }
 
diff --git a/test/fixedbugs/bug337.go b/test/fixedbugs/bug337.go
new file mode 100644 (file)
index 0000000..62e310e
--- /dev/null
@@ -0,0 +1,19 @@
+// 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.
+
+// Issue 1722.
+
+// Check that the error messages says 
+//     bug337.go:16: len("foo") not used
+// and not
+//     bug337.go:16: 3 not used
+
+package main
+
+func main() {
+       len("foo")      // ERROR "len"
+}
+