From fb0632eb7d817e95b706d7841c5192343a08826a Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 31 Oct 2013 12:01:00 -0700 Subject: [PATCH] go.tools/go/types: Provide explicit type checker Error. R=adonovan CC=golang-dev https://golang.org/cl/20400044 --- go/types/api.go | 19 ++++++++++++++++--- go/types/errors.go | 5 +++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/go/types/api.go b/go/types/api.go index a733581d..3f4c9967 100644 --- a/go/types/api.go +++ b/go/types/api.go @@ -23,6 +23,7 @@ package types import ( + "fmt" "go/ast" "go/token" @@ -44,6 +45,20 @@ func Check(path string, fset *token.FileSet, files []*ast.File) (*Package, error return pkg, nil } +// An Error describes a type-checking error; +// it implements the error interface. +type Error struct { + Fset *token.FileSet // file set for interpretation of Pos + Pos token.Pos // error position + Msg string // error message +} + +// Error returns an error string formatted as follows: +// filename:line:column: message +func (err Error) Error() string { + return fmt.Sprintf("%s: %s", err.Fset.Position(err.Pos), err.Msg) +} + // A Config specifies the configuration for type checking. // The zero value for Config is a ready-to-use default configuration. type Config struct { @@ -66,9 +81,7 @@ type Config struct { Packages map[string]*Package // If Error != nil, it is called with each error found - // during type checking. The error strings of errors with - // detailed position information are formatted as follows: - // filename:line:column: message + // during type checking; err has dynamic type Error. Error func(err error) // If Import != nil, it is called for each imported package. diff --git a/go/types/errors.go b/go/types/errors.go index 7173774a..66e8e95b 100644 --- a/go/types/errors.go +++ b/go/types/errors.go @@ -53,7 +53,8 @@ func (check *checker) dump(format string, args ...interface{}) { fmt.Println(check.sprintf(format, args...)) } -func (check *checker) err(err error) { +func (check *checker) err(pos token.Pos, msg string) { + err := Error{check.fset, pos, msg} if check.firstErr == nil { check.firstErr = err } @@ -65,7 +66,7 @@ func (check *checker) err(err error) { } func (check *checker) errorf(pos token.Pos, format string, args ...interface{}) { - check.err(fmt.Errorf("%s: %s", check.fset.Position(pos), check.sprintf(format, args...))) + check.err(pos, check.sprintf(format, args...)) } func (check *checker) invalidAST(pos token.Pos, format string, args ...interface{}) {