]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/errors.go
[dev.fuzz] all: merge master (af72ddf) into dev.fuzz
[gostls13.git] / src / go / types / errors.go
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.
4
5 // This file implements various error reporters.
6
7 package types
8
9 import (
10         "errors"
11         "fmt"
12         "go/ast"
13         "go/token"
14         "strconv"
15         "strings"
16 )
17
18 func assert(p bool) {
19         if !p {
20                 panic("assertion failed")
21         }
22 }
23
24 func unreachable() {
25         panic("unreachable")
26 }
27
28 func (check *Checker) qualifier(pkg *Package) string {
29         // Qualify the package unless it's the package being type-checked.
30         if pkg != check.pkg {
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)
35                 }
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)
39                 }
40                 return pkg.name
41         }
42         return ""
43 }
44
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] {
49                 return
50         }
51         check.seenPkgMap[pkg] = true
52
53         forName, ok := check.pkgPathMap[pkg.name]
54         if !ok {
55                 forName = make(map[string]bool)
56                 check.pkgPathMap[pkg.name] = forName
57         }
58         forName[pkg.path] = true
59
60         for _, imp := range pkg.imports {
61                 check.markImports(imp)
62         }
63 }
64
65 func (check *Checker) sprintf(format string, args ...interface{}) string {
66         return sprintf(check.fset, check.qualifier, format, args...)
67 }
68
69 func sprintf(fset *token.FileSet, qf Qualifier, format string, args ...interface{}) string {
70         for i, arg := range args {
71                 switch a := arg.(type) {
72                 case nil:
73                         arg = "<nil>"
74                 case operand:
75                         panic("got operand instead of *operand")
76                 case *operand:
77                         arg = operandString(a, qf)
78                 case token.Pos:
79                         if fset != nil {
80                                 arg = fset.Position(a).String()
81                         }
82                 case ast.Expr:
83                         arg = ExprString(a)
84                 case Object:
85                         arg = ObjectString(a, qf)
86                 case Type:
87                         arg = TypeString(a, qf)
88                 }
89                 args[i] = arg
90         }
91         return fmt.Sprintf(format, args...)
92 }
93
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...),
99         )
100 }
101
102 // dump is only needed for debugging
103 func (check *Checker) dump(format string, args ...interface{}) {
104         fmt.Println(check.sprintf(format, args...))
105 }
106
107 func (check *Checker) err(err error) {
108         if err == nil {
109                 return
110         }
111         var e 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 {
120                 return
121         }
122
123         if isInternal {
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)
131                         e.Pos = span.pos
132                         e.go116start = span.start
133                         e.go116end = span.end
134                 }
135                 err = e
136         }
137
138         if check.firstErr == nil {
139                 check.firstErr = err
140         }
141
142         if trace {
143                 pos := e.Pos
144                 msg := e.Msg
145                 if !isInternal {
146                         msg = err.Error()
147                         pos = token.NoPos
148                 }
149                 check.trace(pos, "ERROR: %s", msg)
150         }
151
152         f := check.conf.Error
153         if f == nil {
154                 panic(bailout{}) // report only first error
155         }
156         f(err)
157 }
158
159 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
160         span := spanOf(at)
161         return Error{
162                 Fset:       check.fset,
163                 Pos:        span.pos,
164                 Msg:        msg,
165                 Soft:       soft,
166                 go116code:  code,
167                 go116start: span.start,
168                 go116end:   span.end,
169         }
170 }
171
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)
176 }
177
178 func (check *Checker) error(at positioner, code errorCode, msg string) {
179         check.err(check.newError(at, code, false, msg))
180 }
181
182 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...interface{}) {
183         check.error(at, code, check.sprintf(format, args...))
184 }
185
186 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...interface{}) {
187         check.err(check.newErrorf(at, code, true, format, args...))
188 }
189
190 func (check *Checker) invalidAST(at positioner, format string, args ...interface{}) {
191         check.errorf(at, 0, "invalid AST: "+format, args...)
192 }
193
194 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...interface{}) {
195         check.errorf(at, code, "invalid argument: "+format, args...)
196 }
197
198 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...interface{}) {
199         check.errorf(at, code, "invalid operation: "+format, args...)
200 }
201
202 // The positioner interface is used to extract the position of type-checker
203 // errors.
204 type positioner interface {
205         Pos() token.Pos
206 }
207
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
215 }
216
217 func (e posSpan) Pos() token.Pos {
218         return e.pos
219 }
220
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()
226         if debug {
227                 assert(start <= pos && pos < end)
228         }
229         return posSpan{start, pos, end}
230 }
231
232 // atPos wraps a token.Pos to implement the positioner interface.
233 type atPos token.Pos
234
235 func (s atPos) Pos() token.Pos {
236         return token.Pos(s)
237 }
238
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) {
244         case nil:
245                 panic("nil positioner")
246         case posSpan:
247                 return x
248         case ast.Node:
249                 pos := x.Pos()
250                 return posSpan{pos, pos, x.End()}
251         case *operand:
252                 if x.expr != nil {
253                         pos := x.Pos()
254                         return posSpan{pos, pos, x.expr.End()}
255                 }
256                 return posSpan{token.NoPos, token.NoPos, token.NoPos}
257         default:
258                 pos := at.Pos()
259                 return posSpan{pos, pos, pos}
260         }
261 }
262
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
269                         b.WriteRune(r)
270                 }
271         }
272         if b.Len() < len(s) {
273                 return b.String()
274         }
275         return s
276 }