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 msg := "assertion failed"
22 // Include information about the assertion location. Due to panic recovery,
23 // this location is otherwise buried in the middle of the panicking stack.
24 if _, file, line, ok := runtime.Caller(1); ok {
25 msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
35 // An error_ represents a type-checking error.
36 // To report an error_, call Checker.report.
40 soft bool // TODO(gri) eventually determine this from an error code
43 // An errorDesc describes part of a type-checking error.
44 type errorDesc struct {
50 func (err *error_) empty() bool {
51 return err.desc == nil
54 func (err *error_) pos() token.Pos {
58 return err.desc[0].posn.Pos()
61 func (err *error_) msg(fset *token.FileSet, qf Qualifier) string {
65 var buf strings.Builder
66 for i := range err.desc {
69 fmt.Fprint(&buf, "\n\t")
70 if p.posn.Pos().IsValid() {
71 fmt.Fprintf(&buf, "%s: ", fset.Position(p.posn.Pos()))
74 buf.WriteString(sprintf(fset, qf, false, p.format, p.args...))
79 // String is for testing.
80 func (err *error_) String() string {
84 return fmt.Sprintf("%d: %s", err.pos(), err.msg(nil, nil))
87 // errorf adds formatted error information to err.
88 // It may be called multiple times to provide additional information.
89 func (err *error_) errorf(at token.Pos, format string, args ...interface{}) {
90 err.desc = append(err.desc, errorDesc{atPos(at), format, args})
93 func (check *Checker) qualifier(pkg *Package) string {
94 // Qualify the package unless it's the package being type-checked.
96 if check.pkgPathMap == nil {
97 check.pkgPathMap = make(map[string]map[string]bool)
98 check.seenPkgMap = make(map[*Package]bool)
99 check.markImports(check.pkg)
101 // If the same package name was used by multiple packages, display the full path.
102 if len(check.pkgPathMap[pkg.name]) > 1 {
103 return strconv.Quote(pkg.path)
110 // markImports recursively walks pkg and its imports, to record unique import
111 // paths in pkgPathMap.
112 func (check *Checker) markImports(pkg *Package) {
113 if check.seenPkgMap[pkg] {
116 check.seenPkgMap[pkg] = true
118 forName, ok := check.pkgPathMap[pkg.name]
120 forName = make(map[string]bool)
121 check.pkgPathMap[pkg.name] = forName
123 forName[pkg.path] = true
125 for _, imp := range pkg.imports {
126 check.markImports(imp)
131 func (check *Checker) sprintf(format string, args ...any) string {
132 var fset *token.FileSet
138 return sprintf(fset, qf, false, format, args...)
141 func sprintf(fset *token.FileSet, qf Qualifier, debug bool, format string, args ...any) string {
142 for i, arg := range args {
143 switch a := arg.(type) {
147 panic("got operand instead of *operand")
149 arg = operandString(a, qf)
152 arg = fset.Position(a).String()
159 writeExprList(&buf, a)
163 arg = ObjectString(a, qf)
165 arg = typeString(a, qf, debug)
167 var buf strings.Builder
169 for i, x := range a {
171 buf.WriteString(", ")
173 buf.WriteString(typeString(x, qf, debug))
178 var buf strings.Builder
180 for i, x := range a {
182 buf.WriteString(", ")
184 buf.WriteString(typeString(x, qf, debug)) // use typeString so we get subscripts when debugging
191 return fmt.Sprintf(format, args...)
194 func (check *Checker) trace(pos token.Pos, format string, args ...any) {
195 fmt.Printf("%s:\t%s%s\n",
196 check.fset.Position(pos),
197 strings.Repeat(". ", check.indent),
198 sprintf(check.fset, check.qualifier, true, format, args...),
202 // dump is only needed for debugging
203 func (check *Checker) dump(format string, args ...any) {
204 fmt.Println(sprintf(check.fset, check.qualifier, true, format, args...))
207 // Report records the error pointed to by errp, setting check.firstError if
209 func (check *Checker) report(errp *error_) {
211 panic("empty error details")
214 span := spanOf(errp.desc[0].posn)
218 Msg: errp.msg(check.fset, check.qualifier),
220 go116code: errp.code,
221 go116start: span.start,
225 // Cheap trick: Don't report errors with messages containing
226 // "invalid operand" or "invalid type" as those tend to be
227 // follow-on errors which don't add useful information. Only
228 // exclude them if these strings are not at the beginning,
229 // and only if we have at least one error already reported.
230 isInvalidErr := strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0
231 if check.firstErr != nil && isInvalidErr {
235 e.Msg = stripAnnotations(e.Msg)
236 if check.errpos != nil {
237 // If we have an internal error and the errpos override is set, use it to
238 // augment our error positioning.
239 // TODO(rFindley) we may also want to augment the error message and refer
240 // to the position (pos) in the original expression.
241 span := spanOf(check.errpos)
243 e.go116start = span.start
244 e.go116end = span.end
248 if check.firstErr == nil {
255 check.trace(pos, "ERROR: %s", msg)
258 f := check.conf.Error
260 panic(bailout{}) // report only first error
265 // newErrorf creates a new error_ for later reporting with check.report.
266 func newErrorf(at positioner, code errorCode, format string, args ...any) *error_ {
268 desc: []errorDesc{{at, format, args}},
273 func (check *Checker) error(at positioner, code errorCode, msg string) {
274 check.report(newErrorf(at, code, msg))
277 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...any) {
278 check.report(newErrorf(at, code, format, args...))
281 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...any) {
282 err := newErrorf(at, code, format, args...)
287 func (check *Checker) versionErrorf(at positioner, code errorCode, goVersion string, format string, args ...interface{}) {
288 msg := check.sprintf(format, args...)
290 if compilerErrorMessages {
291 err = newErrorf(at, code, "%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
293 err = newErrorf(at, code, "%s requires %s or later", msg, goVersion)
298 func (check *Checker) invalidAST(at positioner, format string, args ...any) {
299 check.errorf(at, 0, "invalid AST: "+format, args...)
302 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...any) {
303 check.errorf(at, code, "invalid argument: "+format, args...)
306 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...any) {
307 check.errorf(at, code, "invalid operation: "+format, args...)
310 // The positioner interface is used to extract the position of type-checker
312 type positioner interface {
316 // posSpan holds a position range along with a highlighted position within that
317 // range. This is used for positioning errors, with pos by convention being the
318 // first position in the source where the error is known to exist, and start
319 // and end defining the full span of syntax being considered when the error was
320 // detected. Invariant: start <= pos < end || start == pos == end.
321 type posSpan struct {
322 start, pos, end token.Pos
325 func (e posSpan) Pos() token.Pos {
329 // inNode creates a posSpan for the given node.
330 // Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
331 // first byte after node within the source).
332 func inNode(node ast.Node, pos token.Pos) posSpan {
333 start, end := node.Pos(), node.End()
335 assert(start <= pos && pos < end)
337 return posSpan{start, pos, end}
340 // atPos wraps a token.Pos to implement the positioner interface.
343 func (s atPos) Pos() token.Pos {
347 // spanOf extracts an error span from the given positioner. By default this is
348 // the trivial span starting and ending at pos, but this span is expanded when
349 // the argument naturally corresponds to a span of source code.
350 func spanOf(at positioner) posSpan {
351 switch x := at.(type) {
353 panic("nil positioner")
358 return posSpan{pos, pos, x.End()}
362 return posSpan{pos, pos, x.expr.End()}
364 return posSpan{token.NoPos, token.NoPos, token.NoPos}
367 return posSpan{pos, pos, pos}
371 // stripAnnotations removes internal (type) annotations from s.
372 func stripAnnotations(s string) string {
373 var buf strings.Builder
374 for _, r := range s {
375 // strip #'s and subscript digits
376 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
380 if buf.Len() < len(s) {