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 return sprintf(check.fset, check.qualifier, format, args...)
69 func sprintf(fset *token.FileSet, qf Qualifier, format string, args ...interface{}) string {
70 for i, arg := range args {
71 switch a := arg.(type) {
75 panic("got operand instead of *operand")
77 arg = operandString(a, qf)
80 arg = fset.Position(a).String()
85 arg = ObjectString(a, qf)
87 arg = TypeString(a, qf)
91 return fmt.Sprintf(format, args...)
94 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
95 fmt.Printf("%s:\t%s%s\n",
96 check.fset.Position(pos),
97 strings.Repeat(". ", check.indent),
98 check.sprintf(format, args...),
102 // dump is only needed for debugging
103 func (check *Checker) dump(format string, args ...interface{}) {
104 fmt.Println(check.sprintf(format, args...))
107 func (check *Checker) err(err error) {
112 isInternal := errors.As(err, &e)
113 // Cheap trick: Don't report errors with messages containing
114 // "invalid operand" or "invalid type" as those tend to be
115 // follow-on errors which don't add useful information. Only
116 // exclude them if these strings are not at the beginning,
117 // and only if we have at least one error already reported.
118 isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
119 if check.firstErr != nil && isInvalidErr {
124 e.Msg = stripAnnotations(e.Msg)
125 if check.errpos != nil {
126 // If we have an internal error and the errpos override is set, use it to
127 // augment our error positioning.
128 // TODO(rFindley) we may also want to augment the error message and refer
129 // to the position (pos) in the original expression.
130 span := spanOf(check.errpos)
132 e.go116start = span.start
133 e.go116end = span.end
138 if check.firstErr == nil {
149 check.trace(pos, "ERROR: %s", msg)
152 f := check.conf.Error
154 panic(bailout{}) // report only first error
159 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
167 go116start: span.start,
172 // newErrorf creates a new Error, but does not handle it.
173 func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...interface{}) error {
174 msg := check.sprintf(format, args...)
175 return check.newError(at, code, soft, msg)
178 func (check *Checker) error(at positioner, code errorCode, msg string) {
179 check.err(check.newError(at, code, false, msg))
182 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...interface{}) {
183 check.error(at, code, check.sprintf(format, args...))
186 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...interface{}) {
187 check.err(check.newErrorf(at, code, true, format, args...))
190 func (check *Checker) invalidAST(at positioner, format string, args ...interface{}) {
191 check.errorf(at, 0, "invalid AST: "+format, args...)
194 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...interface{}) {
195 check.errorf(at, code, "invalid argument: "+format, args...)
198 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...interface{}) {
199 check.errorf(at, code, "invalid operation: "+format, args...)
202 // The positioner interface is used to extract the position of type-checker
204 type positioner interface {
208 // posSpan holds a position range along with a highlighted position within that
209 // range. This is used for positioning errors, with pos by convention being the
210 // first position in the source where the error is known to exist, and start
211 // and end defining the full span of syntax being considered when the error was
212 // detected. Invariant: start <= pos < end || start == pos == end.
213 type posSpan struct {
214 start, pos, end token.Pos
217 func (e posSpan) Pos() token.Pos {
221 // inNode creates a posSpan for the given node.
222 // Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
223 // first byte after node within the source).
224 func inNode(node ast.Node, pos token.Pos) posSpan {
225 start, end := node.Pos(), node.End()
227 assert(start <= pos && pos < end)
229 return posSpan{start, pos, end}
232 // atPos wraps a token.Pos to implement the positioner interface.
235 func (s atPos) Pos() token.Pos {
239 // spanOf extracts an error span from the given positioner. By default this is
240 // the trivial span starting and ending at pos, but this span is expanded when
241 // the argument naturally corresponds to a span of source code.
242 func spanOf(at positioner) posSpan {
243 switch x := at.(type) {
245 panic("nil positioner")
250 return posSpan{pos, pos, x.End()}
254 return posSpan{pos, pos, x.expr.End()}
256 return posSpan{token.NoPos, token.NoPos, token.NoPos}
259 return posSpan{pos, pos, pos}
263 // stripAnnotations removes internal (type) annotations from s.
264 func stripAnnotations(s string) string {
265 var b strings.Builder
266 for _, r := range s {
267 // strip #'s and subscript digits
268 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
272 if b.Len() < len(s) {