-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
224 lines (206 loc) · 5.69 KB
/
Copy patherror.go
File metadata and controls
224 lines (206 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package analysis
import (
"cmp"
"github.com/ProCode-Software/klar/internal/ast"
"github.com/ProCode-Software/klar/internal/klarerrs"
"github.com/ProCode-Software/klar/internal/ranges"
)
// If stopParsing is passed to panic, the checker will immediately stop parsing.
type stopChecker struct{}
func (c *Checker) error(err *klarerrs.Error) *klarerrs.Error {
c.Errors = append(c.Errors, err)
c.module.Flags |= ModuleWithErrors
if c.Options.Error != nil {
c.Options.Error(err)
}
if mx := c.Options.MaxErrors; mx > 0 && len(c.Errors) >= mx &&
klarerrs.ErrorCount(c.Errors) >= mx { // Exclude warnings from error limit
panic(stopChecker{})
}
return err
}
func (c *Checker) fileError(err *klarerrs.Error, fid FileID) {
err.File = c.module.ResolveFilePath(fid)
c.error(err)
}
func redeclaredError(new, old *Object, topLevel bool) *klarerrs.Error {
// TODO
err := klarerrs.Range(klarerrs.ErrRedeclared, new.Range)
err.Details = append(err.Details, klarerrs.Detail{
File: old.FilePath(),
Range: old.Range,
Message: klarerrs.Quote(old.Name) + " was originally declared here",
})
err.Label = klarerrs.Quote(old.Name) + " already exists"
err.SetParam("oldKind", kindOf(old.Type))
err.SetParam("newKind", kindOf(new.Type))
err.Name = old.Name
return err
}
func kindOf(typ ObjectKind) string {
switch typ.(type) {
case *Variable:
return "variable"
case *Constant:
return "constant"
case *Function, *Overload:
return "function"
case *FunctionAlias:
return "function alias"
case *TypeName:
return "type"
/* if typ.Type == nil {
return "type"
}
return typ.Kind().String() */
case *Namespace:
return "namespace"
case nil:
return "object"
default:
return "object"
}
}
func objectError(code klarerrs.Code, obj *Object) *klarerrs.Error {
err := &klarerrs.Error{
Range: obj.Range,
File: obj.FilePath(),
Code: code,
}
return err
}
func typeMismatch(exp, got Type, gotRange ranges.Range) *klarerrs.Error {
if got == Untyped(KindOptional) {
// Trying to assign nil to a non-optional type
err := klarerrs.Range(klarerrs.ErrNotOptionalType, gotRange)
err.Label = "Required type " + klarerrs.Quote(exp.String()) + " can't be 'none'"
err.Info = klarerrs.TypeErrorInfo{
ExpectedType: exp.String(),
GotType: got.String(),
}
return err
}
err := cmp.Or(
tryUnwrapRequiredError(exp, got, gotRange),
klarerrs.Range(klarerrs.ErrTypeMismatch, gotRange),
)
err.Label = "This has type " + klarerrs.Quote(got.String())
err.Info = klarerrs.TypeErrorInfo{
ExpectedType: exp.String(),
GotType: got.String(),
}
return err
}
func tryUnwrapRequiredError(exp, got Type, gotRange ranges.Range) *klarerrs.Error {
var kind string
switch got := Underlying(got).(type) {
case *Optional:
if !Compatible(got.Elem, exp) {
return nil
}
kind = "Optional"
case *Result:
if !Compatible(got.Success, exp) {
return nil
}
kind = "Result"
default:
return nil
}
return klarerrs.Range(klarerrs.ErrUnwrapRequired, gotRange).
SetParam("kind", kind).
SetParam("before", "before it can be used as "+quote(exp.String()))
}
func handlePanic() {
r := recover()
if _, ok := r.(stopChecker); !ok && r != nil {
panic(r)
}
}
// The returned Error's File is already set.
func cycleError(cycle []*Object) *klarerrs.Error {
// Find the object with the earliest position in the file
firstInSrcI, firstPos := 0, cycle[0].Range.Start
for i, o := range cycle {
pos := o.Range.Start
if ranges.ComparePos(pos, firstPos) < 0 {
firstInSrcI, firstPos = i, pos
}
}
o := cycle[firstInSrcI]
// If the object is an alias, mark it as valid to avoid later errors.
tn, isTypeDecl := o.Type.(*TypeName)
if isTypeDecl {
if alias, ok := tn.Type.(*TypeAlias); ok {
alias.resolved = InvalidType
alias.Type = InvalidType
}
}
err := klarerrs.Range(klarerrs.ErrDepCycle, o.Range)
err.File = o.FilePath()
err.Name = o.Name
if isTypeDecl {
err.SetParam("type", true)
}
if len(cycle) == 1 {
// Self-cycle. Report a better error message
err.SetParam("self", true)
err.Label = "This type depends on itself"
return err
}
for i := range cycle {
nextI := (firstInSrcI + i + 1) % len(cycle)
nextObj := cycle[nextI]
var lastInCycleMsg string
if nextI == 0 {
lastInCycleMsg = " in a cycle"
}
err.AddDetailf(
o.FilePath(), o.Range, "%s depends on %s%s",
klarerrs.Quote(o.Name), klarerrs.Quote(nextObj.Name), lastInCycleMsg,
)
o = nextObj
}
return err
}
func fieldNotFound(name string) *klarerrs.Error {
return &klarerrs.Error{
Code: klarerrs.ErrFieldNotFound,
Label: "Field " + quote(name) + " doesn't exist",
Name: name,
}
// A range will be added later by the caller of [Indexer.IndexDot]
}
func indexError(code klarerrs.Code, t Type, label string) *klarerrs.Error {
err := &klarerrs.Error{
Code: code,
Label: label,
Info: klarerrs.TypeErrorInfo{GotType: t.String()},
}
return err
}
func indexTypeMismatchError(code klarerrs.Code, exp, got Type, label string) *klarerrs.Error {
err := &klarerrs.Error{
Code: code,
Label: label,
Info: klarerrs.TypeErrorInfo{ExpectedType: exp.String(), GotType: got.String()},
}
return err
}
func invalidRestTypeError(typ Type, expr ast.Expression) *klarerrs.Error {
err := klarerrs.Node(klarerrs.ErrInvalidRestValue, expr)
if typ.Kind() == KindMap {
err.Code = klarerrs.ErrMisplacedMapRest
err.Label = "Can't rest a map outside a map literal"
} else {
err.Info = klarerrs.TypeErrorInfo{GotType: typ.String()}
err.Label = "Can't rest " + klarerrs.WithA(typ.Kind().String())
}
return err
}
func misplacedListRestError(kind Kind, expr ast.Expression) *klarerrs.Error {
err := klarerrs.Node(klarerrs.ErrMisplacedListRest, expr)
err.Name = kind.String()
err.Label = "The parameter isn't variadic"
return err
}