]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/errors.go
[dev.typeparams] merge master into dev.typeparams
[gostls13.git] / src / go / types / 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 types
8
9 import (
10         "errors"
11         "fmt"
12         "go/ast"
13         "go/token"
14         "strconv"
15         "strings"
16 )
17
18 func assert(p bool) {
19         if !p {
20                 panic("assertion failed")
21         }
22 }
23
24 func unreachable() {
25         panic("unreachable")
26 }
27
28 func (check *Checker) qualifier(pkg *Package) string {
29         // Qualify the package unless it's the package being type-checked.
30         if pkg != check.pkg {
31                 // If the same package name was used by multiple packages, display the full path.
32                 if check.pkgCnt[pkg.name] > 1 {
33                         return strconv.Quote(pkg.path)
34                 }
35                 return pkg.name
36         }
37         return ""
38 }
39
40 func (check *Checker) sprintf(format string, args ...interface{}) string {
41         for i, arg := range args {
42                 switch a := arg.(type) {
43                 case nil:
44                         arg = "<nil>"
45                 case operand:
46                         panic("internal error: should always pass *operand")
47                 case *operand:
48                         arg = operandString(a, check.qualifier)
49                 case token.Pos:
50                         arg = check.fset.Position(a).String()
51                 case ast.Expr:
52                         arg = ExprString(a)
53                 case Object:
54                         arg = ObjectString(a, check.qualifier)
55                 case Type:
56                         arg = TypeString(a, check.qualifier)
57                 }
58                 args[i] = arg
59         }
60         return fmt.Sprintf(format, args...)
61 }
62
63 func (check *Checker) trace(pos token.Pos, format string, args ...interface{}) {
64         fmt.Printf("%s:\t%s%s\n",
65                 check.fset.Position(pos),
66                 strings.Repeat(".  ", check.indent),
67                 check.sprintf(format, args...),
68         )
69 }
70
71 // dump is only needed for debugging
72 func (check *Checker) dump(format string, args ...interface{}) {
73         fmt.Println(check.sprintf(format, args...))
74 }
75
76 func (check *Checker) err(err error) {
77         if err == nil {
78                 return
79         }
80         var e Error
81         isInternal := errors.As(err, &e)
82         // Cheap trick: Don't report errors with messages containing
83         // "invalid operand" or "invalid type" as those tend to be
84         // follow-on errors which don't add useful information. Only
85         // exclude them if these strings are not at the beginning,
86         // and only if we have at least one error already reported.
87         isInvalidErr := isInternal && (strings.Index(e.Msg, "invalid operand") > 0 || strings.Index(e.Msg, "invalid type") > 0)
88         if check.firstErr != nil && isInvalidErr {
89                 return
90         }
91
92         if check.firstErr == nil {
93                 check.firstErr = err
94         }
95
96         if trace {
97                 pos := e.Pos
98                 msg := e.Msg
99                 if !isInternal {
100                         msg = err.Error()
101                         pos = token.NoPos
102                 }
103                 check.trace(pos, "ERROR: %s", msg)
104         }
105
106         f := check.conf.Error
107         if f == nil {
108                 panic(bailout{}) // report only first error
109         }
110         f(err)
111 }
112
113 func (check *Checker) error(pos token.Pos, code errorCode, msg string) {
114         check.err(Error{Fset: check.fset, Pos: pos, Msg: msg, go116code: code})
115 }
116
117 // newErrorf creates a new Error, but does not handle it.
118 func (check *Checker) newErrorf(pos token.Pos, code errorCode, format string, args ...interface{}) error {
119         return Error{
120                 Fset:      check.fset,
121                 Pos:       pos,
122                 Msg:       check.sprintf(format, args...),
123                 Soft:      false,
124                 go116code: code,
125         }
126 }
127
128 func (check *Checker) errorf(pos token.Pos, code errorCode, format string, args ...interface{}) {
129         check.error(pos, code, check.sprintf(format, args...))
130 }
131
132 func (check *Checker) softErrorf(pos token.Pos, code errorCode, format string, args ...interface{}) {
133         check.err(Error{
134                 Fset:      check.fset,
135                 Pos:       pos,
136                 Msg:       check.sprintf(format, args...),
137                 Soft:      true,
138                 go116code: code,
139         })
140 }
141
142 func (check *Checker) invalidAST(pos token.Pos, format string, args ...interface{}) {
143         check.errorf(pos, 0, "invalid AST: "+format, args...)
144 }
145
146 func (check *Checker) invalidArg(pos token.Pos, code errorCode, format string, args ...interface{}) {
147         check.errorf(pos, code, "invalid argument: "+format, args...)
148 }
149
150 func (check *Checker) invalidOp(pos token.Pos, code errorCode, format string, args ...interface{}) {
151         check.errorf(pos, code, "invalid operation: "+format, args...)
152 }