Repository navigation
Expand file tree
/
Copy pathcompatibility.go
More file actions
376 lines (363 loc) 路 9.73 KB
/
Copy pathcompatibility.go
File metadata and controls
376 lines (363 loc) 路 9.73 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package analysis
import (
"cmp"
"fmt"
"iter"
"slices"
)
// Compatible returns whether type a is compatible with b.
func Compatible(a, b Type) bool {
// Allow checking for compatibility with kinds
// Example: Compatible(t, KindStruct) = true if t is a struct
if b, ok := b.(Kind); ok && !b.IsPrimitive() {
return a.Kind() == b
}
aKind, bKind := a.Kind(), b.Kind()
switch a := Underlying(a).(type) {
case Untyped:
// Untyped Int is compatible with Int and Float. For other untyped
// types, they're compatible if they have the same Kind.
if aKind == bKind || (aKind == IntType && bKind == FloatType) {
return true
}
// Continue because 'b' may be a union, optional, etc.
case *UntypedInit:
return aKind == bKind
case *NoReturn:
if a.Type == nil { // Always true for *NoReturn to be an underlying type
return true // This is a TODO function and is compatible with any type
}
}
// If the target is a TODO, it will be assigned to Any?
if nrb, ok := b.(*NoReturn); ok && nrb.Type == nil {
b = &Optional{AnyType}
}
switch {
case b == InvalidType:
return true // Show less errors
case bKind == AnyType:
// A => Any if A != nil. All non-optional types are compatible with Any
return aKind != KindOptional
case aKind == KindList && bKind == KindList:
// [A] => [B] if A => B
a = Underlying(a).(*List).Elem
b = Underlying(b).(*List).Elem
return Compatible(a, b)
case bKind == KindOptional && aKind != KindOptional:
// A => B? if A => B
if b == Untyped(KindOptional) {
return false // Non-optional to untyped nil
}
b := Underlying(b).(*Optional)
return Compatible(a, b.Elem)
case bKind == KindOptional && aKind == KindOptional:
// A? => B? if A => B | nil
a = Underlying(a).(*Optional).Elem
b = Underlying(b).(*Optional).Elem
return Compatible(a, b)
case bKind == KindResult && aKind != KindResult:
// A => Result<B, C> if A => B | C
b := Underlying(b).(*Result)
return Compatible(a, b.Success) || Compatible(a, b.Error)
case bKind == KindUnion && aKind == KindResult:
// Result<A, B> => C | D if A => C | D and B => C | D.
//
// We won't allow compatibility the other way around because the compiler
// needs to know at compile-time which type is the error, and which is the
// success type (assertions do exist).
//
// TODO: Should we keep Result => union compatibility? Results are intended
// for error handling, and assigning to the union means not checking the error.
ra := Underlying(a).(*Result)
return Compatible(ra.Success, b) && Compatible(ra.Error, b)
case bKind == KindTag, bKind == KindInterface:
// TODO
if Implements(a, b) {
return true
}
case bKind == KindUnion && aKind != KindUnion:
// A => B | C if A => B or A => C
ub := As[*Union](b)
return slices.ContainsFunc(ub.Types, func(opt Type) bool {
return Compatible(a, opt)
})
case bKind == KindUnion && aKind == KindUnion:
// AA | AB => BA | BB if (AA => BA | BB) and (AB => BA | BB)
//
// Check that each type in union A is compatible with the entire union B
ua, ub := As[*Union](a), As[*Union](b)
for _, ta := range ua.Types {
if !slices.ContainsFunc(ub.Types, func(tb Type) bool {
return Compatible(ta, tb)
}) {
return false
}
}
return true
case bKind != KindUnion && aKind == KindUnion:
// A | B => C if A => C and B => C
ua := As[*Union](a)
for _, ta := range ua.Types {
if !Compatible(ta, b) {
return false
}
}
return true
case bKind == KindMap && aKind == KindMap:
b = Underlying(b)
if _, ok := b.(Untyped); ok {
return false
}
// #{KA: VA} => #{KB: VB} if KA => KB and VA => VB
ma, mb := As[*Map](a), b.(*Map)
return Compatible(ma.Key, mb.Key) && Compatible(ma.Value, mb.Value)
case bKind == KindEnum && aKind == KindEnum:
ea := Underlying(a).(EnumParenter)
b = Underlying(b)
if _, ok := b.(*UntypedInit); ok {
return false
}
eb := Underlying(b).(EnumParenter)
return ea.EnumParent() == eb.EnumParent()
case bKind == KindFunction && aKind == KindFunction:
if funcsCompatible(a, b) {
return true
}
case bKind == KindTuple && aKind == KindTuple:
// (AA, AB) => (BA, BB) if AA => AB and BA => BB
ta, tb := As[*Tuple](a), As[*Tuple](b)
return slices.EqualFunc(ta.Items, tb.Items, Compatible)
}
return TypesEqual(a, b) // TODO
}
// TypesEqual returns whether the underlying types of a and b are equal.
func TypesEqual(a, b Type) bool {
a, b = Underlying(a), Underlying(b)
// If one is invalid type, avoid showing many errors, so we will say
// they are compatible.
if a.Kind() == InvalidType || b.Kind() == InvalidType {
return true
}
if a.Kind() != b.Kind() {
return false
}
// If the kinds are the same, any untyped type is compatible
switch b.(type) {
case Untyped, *UntypedInit, Kind:
return true
}
switch a := a.(type) {
case Untyped, *UntypedInit, Kind:
return true
case *List:
return TypesEqual(a.Elem, b.(*List).Elem)
case *Optional:
return TypesEqual(a.Elem, b.(*Optional).Elem)
case *Map:
b := b.(*Map)
return TypesEqual(a.Key, b.Key) && TypesEqual(b.Value, b.Value)
case *Tuple:
return slices.EqualFunc(a.Items, b.(*Tuple).Items, TypesEqual)
case *Result:
b := b.(*Result)
return TypesEqual(a.Success, b.Success) && TypesEqual(a.Error, b.Error)
case *Task:
return TypesEqual(a.Result, b.(*Task).Result)
case EnumParenter:
b, ok := b.(EnumParenter)
if !ok {
return false
}
return a.EnumParent() == b.EnumParent()
}
// Such as structs, enums
return a == b
}
func ConcreteTypeOf(t Type) Type {
t = Underlying(t)
switch t := t.(type) {
case *Optional:
return t.Elem
case *Result:
return t.Success
default:
return t
}
}
func IsConcreteType(t Type) bool {
switch t.Kind() {
case KindUnion, KindInterface, KindTag, KindResult, KindOptional:
return false
}
return true
}
// Implements returns whether type a implements b.
func Implements(a, b Type) bool {
a = Underlying(a)
if b.Kind() != KindTag {
// TODO: Interface (and possibly struct) implementation checking
return false
}
b = Underlying(b).(*Tag)
var inherited map[Type]struct{}
switch a := Underlying(a).(type) {
case *Enum:
inherited = a.Inherited
case *Struct:
inherited = a.Inherited
case *Interface:
inherited = a.Inherited
case *Tag:
if a == b {
return true
}
inherited = a.Implements
}
for a := range inherited {
if Compatible(a, b) {
return true
}
}
return false
}
func isTypeName(t Type) bool {
switch t := t.(type) {
case *TypeName:
return true
case *Object:
return t.IsTypeName()
default:
return false
}
}
// CommonType returns the largest type of a and b. If a and b are compatible
// with each other, CommonTyp returns a. If neither are compatible, CommonType
// returns nil.
//
// Examples:
//
// CommonType(UntypedInt, Int) -> Int
// CommonType(interface A, type that implements A) -> A
func CommonType(a, b Type) Type {
compatAB := Compatible(a, b)
compatBA := Compatible(b, a)
switch {
case compatAB && !compatBA:
return b
case compatBA && !compatAB:
return a
case compatAB && compatBA:
return a
case !compatAB && !compatBA:
return nil
default:
panic(fmt.Sprintf(
"CommonType(a, b): unhandled: Compatible(a, b) = %t, Compatible(b, a) = %t",
compatAB, compatBA,
))
}
}
// commonTypeOptional is [CommonType], but accepts nil arguments. If
// any argument is nil, the non-nil argument is returned. If both are nil,
// commonTypeOptional panics. commonTypeOptional returns nil if
// [CommonType](a, b) returns nil.
func commonTypeOptional(a, b Type) Type {
if a == nil || b == nil {
nonNil := cmp.Or(a, b)
if nonNil == nil {
panic("commonTypeOptional(nil, nil)")
}
return nonNil
}
return CommonType(a, b)
}
func IsUntyped(t Type) bool {
switch Underlying(t).(type) {
case Untyped, *UntypedInit:
return true
default:
return false
}
}
func funcsCompatible(a, b Type) bool {
a, b = Underlying(a), Underlying(b)
switch a := a.(type) {
case *UntypedLambda:
switch b := b.(type) {
case *Lambda:
return len(a.Vars) == len(b.Params)
case *Overload:
if len(b.LabelledParams) > 0 {
return false
}
}
case *Function:
return hasCompatibleOverload(a, b)
}
// TODO: I don't know if we can check for this (A => *Function)
/* if b, ok := b.(*Function); ok {
// We can't use [hasCompatibleOverload](b, a) because we still need
// to check for A => B.
return slices.ContainsFunc(b.Overloads, func(a *Overload) bool {
return funcsCompatible(a, b)
})
} */
paramsA, variadicA, returnA, ok := makeFuncIter(a)
if !ok {
return false
}
paramsB, variadicB, returnB, ok := makeFuncIter(a)
if !ok {
return false
}
// Compare variadicity and return types
// TODO: func(...Any) is compatible with func(Int)
if variadicA != variadicB || !Compatible(returnA, returnB) {
return false
}
// Compare params
nextA, stopA := iter.Pull(paramsA)
nextB, stopB := iter.Pull(paramsB)
defer stopA()
defer stopB()
for {
a, okA := nextA()
b, okB := nextB()
if !okA || !okB {
return okA == okB
}
// fn(A) => fn(B) if B => A. It can't be the other way around.
if !Compatible(b, a) {
return false
}
}
}
func makeFuncIter(fn Type) (params iter.Seq[Type], variadic bool, ret Type, ok bool) {
switch fn := fn.(type) {
case *Lambda:
return slices.Values(fn.Params), fn.Variadic, fn.Return, true
case *Function:
if len(fn.Overloads) > 0 {
return nil, false, nil, false
}
return makeFuncIter(fn.Overloads[0])
case *Overload:
if len(fn.LabelledParams) > 0 {
return nil, false, nil, false
}
return func(yield func(Type) bool) {
for _, param := range fn.Params {
if !yield(param) {
return
}
}
}, fn.Variadic(), fn.Return, true
default:
return nil, false, nil, false
}
}
func hasCompatibleOverload(a *Function, b Type) bool {
return slices.ContainsFunc(a.Overloads, func(a *Overload) bool {
return funcsCompatible(a, b)
})
}