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.Fprintf(&buf, "\n\t%s: ", p.pos)
66 buf.WriteString(sprintf(qf, p.format, p.args...))
71 // String is for testing.
72 func (err *error_) String() string {
76 return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
79 // errorf adds formatted error information to err.
80 // It may be called multiple times to provide additional information.
81 func (err *error_) errorf(at poser, format string, args ...interface{}) {
82 err.desc = append(err.desc, errorDesc{posFor(at), format, args})
85 func sprintf(qf Qualifier, format string, args ...interface{}) string {
86 for i, arg := range args {
87 switch a := arg.(type) {
91 panic("internal error: should always pass *operand")
93 arg = operandString(a, qf)
97 arg = syntax.String(a)
99 arg = ObjectString(a, qf)
101 arg = TypeString(a, qf)
105 return fmt.Sprintf(format, args...)
108 func (check *Checker) qualifier(pkg *Package) string {
109 // Qualify the package unless it's the package being type-checked.
110 if pkg != check.pkg {
111 // If the same package name was used by multiple packages, display the full path.
112 if check.pkgCnt[pkg.name] > 1 {
113 return strconv.Quote(pkg.path)
120 func (check *Checker) sprintf(format string, args ...interface{}) string {
121 return sprintf(check.qualifier, format, args...)
124 func (check *Checker) report(err *error_) {
126 panic("internal error: reporting no error")
128 check.err(err.pos(), err.msg(check.qualifier), err.soft)
131 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
132 fmt.Printf("%s:\t%s%s\n",
134 strings.Repeat(". ", check.indent),
135 check.sprintf(format, args...),
139 // dump is only needed for debugging
140 func (check *Checker) dump(format string, args ...interface{}) {
141 fmt.Println(check.sprintf(format, args...))
144 func (check *Checker) err(at poser, msg string, soft bool) {
145 // Cheap trick: Don't report errors with messages containing
146 // "invalid operand" or "invalid type" as those tend to be
147 // follow-on errors which don't add useful information. Only
148 // exclude them if these strings are not at the beginning,
149 // and only if we have at least one error already reported.
150 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
156 // If we are encountering an error while evaluating an inherited
157 // constant initialization expression, pos is the position of in
158 // the original expression, and not of the currently declared
159 // constant identifier. Use the provided errpos instead.
160 // TODO(gri) We may also want to augment the error message and
161 // refer to the position (pos) in the original expression.
162 if check.errpos.IsKnown() {
163 assert(check.iota != nil)
167 err := Error{pos, stripAnnotations(msg), msg, soft}
168 if check.firstErr == nil {
172 if check.conf.Trace {
173 check.trace(pos, "ERROR: %s", msg)
176 f := check.conf.Error
178 panic(bailout{}) // report only first error
184 invalidAST = "invalid AST: "
185 invalidArg = "invalid argument: "
186 invalidOp = "invalid operation: "
189 type poser interface {
193 func (check *Checker) error(at poser, msg string) {
194 check.err(at, msg, false)
197 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
198 check.err(at, check.sprintf(format, args...), false)
201 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
202 check.err(at, check.sprintf(format, args...), true)
205 // posFor reports the left (= start) position of at.
206 func posFor(at poser) syntax.Pos {
207 switch x := at.(type) {
210 return startPos(x.expr)
218 // stripAnnotations removes internal (type) annotations from s.
219 func stripAnnotations(s string) string {
220 // Would like to use strings.Builder but it's not available in Go 1.4.
222 for _, r := range s {
223 // strip #'s and subscript digits
224 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
228 if b.Len() < len(s) {