1
0
mirror of https://github.com/golang/go synced 2024-11-23 19:10:02 -07:00

gc: avoid package name ambiguity in error messages

Fixes #2006.

R=ken2
CC=golang-dev
https://golang.org/cl/4643056
This commit is contained in:
Russ Cox 2011-06-27 18:44:30 -04:00
parent 19f795042a
commit 6aaa86ff66
6 changed files with 63 additions and 0 deletions

View 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;

View 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 ';'
{

View 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;
}

View File

@ -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) {}

View File

@ -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"
}

7
test/fixedbugs/bug345.go Normal file
View File

@ -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