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("got operand instead of *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 check.pkgPathMap == nil {
112 check.pkgPathMap = make(map[string]map[string]bool)
113 check.seenPkgMap = make(map[*Package]bool)
114 check.markImports(check.pkg)
116 // If the same package name was used by multiple packages, display the full path.
117 if len(check.pkgPathMap[pkg.name]) > 1 {
118 return strconv.Quote(pkg.path)
125 // markImports recursively walks pkg and its imports, to record unique import
126 // paths in pkgPathMap.
127 func (check *Checker) markImports(pkg *Package) {
128 if check.seenPkgMap[pkg] {
131 check.seenPkgMap[pkg] = true
133 forName, ok := check.pkgPathMap[pkg.name]
135 forName = make(map[string]bool)
136 check.pkgPathMap[pkg.name] = forName
138 forName[pkg.path] = true
140 for _, imp := range pkg.imports {
141 check.markImports(imp)
145 func (check *Checker) sprintf(format string, args ...interface{}) string {
146 return sprintf(check.qualifier, format, args...)
149 func (check *Checker) report(err *error_) {
151 panic("no error to report")
153 check.err(err.pos(), err.msg(check.qualifier), err.soft)
156 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
157 fmt.Printf("%s:\t%s%s\n",
159 strings.Repeat(". ", check.indent),
160 check.sprintf(format, args...),
164 // dump is only needed for debugging
165 func (check *Checker) dump(format string, args ...interface{}) {
166 fmt.Println(check.sprintf(format, args...))
169 func (check *Checker) err(at poser, msg string, soft bool) {
170 // Cheap trick: Don't report errors with messages containing
171 // "invalid operand" or "invalid type" as those tend to be
172 // follow-on errors which don't add useful information. Only
173 // exclude them if these strings are not at the beginning,
174 // and only if we have at least one error already reported.
175 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
181 // If we are encountering an error while evaluating an inherited
182 // constant initialization expression, pos is the position of in
183 // the original expression, and not of the currently declared
184 // constant identifier. Use the provided errpos instead.
185 // TODO(gri) We may also want to augment the error message and
186 // refer to the position (pos) in the original expression.
187 if check.errpos.IsKnown() {
188 assert(check.iota != nil)
192 err := Error{pos, stripAnnotations(msg), msg, soft}
193 if check.firstErr == nil {
197 if check.conf.Trace {
198 check.trace(pos, "ERROR: %s", msg)
201 f := check.conf.Error
203 panic(bailout{}) // report only first error
209 invalidAST = "invalid AST: "
210 invalidArg = "invalid argument: "
211 invalidOp = "invalid operation: "
214 type poser interface {
218 func (check *Checker) error(at poser, msg string) {
219 check.err(at, msg, false)
222 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
223 check.err(at, check.sprintf(format, args...), false)
226 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
227 check.err(at, check.sprintf(format, args...), true)
230 // posFor reports the left (= start) position of at.
231 func posFor(at poser) syntax.Pos {
232 switch x := at.(type) {
235 return syntax.StartPos(x.expr)
238 return syntax.StartPos(x)
243 // stripAnnotations removes internal (type) annotations from s.
244 func stripAnnotations(s string) string {
245 // Would like to use strings.Builder but it's not available in Go 1.4.
247 for _, r := range s {
248 // strip #'s and subscript digits
249 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
253 if b.Len() < len(s) {