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.
11 "cmd/compile/internal/syntax"
18 func unimplemented() {
19 panic("unimplemented")
24 msg := "assertion failed"
25 // Include information about the assertion location. Due to panic recovery,
26 // this location is otherwise buried in the middle of the panicking stack.
27 if _, file, line, ok := runtime.Caller(1); ok {
28 msg = fmt.Sprintf("%s:%d: %s", file, line, msg)
38 // An error_ represents a type-checking error.
39 // To report an error_, call Checker.report.
42 soft bool // TODO(gri) eventually determine this from an error code
45 // An errorDesc describes part of a type-checking error.
46 type errorDesc struct {
52 func (err *error_) empty() bool {
53 return err.desc == nil
56 func (err *error_) pos() syntax.Pos {
60 return err.desc[0].pos
63 func (err *error_) msg(qf Qualifier) string {
68 for i := range err.desc {
71 fmt.Fprint(&buf, "\n\t")
73 fmt.Fprintf(&buf, "%s: ", p.pos)
76 buf.WriteString(sprintf(qf, false, p.format, p.args...))
81 // String is for testing.
82 func (err *error_) String() string {
86 return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
89 // errorf adds formatted error information to err.
90 // It may be called multiple times to provide additional information.
91 func (err *error_) errorf(at poser, format string, args ...interface{}) {
92 err.desc = append(err.desc, errorDesc{posFor(at), format, args})
95 func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) string {
96 for i, arg := range args {
97 switch a := arg.(type) {
101 panic("got operand instead of *operand")
103 arg = operandString(a, qf)
107 arg = syntax.String(a)
111 for i, x := range a {
113 buf.WriteString(", ")
115 buf.WriteString(syntax.String(x))
120 arg = ObjectString(a, qf)
122 arg = typeString(a, qf, debug)
126 for i, x := range a {
128 buf.WriteString(", ")
130 buf.WriteString(typeString(x, qf, debug))
137 for i, x := range a {
139 buf.WriteString(", ")
141 buf.WriteString(typeString(x, qf, debug)) // use typeString so we get subscripts when debugging
148 return fmt.Sprintf(format, args...)
151 func (check *Checker) qualifier(pkg *Package) string {
152 // Qualify the package unless it's the package being type-checked.
153 if pkg != check.pkg {
154 if check.pkgPathMap == nil {
155 check.pkgPathMap = make(map[string]map[string]bool)
156 check.seenPkgMap = make(map[*Package]bool)
157 check.markImports(check.pkg)
159 // If the same package name was used by multiple packages, display the full path.
160 if len(check.pkgPathMap[pkg.name]) > 1 {
161 return strconv.Quote(pkg.path)
168 // markImports recursively walks pkg and its imports, to record unique import
169 // paths in pkgPathMap.
170 func (check *Checker) markImports(pkg *Package) {
171 if check.seenPkgMap[pkg] {
174 check.seenPkgMap[pkg] = true
176 forName, ok := check.pkgPathMap[pkg.name]
178 forName = make(map[string]bool)
179 check.pkgPathMap[pkg.name] = forName
181 forName[pkg.path] = true
183 for _, imp := range pkg.imports {
184 check.markImports(imp)
189 func (check *Checker) sprintf(format string, args ...interface{}) string {
194 return sprintf(qf, false, format, args...)
197 func (check *Checker) report(err *error_) {
199 panic("no error to report")
201 check.err(err.pos(), err.msg(check.qualifier), err.soft)
204 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
205 fmt.Printf("%s:\t%s%s\n",
207 strings.Repeat(". ", check.indent),
208 sprintf(check.qualifier, true, format, args...),
212 // dump is only needed for debugging
213 func (check *Checker) dump(format string, args ...interface{}) {
214 fmt.Println(sprintf(check.qualifier, true, format, args...))
217 func (check *Checker) err(at poser, msg string, soft bool) {
218 // Cheap trick: Don't report errors with messages containing
219 // "invalid operand" or "invalid type" as those tend to be
220 // follow-on errors which don't add useful information. Only
221 // exclude them if these strings are not at the beginning,
222 // and only if we have at least one error already reported.
223 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
229 // If we are encountering an error while evaluating an inherited
230 // constant initialization expression, pos is the position of in
231 // the original expression, and not of the currently declared
232 // constant identifier. Use the provided errpos instead.
233 // TODO(gri) We may also want to augment the error message and
234 // refer to the position (pos) in the original expression.
235 if check.errpos.IsKnown() {
236 assert(check.iota != nil)
240 err := Error{pos, stripAnnotations(msg), msg, soft}
241 if check.firstErr == nil {
245 if check.conf.Trace {
246 check.trace(pos, "ERROR: %s", msg)
249 f := check.conf.Error
251 panic(bailout{}) // report only first error
257 invalidAST = "invalid AST: "
258 invalidArg = "invalid argument: "
259 invalidOp = "invalid operation: "
262 type poser interface {
266 func (check *Checker) error(at poser, msg string) {
267 check.err(at, msg, false)
270 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
271 check.err(at, check.sprintf(format, args...), false)
274 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
275 check.err(at, check.sprintf(format, args...), true)
278 func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
279 msg := check.sprintf(format, args...)
280 if check.conf.CompilerErrorMessages {
281 msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
283 msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
285 check.err(at, msg, true)
288 // posFor reports the left (= start) position of at.
289 func posFor(at poser) syntax.Pos {
290 switch x := at.(type) {
293 return syntax.StartPos(x.expr)
296 return syntax.StartPos(x)
301 // stripAnnotations removes internal (type) annotations from s.
302 func stripAnnotations(s string) string {
303 // Would like to use strings.Builder but it's not available in Go 1.4.
305 for _, r := range s {
306 // strip #'s and subscript digits
307 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
311 if b.Len() < len(s) {