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"
17 func unimplemented() {
18 panic("unimplemented")
23 panic("assertion failed")
31 // An error_ represents a type-checking error.
32 // To report an error_, call Checker.report.
35 soft bool // TODO(gri) eventually determine this from an error code
38 // An errorDesc describes part of a type-checking error.
39 type errorDesc struct {
45 func (err *error_) empty() bool {
46 return err.desc == nil
49 func (err *error_) pos() syntax.Pos {
53 return err.desc[0].pos
56 func (err *error_) msg(qf Qualifier) string {
61 for i := range err.desc {
64 fmt.Fprint(&buf, "\n\t")
66 fmt.Fprintf(&buf, "%s: ", p.pos)
69 buf.WriteString(sprintf(qf, false, p.format, p.args...))
74 // String is for testing.
75 func (err *error_) String() string {
79 return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
82 // errorf adds formatted error information to err.
83 // It may be called multiple times to provide additional information.
84 func (err *error_) errorf(at poser, format string, args ...interface{}) {
85 err.desc = append(err.desc, errorDesc{posFor(at), format, args})
88 func sprintf(qf Qualifier, debug bool, format string, args ...interface{}) string {
89 for i, arg := range args {
90 switch a := arg.(type) {
94 panic("got operand instead of *operand")
96 arg = operandString(a, qf)
100 arg = syntax.String(a)
104 for i, x := range a {
106 buf.WriteString(", ")
108 buf.WriteString(syntax.String(x))
113 arg = ObjectString(a, qf)
115 arg = typeString(a, qf, debug)
119 for i, x := range a {
121 buf.WriteString(", ")
123 buf.WriteString(typeString(x, qf, debug))
130 for i, x := range a {
132 buf.WriteString(", ")
134 buf.WriteString(typeString(x, qf, debug)) // use typeString so we get subscripts when debugging
141 return fmt.Sprintf(format, args...)
144 func (check *Checker) qualifier(pkg *Package) string {
145 // Qualify the package unless it's the package being type-checked.
146 if pkg != check.pkg {
147 if check.pkgPathMap == nil {
148 check.pkgPathMap = make(map[string]map[string]bool)
149 check.seenPkgMap = make(map[*Package]bool)
150 check.markImports(check.pkg)
152 // If the same package name was used by multiple packages, display the full path.
153 if len(check.pkgPathMap[pkg.name]) > 1 {
154 return strconv.Quote(pkg.path)
161 // markImports recursively walks pkg and its imports, to record unique import
162 // paths in pkgPathMap.
163 func (check *Checker) markImports(pkg *Package) {
164 if check.seenPkgMap[pkg] {
167 check.seenPkgMap[pkg] = true
169 forName, ok := check.pkgPathMap[pkg.name]
171 forName = make(map[string]bool)
172 check.pkgPathMap[pkg.name] = forName
174 forName[pkg.path] = true
176 for _, imp := range pkg.imports {
177 check.markImports(imp)
182 func (check *Checker) sprintf(format string, args ...interface{}) string {
187 return sprintf(qf, false, format, args...)
190 func (check *Checker) report(err *error_) {
192 panic("no error to report")
194 check.err(err.pos(), err.msg(check.qualifier), err.soft)
197 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
198 fmt.Printf("%s:\t%s%s\n",
200 strings.Repeat(". ", check.indent),
201 sprintf(check.qualifier, true, format, args...),
205 // dump is only needed for debugging
206 func (check *Checker) dump(format string, args ...interface{}) {
207 fmt.Println(sprintf(check.qualifier, true, format, args...))
210 func (check *Checker) err(at poser, msg string, soft bool) {
211 // Cheap trick: Don't report errors with messages containing
212 // "invalid operand" or "invalid type" as those tend to be
213 // follow-on errors which don't add useful information. Only
214 // exclude them if these strings are not at the beginning,
215 // and only if we have at least one error already reported.
216 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
222 // If we are encountering an error while evaluating an inherited
223 // constant initialization expression, pos is the position of in
224 // the original expression, and not of the currently declared
225 // constant identifier. Use the provided errpos instead.
226 // TODO(gri) We may also want to augment the error message and
227 // refer to the position (pos) in the original expression.
228 if check.errpos.IsKnown() {
229 assert(check.iota != nil)
233 err := Error{pos, stripAnnotations(msg), msg, soft}
234 if check.firstErr == nil {
238 if check.conf.Trace {
239 check.trace(pos, "ERROR: %s", msg)
242 f := check.conf.Error
244 panic(bailout{}) // report only first error
250 invalidAST = "invalid AST: "
251 invalidArg = "invalid argument: "
252 invalidOp = "invalid operation: "
255 type poser interface {
259 func (check *Checker) error(at poser, msg string) {
260 check.err(at, msg, false)
263 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
264 check.err(at, check.sprintf(format, args...), false)
267 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
268 check.err(at, check.sprintf(format, args...), true)
271 func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
272 msg := check.sprintf(format, args...)
273 if check.conf.CompilerErrorMessages {
274 msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
276 msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
278 check.err(at, msg, true)
281 // posFor reports the left (= start) position of at.
282 func posFor(at poser) syntax.Pos {
283 switch x := at.(type) {
286 return syntax.StartPos(x.expr)
289 return syntax.StartPos(x)
294 // stripAnnotations removes internal (type) annotations from s.
295 func stripAnnotations(s string) string {
296 // Would like to use strings.Builder but it's not available in Go 1.4.
298 for _, r := range s {
299 // strip #'s and subscript digits
300 if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
304 if b.Len() < len(s) {