diff options
Diffstat (limited to 'src/go/types/errors.go')
-rw-r--r-- | src/go/types/errors.go | 140 |
1 files changed, 97 insertions, 43 deletions
diff --git a/src/go/types/errors.go b/src/go/types/errors.go index fade8630e0..0dc0bc8799 100644 --- a/src/go/types/errors.go +++ b/src/go/types/errors.go @@ -8,7 +8,6 @@ package types import ( "bytes" - "errors" "fmt" "go/ast" "go/token" @@ -26,6 +25,64 @@ func unreachable() { panic("unreachable") } +// An error_ represents a type-checking error. +// To report an error_, call Checker.report. +type error_ struct { + desc []errorDesc + code errorCode + soft bool // TODO(gri) eventually determine this from an error code +} + +// An errorDesc describes part of a type-checking error. +type errorDesc struct { + posn positioner + format string + args []interface{} +} + +func (err *error_) empty() bool { + return err.desc == nil +} + +func (err *error_) pos() token.Pos { + if err.empty() { + return token.NoPos + } + return err.desc[0].posn.Pos() +} + +func (err *error_) msg(fset *token.FileSet, qf Qualifier) string { + if err.empty() { + return "no error" + } + var buf bytes.Buffer + for i := range err.desc { + p := &err.desc[i] + if i > 0 { + fmt.Fprint(&buf, "\n\t") + if p.posn.Pos().IsValid() { + fmt.Fprintf(&buf, "%s: ", fset.Position(p.posn.Pos())) + } + } + buf.WriteString(sprintf(fset, qf, false, p.format, p.args...)) + } + return buf.String() +} + +// String is for testing. +func (err *error_) String() string { + if err.empty() { + return "no error" + } + return fmt.Sprintf("%d: %s", err.pos(), err.msg(nil, nil)) +} + +// errorf adds formatted error information to err. +// It may be called multiple times to provide additional information. +func (err *error_) errorf(at token.Pos, format string, args ...interface{}) { + err.desc = append(err.desc, errorDesc{atPos(at), format, args}) +} + func (check *Checker) qualifier(pkg *Package) string { // Qualify the package unless it's the package being type-checked. if pkg != check.pkg { @@ -140,36 +197,46 @@ func (check *Checker) dump(format string, args ...any) { fmt.Println(sprintf(check.fset, check.qualifier, true, format, args...)) } -func (check *Checker) err(err error) { - if err == nil { - return +// Report records the error pointed to by errp, setting check.firstError if +// necessary. +func (check *Checker) report(errp *error_) { + if errp.empty() { + panic("empty error details") + } + + span := spanOf(errp.desc[0].posn) + e := Error{ + Fset: check.fset, + Pos: span.pos, + Msg: errp.msg(check.fset, check.qualifier), + Soft: errp.soft, + go116code: errp.code, + go116start: span.start, + go116end: span.end, } - var e Error - isInternal := errors.As(err, &e) + // Cheap trick: Don't report errors with messages containing // "invalid operand" or "invalid type" as those tend to be // follow-on errors which don't add useful information. Only // exclude them if these strings are not at the beginning, // and only if we have at least one error already reported. - isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0) + isInvalidErr := strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0 if check.firstErr != nil && isInvalidErr { return } - if isInternal { - e.Msg = stripAnnotations(e.Msg) - if check.errpos != nil { - // If we have an internal error and the errpos override is set, use it to - // augment our error positioning. - // TODO(rFindley) we may also want to augment the error message and refer - // to the position (pos) in the original expression. - span := spanOf(check.errpos) - e.Pos = span.pos - e.go116start = span.start - e.go116end = span.end - } - err = e + e.Msg = stripAnnotations(e.Msg) + if check.errpos != nil { + // If we have an internal error and the errpos override is set, use it to + // augment our error positioning. + // TODO(rFindley) we may also want to augment the error message and refer + // to the position (pos) in the original expression. + span := spanOf(check.errpos) + e.Pos = span.pos + e.go116start = span.start + e.go116end = span.end } + err := e if check.firstErr == nil { check.firstErr = err @@ -178,10 +245,6 @@ func (check *Checker) err(err error) { if trace { pos := e.Pos msg := e.Msg - if !isInternal { - msg = err.Error() - pos = token.NoPos - } check.trace(pos, "ERROR: %s", msg) } @@ -192,35 +255,26 @@ func (check *Checker) err(err error) { f(err) } -func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error { - span := spanOf(at) - return Error{ - Fset: check.fset, - Pos: span.pos, - Msg: msg, - Soft: soft, - go116code: code, - go116start: span.start, - go116end: span.end, +// newErrorf creates a new error_ for later reporting with check.report. +func newErrorf(at positioner, code errorCode, format string, args ...any) *error_ { + return &error_{ + desc: []errorDesc{{at, format, args}}, + code: code, } } -// newErrorf creates a new Error, but does not handle it. -func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...any) error { - msg := check.sprintf(format, args...) - return check.newError(at, code, soft, msg) -} - func (check *Checker) error(at positioner, code errorCode, msg string) { - check.err(check.newError(at, code, false, msg)) + check.report(newErrorf(at, code, msg)) } func (check *Checker) errorf(at positioner, code errorCode, format string, args ...any) { - check.error(at, code, check.sprintf(format, args...)) + check.report(newErrorf(at, code, format, args...)) } func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...any) { - check.err(check.newErrorf(at, code, true, format, args...)) + err := newErrorf(at, code, format, args...) + err.soft = true + check.report(err) } func (check *Checker) invalidAST(at positioner, format string, args ...any) { |