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.
21 panic("assertion failed")
29 func (check *Checker) qualifier(pkg *Package) string {
30 // Qualify the package unless it's the package being type-checked.
32 if check.pkgPathMap == nil {
33 check.pkgPathMap = make(map[string]map[string]bool)
34 check.seenPkgMap = make(map[*Package]bool)
35 check.markImports(check.pkg)
37 // If the same package name was used by multiple packages, display the full path.
38 if len(check.pkgPathMap[pkg.name]) > 1 {
39 return strconv.Quote(pkg.path)
46 // markImports recursively walks pkg and its imports, to record unique import
47 // paths in pkgPathMap.
48 func (check *Checker) markImports(pkg *Package) {
49 if check.seenPkgMap[pkg] {
52 check.seenPkgMap[pkg] = true
54 forName, ok := check.pkgPathMap[pkg.name]
56 forName = make(map[string]bool)
57 check.pkgPathMap[pkg.name] = forName
59 forName[pkg.path] = true
61 for _, imp := range pkg.imports {
62 check.markImports(imp)
66 func (check *Checker) sprintf(format string, args ...any) string {
67 return sprintf(check.fset, check.qualifier, false, format, args...)
70 func sprintf(fset *token.FileSet, qf Qualifier, debug bool, format string, args ...any) string {
71 for i, arg := range args {
72 switch a := arg.(type) {
76 panic("got operand instead of *operand")
78 arg = operandString(a, qf)
81 arg = fset.Position(a).String()
88 writeExprList(&buf, a)
92 arg = ObjectString(a, qf)
94 arg = typeString(a, qf, debug)
100 buf.WriteString(", ")
102 buf.WriteString(typeString(x, qf, debug))
109 return fmt.Sprintf(format, args...)
112 func (check *Checker) trace(pos token.Pos, format string, args ...any) {
113 fmt.Printf("%s:\t%s%s\n",
114 check.fset.Position(pos),
115 strings.Repeat(". ", check.indent),
116 sprintf(check.fset, check.qualifier, true, format, args...),
120 // dump is only needed for debugging
121 func (check *Checker) dump(format string, args ...any) {
122 fmt.Println(sprintf(check.fset, check.qualifier, true, format, args...))
125 func (check *Checker) err(err error) {
130 isInternal := errors.As(err, &e)
131 // Cheap trick: Don't report errors with messages containing
132 // "invalid operand" or "invalid type" as those tend to be
133 // follow-on errors which don't add useful information. Only
134 // exclude them if these strings are not at the beginning,
135 // and only if we have at least one error already reported.
136 isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
137 if check.firstErr != nil && isInvalidErr {
142 e.Msg = stripAnnotations(e.Msg)
143 if check.errpos != nil {
144 // If we have an internal error and the errpos override is set, use it to
145 // augment our error positioning.
146 // TODO(rFindley) we may also want to augment the error message and refer
147 // to the position (pos) in the original expression.
148 span := spanOf(check.errpos)
150 e.go116start = span.start
151 e.go116end = span.end
156 if check.firstErr == nil {
167 check.trace(pos, "ERROR: %s", msg)
170 f := check.conf.Error
172 panic(bailout{}) // report only first error
177 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
185 go116start: span.start,
190 // newErrorf creates a new Error, but does not handle it.
191 func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...any) error {
192 msg := check.sprintf(format, args...)
193 return check.newError(at, code, soft, msg)
196 func (check *Checker) error(at positioner, code errorCode, msg string) {
197 check.err(check.newError(at, code, false, msg))
200 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...any) {
201 check.error(at, code, check.sprintf(format, args...))
204 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...any) {
205 check.err(check.newErrorf(at, code, true, format, args...))
208 func (check *Checker) invalidAST(at positioner, format string, args ...any) {
209 check.errorf(at, 0, "invalid AST: "+format, args...)
212 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...any) {
213 check.errorf(at, code, "invalid argument: "+format, args...)
216 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...any) {
217 check.errorf(at, code, "invalid operation: "+format, args...)
220 // The positioner interface is used to extract the position of type-checker
222 type positioner interface {
226 // posSpan holds a position range along with a highlighted position within that
227 // range. This is used for positioning errors, with pos by convention being the
228 // first position in the source where the error is known to exist, and start
229 // and end defining the full span of syntax being considered when the error was
230 // detected. Invariant: start <= pos < end || start == pos == end.
231 type posSpan struct {
232 start, pos, end token.Pos
235 func (e posSpan) Pos() token.Pos {
239 // inNode creates a posSpan for the given node.
240 // Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
241 // first byte after node within the source).
242 func inNode(node ast.Node, pos token.Pos) posSpan {
243 start, end := node.Pos(), node.End()
245 assert(start <= pos && pos < end)
247 return posSpan{start, pos, end}
250 // atPos wraps a token.Pos to implement the positioner interface.
253 func (s atPos) Pos() token.Pos {
257 // spanOf extracts an error span from the given positioner. By default this is
258 // the trivial span starting and ending at pos, but this span is expanded when
259 // the argument naturally corresponds to a span of source code.
260 func spanOf(at positioner) posSpan {
261 switch x := at.(type) {
263 panic("nil positioner")
268 return posSpan{pos, pos, x.End()}
272 return posSpan{pos, pos, x.expr.End()}
274 return posSpan{token.NoPos, token.NoPos, token.NoPos}
277 return posSpan{pos, pos, pos}
281 // stripAnnotations removes internal (type) annotations from s.
282 func stripAnnotations(s string) string {
283 var b strings.Builder
284 for _, r := range s {
285 // strip #'s and subscript digits
286 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
290 if b.Len() < len(s) {