]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/errors.go
[dev.typeparams] all: merge dev.regabi (5e4a0cd) into dev.typeparams
[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 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)
34                 }
35                 return pkg.name
36         }
37         return ""
38 }
39
40 func (check *Checker) sprintf(format string, args ...interface{}) string {
41         for i, arg := range args {
42                 switch a := arg.(type) {
43                 case nil:
44                         arg = "<nil>"
45                 case operand:
46                         panic("internal error: should always pass *operand")
47                 case *operand:
48                         arg = operandString(a, check.qualifier)
49                 case token.Pos:
50                         arg = check.fset.Position(a).String()
51                 case ast.Expr:
52                         arg = ExprString(a)
53                 case Object:
54                         arg = ObjectString(a, check.qualifier)
55                 case Type:
56                         arg = TypeString(a, check.qualifier)
57                 }
58                 args[i] = arg
59         }
60         return fmt.Sprintf(format, args...)
61 }
62
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...),
68         )
69 }
70
71 // dump is only needed for debugging
72 func (check *Checker) dump(format string, args ...interface{}) {
73         fmt.Println(check.sprintf(format, args...))
74 }
75
76 func (check *Checker) err(err error) {
77         if err == nil {
78                 return
79         }
80         var e 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 {
89                 return
90         }
91
92         if isInternal {
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)
100                         e.Pos = span.pos
101                         e.go116start = span.start
102                         e.go116end = span.end
103                 }
104                 err = e
105         }
106
107         if check.firstErr == nil {
108                 check.firstErr = err
109         }
110
111         if trace {
112                 pos := e.Pos
113                 msg := e.Msg
114                 if !isInternal {
115                         msg = err.Error()
116                         pos = token.NoPos
117                 }
118                 check.trace(pos, "ERROR: %s", msg)
119         }
120
121         f := check.conf.Error
122         if f == nil {
123                 panic(bailout{}) // report only first error
124         }
125         f(err)
126 }
127
128 func (check *Checker) newError(at positioner, code errorCode, soft bool, msg string) error {
129         span := spanOf(at)
130         return Error{
131                 Fset:       check.fset,
132                 Pos:        span.pos,
133                 Msg:        msg,
134                 Soft:       soft,
135                 go116code:  code,
136                 go116start: span.start,
137                 go116end:   span.end,
138         }
139 }
140
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)
145 }
146
147 func (check *Checker) error(at positioner, code errorCode, msg string) {
148         check.err(check.newError(at, code, false, msg))
149 }
150
151 func (check *Checker) errorf(at positioner, code errorCode, format string, args ...interface{}) {
152         check.error(at, code, check.sprintf(format, args...))
153 }
154
155 func (check *Checker) softErrorf(at positioner, code errorCode, format string, args ...interface{}) {
156         check.err(check.newErrorf(at, code, true, format, args...))
157 }
158
159 func (check *Checker) invalidAST(at positioner, format string, args ...interface{}) {
160         check.errorf(at, 0, "invalid AST: "+format, args...)
161 }
162
163 func (check *Checker) invalidArg(at positioner, code errorCode, format string, args ...interface{}) {
164         check.errorf(at, code, "invalid argument: "+format, args...)
165 }
166
167 func (check *Checker) invalidOp(at positioner, code errorCode, format string, args ...interface{}) {
168         check.errorf(at, code, "invalid operation: "+format, args...)
169 }
170
171 // The positioner interface is used to extract the position of type-checker
172 // errors.
173 type positioner interface {
174         Pos() token.Pos
175 }
176
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
184 }
185
186 func (e posSpan) Pos() token.Pos {
187         return e.pos
188 }
189
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()
195         if debug {
196                 assert(start <= pos && pos < end)
197         }
198         return posSpan{start, pos, end}
199 }
200
201 // atPos wraps a token.Pos to implement the positioner interface.
202 type atPos token.Pos
203
204 func (s atPos) Pos() token.Pos {
205         return token.Pos(s)
206 }
207
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) {
213         case nil:
214                 panic("internal error: nil")
215         case posSpan:
216                 return x
217         case ast.Node:
218                 pos := x.Pos()
219                 return posSpan{pos, pos, x.End()}
220         case *operand:
221                 if x.expr != nil {
222                         pos := x.Pos()
223                         return posSpan{pos, pos, x.expr.End()}
224                 }
225                 return posSpan{token.NoPos, token.NoPos, token.NoPos}
226         default:
227                 pos := at.Pos()
228                 return posSpan{pos, pos, pos}
229         }
230 }
231
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
238                         b.WriteRune(r)
239                 }
240         }
241         if b.Len() < len(s) {
242                 return b.String()
243         }
244         return s
245 }