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 the same package name was used by multiple packages, display the full path.
32 if check.pkgCnt[pkg.name] > 1 {
33 return strconv.Quote(pkg.path)
40 func (check *Checker) sprintf(format string, args ...interface{}) string {
41 for i, arg := range args {
42 switch a := arg.(type) {
46 panic("internal error: should always pass *operand")
48 arg = operandString(a, check.qualifier)
50 arg = check.fset.Position(a).String()
54 arg = ObjectString(a, check.qualifier)
56 arg = TypeString(a, check.qualifier)
60 return fmt.Sprintf(format, args...)
63 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
64 fmt.Printf("%s:\t%s%s\n",
65 check.fset.Position(pos),
66 strings.Repeat(". ", check.indent),
67 check.sprintf(format, args...),
71 // dump is only needed for debugging
72 func (check *Checker) dump(format string, args ...interface{}) {
73 fmt.Println(check.sprintf(format, args...))
76 func (check *Checker) err(err error) {
81 isInternal := errors.As(err, &e)
82 // Cheap trick: Don't report errors with messages containing
83 // "invalid operand" or "invalid type" as those tend to be
84 // follow-on errors which don't add useful information. Only
85 // exclude them if these strings are not at the beginning,
86 // and only if we have at least one error already reported.
87 isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
88 if check.firstErr != nil && isInvalidErr {
93 e.Msg = stripAnnotations(e.Msg)
94 if check.errpos != nil {
95 // If we have an internal error and the errpos override is set, use it to
96 // augment our error positioning.
97 // TODO(rFindley) we may also want to augment the error message and refer
98 // to the position (pos) in the original expression.
99 span := spanOf(check.errpos)
101 e.go116start = span.start
102 e.go116end = span.end
107 if check.firstErr == nil {
118 check.trace(pos, "ERROR: %s", msg)
121 f := check.conf.Error
123 panic(bailout{}) // report only first error
128 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
136 go116start: span.start,
141 // newErrorf creates a new Error, but does not handle it.
142 func (check *Checker) newErrorf(at positioner, code errorCode, soft bool, format string, args ...interface{}) error {
143 msg := check.sprintf(format, args...)
144 return check.newError(at, code, soft, msg)
147 func (check *Checker) error(at positioner, code errorCode, msg string) {
148 check.err(check.newError(at, code, false, msg))
151 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...interface{}) {
152 check.error(at, code, check.sprintf(format, args...))
155 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...interface{}) {
156 check.err(check.newErrorf(at, code, true, format, args...))
159 func (check *Checker) invalidAST(at positioner, format string, args ...interface{}) {
160 check.errorf(at, 0, "invalid AST: "+format, args...)
163 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...interface{}) {
164 check.errorf(at, code, "invalid argument: "+format, args...)
167 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...interface{}) {
168 check.errorf(at, code, "invalid operation: "+format, args...)
171 // The positioner interface is used to extract the position of type-checker
173 type positioner interface {
177 // posSpan holds a position range along with a highlighted position within that
178 // range. This is used for positioning errors, with pos by convention being the
179 // first position in the source where the error is known to exist, and start
180 // and end defining the full span of syntax being considered when the error was
181 // detected. Invariant: start <= pos < end || start == pos == end.
182 type posSpan struct {
183 start, pos, end token.Pos
186 func (e posSpan) Pos() token.Pos {
190 // inNode creates a posSpan for the given node.
191 // Invariant: node.Pos() <= pos < node.End() (node.End() is the position of the
192 // first byte after node within the source).
193 func inNode(node ast.Node, pos token.Pos) posSpan {
194 start, end := node.Pos(), node.End()
196 assert(start <= pos && pos < end)
198 return posSpan{start, pos, end}
201 // atPos wraps a token.Pos to implement the positioner interface.
204 func (s atPos) Pos() token.Pos {
208 // spanOf extracts an error span from the given positioner. By default this is
209 // the trivial span starting and ending at pos, but this span is expanded when
210 // the argument naturally corresponds to a span of source code.
211 func spanOf(at positioner) posSpan {
212 switch x := at.(type) {
214 panic("internal error: nil")
219 return posSpan{pos, pos, x.End()}
223 return posSpan{pos, pos, x.expr.End()}
225 return posSpan{token.NoPos, token.NoPos, token.NoPos}
228 return posSpan{pos, pos, pos}
232 // stripAnnotations removes internal (type) annotations from s.
233 func stripAnnotations(s string) string {
234 var b strings.Builder
235 for _, r := range s {
236 // strip #'s and subscript digits
237 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
241 if b.Len() < len(s) {