1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:04:46 -07:00

go.tools/go/types: use qualified names when printing types in errors

Also: Require that clean package paths are not ".".

R=adonovan, r
CC=golang-dev
https://golang.org/cl/10368047
This commit is contained in:
Robert Griesemer 2013-06-18 16:41:11 -07:00
parent 9a50e157b4
commit d338982a64
4 changed files with 21 additions and 7 deletions

View File

@ -138,6 +138,8 @@ type Importer func(imports map[string]*Package, path string) (pkg *Package, err
// entire package is checked. If there are errors, the package may be
// only partially type-checked, and the resulting package may be incomplete
// (missing objects, imports, etc.).
// The provided package path must not resolve to ".", otherwise Check
// returns immediately with a corresponding error message.
func (ctxt *Context) Check(path string, fset *token.FileSet, files ...*ast.File) (*Package, error) {
return check(ctxt, path, fset, files...)
}

View File

@ -10,6 +10,7 @@ import (
"fmt"
"go/ast"
"go/token"
"path"
"code.google.com/p/go.tools/go/exact"
)
@ -83,9 +84,9 @@ func (check *checker) later(f *Func, sig *Signature, body *ast.BlockStmt) {
// A bailout panic is raised to indicate early termination.
type bailout struct{}
func check(ctxt *Context, path string, fset *token.FileSet, files ...*ast.File) (pkg *Package, err error) {
func check(ctxt *Context, pkgPath string, fset *token.FileSet, files ...*ast.File) (pkg *Package, err error) {
pkg = &Package{
path: path,
path: pkgPath,
scope: NewScope(Universe),
imports: make(map[string]*Package),
}
@ -117,6 +118,12 @@ func check(ctxt *Context, path string, fset *token.FileSet, files ...*ast.File)
}
}()
// we need a reasonable path to continue
if path.Clean(pkgPath) == "." {
check.errorf(token.NoPos, "Check invoked with invalid package path: %q", pkgPath)
return
}
// determine package name and files
i := 0
for _, file := range files {

View File

@ -320,9 +320,13 @@ func writeType(buf *bytes.Buffer, typ Type) {
case *Named:
s := "<Named w/o object>"
if obj := t.obj; obj != nil {
if obj.pkg != nil && obj.pkg.path != "" {
buf.WriteString(obj.pkg.path)
buf.WriteString(".")
if obj.pkg != nil {
// TODO(gri) Ideally we only want the qualification
// if we are referring to a type that was imported;
// but not when we are at the "top". We don't have
// this information easily available here.
buf.WriteString(obj.pkg.name)
buf.WriteByte('.')
}
s = t.obj.name
}

View File

@ -20,7 +20,8 @@ func makePkg(t *testing.T, src string) (*Package, error) {
if err != nil {
return nil, err
}
pkg, err := Check("", fset, file)
// use the package name as package path
pkg, err := Check(file.Name.Name, fset, file)
return pkg, err
}
@ -56,7 +57,7 @@ var testTypes = []testEntry{
{`struct {
string
elems []T
}`, `struct{string; elems []T}`},
}`, `struct{string; elems []p.T}`},
// pointers
dup("*int"),