1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // This file implements various error reporters.
20 panic("assertion failed")
28 func (check *Checker) qualifier(pkg *Package) string {
29 // Qualify the package unless it's the package being type-checked.
31 // If the same package name was used by multiple packages, display the full path.
32 if check.pkgCnt[pkg.name] > 1 {
33 return strconv.Quote(pkg.path)
40 func (check *Checker) sprintf(format string, args ...interface{}) string {
41 for i, arg := range args {
42 switch a := arg.(type) {
46 panic("internal error: should always pass *operand")
48 arg = operandString(a, check.qualifier)
50 arg = check.fset.Position(a).String()
54 arg = ObjectString(a, check.qualifier)
56 arg = TypeString(a, check.qualifier)
60 return fmt.Sprintf(format, args...)
63 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
64 fmt.Printf("%s:\t%s%s\n",
65 check.fset.Position(pos),
66 strings.Repeat(". ", check.indent),
67 check.sprintf(format, args...),
71 // dump is only needed for debugging
72 func (check *Checker) dump(format string, args ...interface{}) {
73 fmt.Println(check.sprintf(format, args...))
76 func (check *Checker) err(err error) {
81 isInternal := errors.As(err, &e)
82 // Cheap trick: Don't report errors with messages containing
83 // "invalid operand" or "invalid type" as those tend to be
84 // follow-on errors which don't add useful information. Only
85 // exclude them if these strings are not at the beginning,
86 // and only if we have at least one error already reported.
87 isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
88 if check.firstErr != nil && isInvalidErr {
92 if check.firstErr == nil {
103 check.trace(pos, "ERROR: %s", msg)
106 f := check.conf.Error
108 panic(bailout{}) // report only first error
113 func (check *Checker) error(pos token.Pos, msg string) {
114 check.err(Error{Fset: check.fset, Pos: pos, Msg: msg})
117 // newErrorf creates a new Error, but does not handle it.
118 func (check *Checker) newErrorf(pos token.Pos, format string, args ...interface{}) error {
122 Msg: check.sprintf(format, args...),
127 func (check *Checker) errorf(pos token.Pos, format string, args ...interface{}) {
128 check.error(pos, check.sprintf(format, args...))
131 func (check *Checker) softErrorf(pos token.Pos, format string, args ...interface{}) {
135 Msg: check.sprintf(format, args...),
140 func (check *Checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
141 check.errorf(pos, "invalid AST: "+format, args...)
144 func (check *Checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
145 check.errorf(pos, "invalid argument: "+format, args...)
148 func (check *Checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
149 check.errorf(pos, "invalid operation: "+format, args...)