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.
10 "cmd/compile/internal/syntax"
17 func unimplemented() {
18 panic("unimplemented")
23 msg := "assertion failed"
24 // Include information about the assertion location. Due to panic recovery,
25 // this location is otherwise buried in the middle of the panicking stack.
26 if _, file, line, ok := runtime.Caller(1); ok {
27 msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
37 // An error_ represents a type-checking error.
38 // To report an error_, call Checker.report.
41 soft bool // TODO(gri) eventually determine this from an error code
44 // An errorDesc describes part of a type-checking error.
45 type errorDesc struct {
51 func (err *error_) empty() bool {
52 return err.desc == nil
55 func (err *error_) pos() syntax.Pos {
59 return err.desc[0].pos
62 func (err *error_) msg(qf Qualifier) string {
66 var buf strings.Builder
67 for i := range err.desc {
70 fmt.Fprint(&buf, "\n\t")
72 fmt.Fprintf(&buf, "%s: ", p.pos)
75 buf.WriteString(sprintf(qf, false, p.format, p.args...))
80 // String is for testing.
81 func (err *error_) String() string {
85 return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
88 // errorf adds formatted error information to err.
89 // It may be called multiple times to provide additional information.
90 func (err *error_) errorf(at poser, format string, args ...interface{}) {
91 err.desc = append(err.desc, errorDesc{posFor(at), format, args})
94 func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) string {
95 for i, arg := range args {
96 switch a := arg.(type) {
100 panic("got operand instead of *operand")
102 arg = operandString(a, qf)
106 arg = syntax.String(a)
108 var buf strings.Builder
110 for i, x := range a {
112 buf.WriteString(", ")
114 buf.WriteString(syntax.String(x))
119 arg = ObjectString(a, qf)
121 arg = typeString(a, qf, debug)
123 var buf strings.Builder
125 for i, x := range a {
127 buf.WriteString(", ")
129 buf.WriteString(typeString(x, qf, debug))
134 var buf strings.Builder
136 for i, x := range a {
138 buf.WriteString(", ")
140 buf.WriteString(typeString(x, qf, debug)) // use typeString so we get subscripts when debugging
147 return fmt.Sprintf(format, args...)
150 func (check *Checker) qualifier(pkg *Package) string {
151 // Qualify the package unless it's the package being type-checked.
152 if pkg != check.pkg {
153 if check.pkgPathMap == nil {
154 check.pkgPathMap = make(map[string]map[string]bool)
155 check.seenPkgMap = make(map[*Package]bool)
156 check.markImports(check.pkg)
158 // If the same package name was used by multiple packages, display the full path.
159 if len(check.pkgPathMap[pkg.name]) > 1 {
160 return strconv.Quote(pkg.path)
167 // markImports recursively walks pkg and its imports, to record unique import
168 // paths in pkgPathMap.
169 func (check *Checker) markImports(pkg *Package) {
170 if check.seenPkgMap[pkg] {
173 check.seenPkgMap[pkg] = true
175 forName, ok := check.pkgPathMap[pkg.name]
177 forName = make(map[string]bool)
178 check.pkgPathMap[pkg.name] = forName
180 forName[pkg.path] = true
182 for _, imp := range pkg.imports {
183 check.markImports(imp)
188 func (check *Checker) sprintf(format string, args ...interface{}) string {
193 return sprintf(qf, false, format, args...)
196 func (check *Checker) report(err *error_) {
198 panic("no error to report")
200 check.err(err.pos(), err.msg(check.qualifier), err.soft)
203 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
204 fmt.Printf("%s:\t%s%s\n",
206 strings.Repeat(". ", check.indent),
207 sprintf(check.qualifier, true, format, args...),
211 // dump is only needed for debugging
212 func (check *Checker) dump(format string, args ...interface{}) {
213 fmt.Println(sprintf(check.qualifier, true, format, args...))
216 func (check *Checker) err(at poser, msg string, soft bool) {
217 // Cheap trick: Don't report errors with messages containing
218 // "invalid operand" or "invalid type" as those tend to be
219 // follow-on errors which don't add useful information. Only
220 // exclude them if these strings are not at the beginning,
221 // and only if we have at least one error already reported.
222 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
228 // If we are encountering an error while evaluating an inherited
229 // constant initialization expression, pos is the position of in
230 // the original expression, and not of the currently declared
231 // constant identifier. Use the provided errpos instead.
232 // TODO(gri) We may also want to augment the error message and
233 // refer to the position (pos) in the original expression.
234 if check.errpos.IsKnown() {
235 assert(check.iota != nil)
239 err := Error{pos, stripAnnotations(msg), msg, soft}
240 if check.firstErr == nil {
244 if check.conf.Trace {
245 check.trace(pos, "ERROR: %s", msg)
248 f := check.conf.Error
250 panic(bailout{}) // report only first error
256 invalidAST = "invalid AST: "
257 invalidArg = "invalid argument: "
258 invalidOp = "invalid operation: "
261 type poser interface {
265 func (check *Checker) error(at poser, msg string) {
266 check.err(at, msg, false)
269 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
270 check.err(at, check.sprintf(format, args...), false)
273 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
274 check.err(at, check.sprintf(format, args...), true)
277 func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
278 msg := check.sprintf(format, args...)
279 if check.conf.CompilerErrorMessages {
280 msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
282 msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
284 check.err(at, msg, true)
287 // posFor reports the left (= start) position of at.
288 func posFor(at poser) syntax.Pos {
289 switch x := at.(type) {
292 return syntax.StartPos(x.expr)
295 return syntax.StartPos(x)
300 // stripAnnotations removes internal (type) annotations from s.
301 func stripAnnotations(s string) string {
302 var buf strings.Builder
303 for _, r := range s {
304 // strip #'s and subscript digits
305 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
309 if buf.Len() < len(s) {