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 check.pkgPathMap == nil {
32 check.pkgPathMap = make(map[string]map[string]bool)
33 check.seenPkgMap = make(map[*Package]bool)
34 check.markImports(check.pkg)
36 // If the same package name was used by multiple packages, display the full path.
37 if len(check.pkgPathMap[pkg.name]) > 1 {
38 return strconv.Quote(pkg.path)
45 // markImports recursively walks pkg and its imports, to record unique import
46 // paths in pkgPathMap.
47 func (check *Checker) markImports(pkg *Package) {
48 if check.seenPkgMap[pkg] {
51 check.seenPkgMap[pkg] = true
53 forName, ok := check.pkgPathMap[pkg.name]
55 forName = make(map[string]bool)
56 check.pkgPathMap[pkg.name] = forName
58 forName[pkg.path] = true
60 for _, imp := range pkg.imports {
61 check.markImports(imp)
65 func (check *Checker) sprintf(format string, args ...interface{}) string {
66 for i, arg := range args {
67 switch a := arg.(type) {
71 panic("internal error: should always pass *operand")
73 arg = operandString(a, check.qualifier)
75 arg = check.fset.Position(a).String()
79 arg = ObjectString(a, check.qualifier)
81 arg = TypeString(a, check.qualifier)
85 return fmt.Sprintf(format, args...)
88 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
89 fmt.Printf("%s:\t%s%s\n",
90 check.fset.Position(pos),
91 strings.Repeat(". ", check.indent),
92 check.sprintf(format, args...),
96 // dump is only needed for debugging
97 func (check *Checker) dump(format string, args ...interface{}) {
98 fmt.Println(check.sprintf(format, args...))
101 func (check *Checker) err(err error) {
106 isInternal := errors.As(err, &e)
107 // Cheap trick: Don't report errors with messages containing
108 // "invalid operand" or "invalid type" as those tend to be
109 // follow-on errors which don't add useful information. Only
110 // exclude them if these strings are not at the beginning,
111 // and only if we have at least one error already reported.
112 isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
113 if check.firstErr != nil && isInvalidErr {
118 e.Msg = stripAnnotations(e.Msg)
119 if check.errpos != nil {
120 // If we have an internal error and the errpos override is set, use it to
121 // augment our error positioning.
122 // TODO(rFindley) we may also want to augment the error message and refer
123 // to the position (pos) in the original expression.
124 span := spanOf(check.errpos)
126 e.go116start = span.start
127 e.go116end = span.end
132 if check.firstErr == nil {
143 check.trace(pos, "ERROR: %s", msg)
146 f := check.conf.Error
148 panic(bailout{}) // report only first error
153 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
161 go116start: span.start,
166 // newErrorf creates a new Error, but does not handle it.
167 func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...interface{}) error {
168 msg := check.sprintf(format, args...)
169 return check.newError(at, code, soft, msg)
172 func (check *Checker) error(at positioner, code errorCode, msg string) {
173 check.err(check.newError(at, code, false, msg))
176 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...interface{}) {
177 check.error(at, code, check.sprintf(format, args...))
180 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...interface{}) {
181 check.err(check.newErrorf(at, code, true, format, args...))
184 func (check *Checker) invalidAST(at positioner, format string, args ...interface{}) {
185 check.errorf(at, 0, "invalid AST: "+format, args...)
188 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...interface{}) {
189 check.errorf(at, code, "invalid argument: "+format, args...)
192 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...interface{}) {
193 check.errorf(at, code, "invalid operation: "+format, args...)
196 // The positioner interface is used to extract the position of type-checker
198 type positioner interface {
202 // posSpan holds a position range along with a highlighted position within that
203 // range. This is used for positioning errors, with pos by convention being the
204 // first position in the source where the error is known to exist, and start
205 // and end defining the full span of syntax being considered when the error was
206 // detected. Invariant: start <= pos < end || start == pos == end.
207 type posSpan struct {
208 start, pos, end token.Pos
211 func (e posSpan) Pos() token.Pos {
215 // inNode creates a posSpan for the given node.
216 // Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
217 // first byte after node within the source).
218 func inNode(node ast.Node, pos token.Pos) posSpan {
219 start, end := node.Pos(), node.End()
221 assert(start <= pos && pos < end)
223 return posSpan{start, pos, end}
226 // atPos wraps a token.Pos to implement the positioner interface.
229 func (s atPos) Pos() token.Pos {
233 // spanOf extracts an error span from the given positioner. By default this is
234 // the trivial span starting and ending at pos, but this span is expanded when
235 // the argument naturally corresponds to a span of source code.
236 func spanOf(at positioner) posSpan {
237 switch x := at.(type) {
239 panic("internal error: nil")
244 return posSpan{pos, pos, x.End()}
248 return posSpan{pos, pos, x.expr.End()}
250 return posSpan{token.NoPos, token.NoPos, token.NoPos}
253 return posSpan{pos, pos, pos}
257 // stripAnnotations removes internal (type) annotations from s.
258 func stripAnnotations(s string) string {
259 var b strings.Builder
260 for _, r := range s {
261 // strip #'s and subscript digits
262 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
266 if b.Len() < len(s) {