From 6aaa86ff667f0900bd09cb29eb722c71a3af65e1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 27 Jun 2011 18:44:30 -0400 Subject: [PATCH] gc: avoid package name ambiguity in error messages Fixes #2006. R=ken2 CC=golang-dev https://golang.org/cl/4643056 --- src/cmd/gc/go.h | 2 ++ src/cmd/gc/go.y | 2 ++ src/cmd/gc/subr.c | 9 +++++++++ test/fixedbugs/bug345.dir/io.go | 15 +++++++++++++++ test/fixedbugs/bug345.dir/main.go | 28 ++++++++++++++++++++++++++++ test/fixedbugs/bug345.go | 7 +++++++ 6 files changed, 63 insertions(+) create mode 100644 test/fixedbugs/bug345.dir/io.go create mode 100644 test/fixedbugs/bug345.dir/main.go create mode 100644 test/fixedbugs/bug345.go diff --git a/src/cmd/gc/go.h b/src/cmd/gc/go.h index b68768165e5..8ca086ee043 100644 --- a/src/cmd/gc/go.h +++ b/src/cmd/gc/go.h @@ -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; diff --git a/src/cmd/gc/go.y b/src/cmd/gc/go.y index 5d28c0e3b64..5d70c4edac6 100644 --- a/src/cmd/gc/go.y +++ b/src/cmd/gc/go.y @@ -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 ';' { diff --git a/src/cmd/gc/subr.c b/src/cmd/gc/subr.c index 8eb60de319f..7c472147a25 100644 --- a/src/cmd/gc/subr.c +++ b/src/cmd/gc/subr.c @@ -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 index 00000000000..1d695c30458 --- /dev/null +++ b/test/fixedbugs/bug345.dir/io.go @@ -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 index 00000000000..5bdc713f4e0 --- /dev/null +++ b/test/fixedbugs/bug345.dir/main.go @@ -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 index 00000000000..874710ce88a --- /dev/null +++ b/test/fixedbugs/bug345.go @@ -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