]> Cypherpunks repositories - gostls13.git/commitdiff
gc: avoid package name ambiguity in error messages
authorRuss Cox <rsc@golang.org>
Mon, 27 Jun 2011 22:44:30 +0000 (18:44 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 27 Jun 2011 22:44:30 +0000 (18:44 -0400)
Fixes #2006.

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

src/cmd/gc/go.h
src/cmd/gc/go.y
src/cmd/gc/subr.c
test/fixedbugs/bug345.dir/io.go [new file with mode: 0644]
test/fixedbugs/bug345.dir/main.go [new file with mode: 0644]
test/fixedbugs/bug345.go [new file with mode: 0644]

index b68768165e5a5cf92d5d422414ba2b6a9f90c995..8ca086ee043fe53df711871700e44b3f33959342 100644 (file)
@@ -302,6 +302,7 @@ struct      Sym
        uchar   flags;
        uchar   sym;            // huffman encoding in object file
        Sym*    link;
+       int32   npkg;   // number of imported packages with this name
 
        // saved and restored by dcopy
        Pkg*    pkg;
@@ -777,6 +778,7 @@ EXTERN      int32   nhunk;
 EXTERN int32   thunk;
 
 EXTERN int     exporting;
+EXTERN int     erroring;
 EXTERN int     noargnames;
 
 EXTERN int     funcdepth;
index 5d28c0e3b64a6fbd92741ce8e20a61e9ebec53d1..5d70c4edac6f84da7e8a065f1ad98913a67ccb10 100644 (file)
@@ -238,6 +238,7 @@ import_package:
        LPACKAGE sym import_safety ';'
        {
                importpkg->name = $2->name;
+               pkglookup($2->name, nil)->npkg++;
                importpkg->direct = 1;
                
                if(safemode && !curio.importsafe)
@@ -1658,6 +1659,7 @@ hidden_import:
 
                p = mkpkg($3.u.sval);
                p->name = $2->name;
+               pkglookup($2->name, nil)->npkg++;
        }
 |      LVAR hidden_pkg_importsym hidden_type ';'
        {
index 8eb60de319f9f9c8035e1cefd070110487e9977d..7c472147a2591a8925ed2300867c7df91d1d7fcc 100644 (file)
@@ -45,10 +45,12 @@ adderr(int line, char *fmt, va_list arg)
        Fmt f;
        Error *p;
 
+       erroring++;
        fmtstrinit(&f);
        fmtprint(&f, "%L: ", line);
        fmtvprint(&f, fmt, arg);
        fmtprint(&f, "\n");
+       erroring--;
 
        if(nerr >= merr) {
                if(merr == 0)
@@ -1123,6 +1125,13 @@ Sconv(Fmt *fp)
        }
 
        if(s->pkg != localpkg || longsymnames || (fp->flags & FmtLong)) {
+               // This one is for the user.  If the package name
+               // was used by multiple packages, give the full
+               // import path to disambiguate.
+               if(erroring && pkglookup(s->pkg->name, nil)->npkg > 1) {
+                       fmtprint(fp, "\"%Z\".%s", s->pkg->path, s->name);
+                       return 0;
+               }
                fmtprint(fp, "%s.%s", s->pkg->name, s->name);
                return 0;
        }
diff --git a/test/fixedbugs/bug345.dir/io.go b/test/fixedbugs/bug345.dir/io.go
new file mode 100644 (file)
index 0000000..1d695c3
--- /dev/null
@@ -0,0 +1,15 @@
+// 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 io
+
+type Writer interface {
+       WrongWrite()
+}
+
+type SectionReader struct {
+       X int
+}
+
+func SR(*SectionReader) {}
diff --git a/test/fixedbugs/bug345.dir/main.go b/test/fixedbugs/bug345.dir/main.go
new file mode 100644 (file)
index 0000000..5bdc713
--- /dev/null
@@ -0,0 +1,28 @@
+// 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
+
+import (
+       "bufio"
+       "./io"
+       goio "io"
+)
+
+func main() {
+       // The errors here complain that io.X != io.X
+       // for different values of io so they should be
+       // showing the full import path, which for the
+       // "./io" import is really ..../go/test/io.
+       // For example:
+       //
+       // main.go:25: cannot use w (type "/Users/rsc/g/go/test/fixedbugs/bug345.dir/io".Writer) as type "io".Writer in function argument:
+       //      io.Writer does not implement io.Writer (missing Write method)
+       // main.go:27: cannot use &x (type *"io".SectionReader) as type *"/Users/rsc/g/go/test/fixedbugs/bug345.dir/io".SectionReader in function argument
+
+       var w io.Writer
+       bufio.NewWriter(w)  // ERROR "test/io"
+       var x goio.SectionReader
+       io.SR(&x)  // ERROR "test/io"
+}
diff --git a/test/fixedbugs/bug345.go b/test/fixedbugs/bug345.go
new file mode 100644 (file)
index 0000000..874710c
--- /dev/null
@@ -0,0 +1,7 @@
+// $G $D/$F.dir/io.go && errchk $G -e $D/$F.dir/main.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 ignored