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.
19 panic("assertion failed")
27 func (check *Checker) qualifier(pkg *Package) string {
29 return path.Base(pkg.path) // avoid excessively long path names in error messages
34 func (check *Checker) sprintf(format string, args ...interface{}) string {
35 for i, arg := range args {
36 switch a := arg.(type) {
40 panic("internal error: should always pass *operand")
42 arg = operandString(a, check.qualifier)
44 arg = check.fset.Position(a).String()
48 arg = ObjectString(a, check.qualifier)
50 arg = TypeString(a, check.qualifier)
54 return fmt.Sprintf(format, args...)
57 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
58 fmt.Printf("%s:\t%s%s\n",
59 check.fset.Position(pos),
60 strings.Repeat(". ", check.indent),
61 check.sprintf(format, args...),
65 // dump is only needed for debugging
66 func (check *Checker) dump(format string, args ...interface{}) {
67 fmt.Println(check.sprintf(format, args...))
70 func (check *Checker) err(pos token.Pos, msg string, soft bool) {
71 // Cheap trick: Don't report errors with messages containing
72 // "invalid operand" or "invalid type" as those tend to be
73 // follow-on errors which don't add useful information. Only
74 // exclude them if these strings are not at the beginning,
75 // and only if we have at least one error already reported.
76 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
80 err := Error{check.fset, pos, msg, soft}
81 if check.firstErr == nil {
87 panic(bailout{}) // report only first error
92 func (check *Checker) error(pos token.Pos, msg string) {
93 check.err(pos, msg, false)
96 func (check *Checker) errorf(pos token.Pos, format string, args ...interface{}) {
97 check.err(pos, check.sprintf(format, args...), false)
100 func (check *Checker) softErrorf(pos token.Pos, format string, args ...interface{}) {
101 check.err(pos, check.sprintf(format, args...), true)
104 func (check *Checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
105 check.errorf(pos, "invalid AST: "+format, args...)
108 func (check *Checker) invalidArg(pos token.Pos, format string, args ...interface{}) {
109 check.errorf(pos, "invalid argument: "+format, args...)
112 func (check *Checker) invalidOp(pos token.Pos, format string, args ...interface{}) {
113 check.errorf(pos, "invalid operation: "+format, args...)