2 // Copyright 2012 The Go Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
6 // This file implements various error reporters.
11 "cmd/compile/internal/syntax"
17 func unimplemented() {
18 panic("unimplemented")
23 panic("assertion failed")
31 func (check *Checker) qualifier(pkg *Package) string {
32 // Qualify the package unless it's the package being type-checked.
34 // If the same package name was used by multiple packages, display the full path.
35 if check.pkgCnt[pkg.name] > 1 {
36 return strconv.Quote(pkg.path)
43 func (check *Checker) sprintf(format string, args ...interface{}) string {
44 for i, arg := range args {
45 switch a := arg.(type) {
49 panic("internal error: should always pass *operand")
51 arg = operandString(a, check.qualifier)
57 arg = ObjectString(a, check.qualifier)
59 arg = TypeString(a, check.qualifier)
63 return fmt.Sprintf(format, args...)
66 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
67 fmt.Printf("%s:\t%s%s\n",
69 strings.Repeat(". ", check.indent),
70 check.sprintf(format, args...),
74 // dump is only needed for debugging
75 func (check *Checker) dump(format string, args ...interface{}) {
76 fmt.Println(check.sprintf(format, args...))
79 func (check *Checker) err(pos syntax.Pos, msg string, soft bool) {
80 // Cheap trick: Don't report errors with messages containing
81 // "invalid operand" or "invalid type" as those tend to be
82 // follow-on errors which don't add useful information. Only
83 // exclude them if these strings are not at the beginning,
84 // and only if we have at least one error already reported.
85 if check.firstErr != nil && (strings.Index(msg, "invalid operand") > 0 || strings.Index(msg, "invalid type") > 0) {
89 err := Error{pos, stripAnnotations(msg), msg, soft}
90 if check.firstErr == nil {
95 check.trace(pos, "ERROR: %s", msg)
100 panic(bailout{}) // report only first error
105 type poser interface {
109 func (check *Checker) error(at poser, msg string) {
110 check.err(posFor(at), msg, false)
113 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
114 check.err(posFor(at), check.sprintf(format, args...), false)
117 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
118 check.err(posFor(at), check.sprintf(format, args...), true)
121 func (check *Checker) invalidASTf(at poser, format string, args ...interface{}) {
122 check.errorf(at, "invalid AST: "+format, args...)
125 func (check *Checker) invalidArgf(at poser, format string, args ...interface{}) {
126 check.errorf(at, "invalid argument: "+format, args...)
129 func (check *Checker) invalidOpf(at poser, format string, args ...interface{}) {
130 check.errorf(at, "invalid operation: "+format, args...)
133 // posFor reports the left (= start) position of at.
134 func posFor(at poser) syntax.Pos {
135 switch x := at.(type) {
138 return startPos(x.expr)
146 // stripAnnotations removes internal (type) annotations from s.
147 func stripAnnotations(s string) string {
148 var b strings.Builder
149 for _, r := range s {
150 // strip #'s and subscript digits
151 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
155 if b.Len() < len(s) {