objMap map[Object]*declInfo // maps package-level objects and (non-interface) methods to declaration info
impMap map[importKey]*Package // maps (import path, source directory) to (complete or fake) package
posMap map[*Interface][]token.Pos // maps interface types to lists of embedded interface positions
+ pkgCnt map[string]int // counts number of imported packages with a given name (for better error messages)
// information collected during type-checking of a set of package files
// (initialized by Files, valid only for the duration of check.Files;
objMap: make(map[Object]*declInfo),
impMap: make(map[importKey]*Package),
posMap: make(map[*Interface][]token.Pos),
+ pkgCnt: make(map[string]int),
}
}
"fmt"
"go/ast"
"go/token"
- "path"
+ "strconv"
"strings"
)
}
func (check *Checker) qualifier(pkg *Package) string {
+ // Qualify the package unless it's the package being type-checked.
if pkg != check.pkg {
- return path.Base(pkg.path) // avoid excessively long path names in error messages
+ // If the same package name was used by multiple packages, display the full path.
+ if check.pkgCnt[pkg.name] > 1 {
+ return strconv.Quote(pkg.path)
+ }
+ return pkg.name
}
return ""
}
// package should be complete or marked fake, but be cautious
if imp.complete || imp.fake {
check.impMap[key] = imp
+ check.pkgCnt[imp.name]++
return imp
}
package issues
-import "fmt"
-import syn "cmd/compile/internal/syntax"
+import (
+ "fmt"
+ syn "cmd/compile/internal/syntax"
+ t1 "text/template"
+ t2 "html/template"
+)
func issue7035() {
type T struct{ X int }
// Issue #26234: Make various field/method lookup errors easier to read by matching cmd/compile's output
func issue26234a(f *syn.File) {
- // The error message below should refer to the actual package path base (syntax)
+ // The error message below should refer to the actual package name (syntax)
// not the local package name (syn).
f.foo /* ERROR f.foo undefined \(type \*syntax.File has no field or method foo\) */
}
func issue26234c() {
T.x /* ERROR T.x undefined \(type T has no method x\) */ ()
}
+
+func issue35895() {
+ // T is defined in this package, don't qualify its name with the package name.
+ var _ T = 0 // ERROR cannot convert 0 \(untyped int constant\) to T
+
+ // There is only one package with name syntax imported, only use the (global) package name in error messages.
+ var _ *syn.File = 0 // ERROR cannot convert 0 \(untyped int constant\) to \*syntax.File
+
+ // Because both t1 and t2 have the same global package name (template),
+ // qualify packages with full path name in this case.
+ var _ t1.Template = t2 /* ERROR cannot use .* \(value of type "html/template".Template\) as "text/template".Template */ .Template{}
+}
\ No newline at end of file