]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/errors.go
Merge "all: REVERSE MERGE dev.typeparams (4d3cc84) into master"
[gostls13.git] / src / cmd / compile / internal / types2 / errors.go
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.
4
5 // This file implements various error reporters.
6
7 package types2
8
9 import (
10         "bytes"
11         "cmd/compile/internal/syntax"
12         "fmt"
13         "strconv"
14         "strings"
15 )
16
17 func unimplemented() {
18         panic("unimplemented")
19 }
20
21 func assert(p bool) {
22         if !p {
23                 panic("assertion failed")
24         }
25 }
26
27 func unreachable() {
28         panic("unreachable")
29 }
30
31 // An error_ represents a type-checking error.
32 // To report an error_, call Checker.report.
33 type error_ struct {
34         desc []errorDesc
35         soft bool // TODO(gri) eventually determine this from an error code
36 }
37
38 // An errorDesc describes part of a type-checking error.
39 type errorDesc struct {
40         pos    syntax.Pos
41         format string
42         args   []interface{}
43 }
44
45 func (err *error_) empty() bool {
46         return err.desc == nil
47 }
48
49 func (err *error_) pos() syntax.Pos {
50         if err.empty() {
51                 return nopos
52         }
53         return err.desc[0].pos
54 }
55
56 func (err *error_) msg(qf Qualifier) string {
57         if err.empty() {
58                 return "no error"
59         }
60         var buf bytes.Buffer
61         for i := range err.desc {
62                 p := &err.desc[i]
63                 if i > 0 {
64                         fmt.Fprintf(&buf, "\n\t%s: ", p.pos)
65                 }
66                 buf.WriteString(sprintf(qf, p.format, p.args...))
67         }
68         return buf.String()
69 }
70
71 // String is for testing.
72 func (err *error_) String() string {
73         if err.empty() {
74                 return "no error"
75         }
76         return fmt.Sprintf("%s: %s", err.pos(), err.msg(nil))
77 }
78
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})
83 }
84
85 func sprintf(qf Qualifier, format string, args ...interface{}) string {
86         for i, arg := range args {
87                 switch a := arg.(type) {
88                 case nil:
89                         arg = "<nil>"
90                 case operand:
91                         panic("got operand instead of *operand")
92                 case *operand:
93                         arg = operandString(a, qf)
94                 case syntax.Pos:
95                         arg = a.String()
96                 case syntax.Expr:
97                         arg = syntax.String(a)
98                 case Object:
99                         arg = ObjectString(a, qf)
100                 case Type:
101                         arg = TypeString(a, qf)
102                 }
103                 args[i] = arg
104         }
105         return fmt.Sprintf(format, args...)
106 }
107
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)
115                 }
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)
119                 }
120                 return pkg.name
121         }
122         return ""
123 }
124
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] {
129                 return
130         }
131         check.seenPkgMap[pkg] = true
132
133         forName, ok := check.pkgPathMap[pkg.name]
134         if !ok {
135                 forName = make(map[string]bool)
136                 check.pkgPathMap[pkg.name] = forName
137         }
138         forName[pkg.path] = true
139
140         for _, imp := range pkg.imports {
141                 check.markImports(imp)
142         }
143 }
144
145 func (check *Checker) sprintf(format string, args ...interface{}) string {
146         return sprintf(check.qualifier, format, args...)
147 }
148
149 func (check *Checker) report(err *error_) {
150         if err.empty() {
151                 panic("no error to report")
152         }
153         check.err(err.pos(), err.msg(check.qualifier), err.soft)
154 }
155
156 func (check *Checker) trace(pos syntax.Pos, format string, args ...interface{}) {
157         fmt.Printf("%s:\t%s%s\n",
158                 pos,
159                 strings.Repeat(".  ", check.indent),
160                 check.sprintf(format, args...),
161         )
162 }
163
164 // dump is only needed for debugging
165 func (check *Checker) dump(format string, args ...interface{}) {
166         fmt.Println(check.sprintf(format, args...))
167 }
168
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) {
176                 return
177         }
178
179         pos := posFor(at)
180
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)
189                 pos = check.errpos
190         }
191
192         err := Error{pos, stripAnnotations(msg), msg, soft}
193         if check.firstErr == nil {
194                 check.firstErr = err
195         }
196
197         if check.conf.Trace {
198                 check.trace(pos, "ERROR: %s", msg)
199         }
200
201         f := check.conf.Error
202         if f == nil {
203                 panic(bailout{}) // report only first error
204         }
205         f(err)
206 }
207
208 const (
209         invalidAST = "invalid AST: "
210         invalidArg = "invalid argument: "
211         invalidOp  = "invalid operation: "
212 )
213
214 type poser interface {
215         Pos() syntax.Pos
216 }
217
218 func (check *Checker) error(at poser, msg string) {
219         check.err(at, msg, false)
220 }
221
222 func (check *Checker) errorf(at poser, format string, args ...interface{}) {
223         check.err(at, check.sprintf(format, args...), false)
224 }
225
226 func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
227         check.err(at, check.sprintf(format, args...), true)
228 }
229
230 // posFor reports the left (= start) position of at.
231 func posFor(at poser) syntax.Pos {
232         switch x := at.(type) {
233         case *operand:
234                 if x.expr != nil {
235                         return syntax.StartPos(x.expr)
236                 }
237         case syntax.Node:
238                 return syntax.StartPos(x)
239         }
240         return at.Pos()
241 }
242
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.
246         var b bytes.Buffer
247         for _, r := range s {
248                 // strip #'s and subscript digits
249                 if r != instanceMarker && !('₀' <= r && r < '₀'+10) { // '₀' == U+2080
250                         b.WriteRune(r)
251                 }
252         }
253         if b.Len() < len(s) {
254                 return b.String()
255         }
256         return s
257 }