1
2
3
4
5
6
7 package types2
8
9
10 func isValid(t Type) bool { return Unalias(t) != Typ[Invalid] }
11
12
13
14
15
16 func isBoolean(t Type) bool { return isBasic(t, IsBoolean) }
17 func isInteger(t Type) bool { return isBasic(t, IsInteger) }
18 func isUnsigned(t Type) bool { return isBasic(t, IsUnsigned) }
19 func isFloat(t Type) bool { return isBasic(t, IsFloat) }
20 func isComplex(t Type) bool { return isBasic(t, IsComplex) }
21 func isNumeric(t Type) bool { return isBasic(t, IsNumeric) }
22 func isString(t Type) bool { return isBasic(t, IsString) }
23 func isIntegerOrFloat(t Type) bool { return isBasic(t, IsInteger|IsFloat) }
24 func isConstType(t Type) bool { return isBasic(t, IsConstType) }
25
26
27
28
29 func isBasic(t Type, info BasicInfo) bool {
30 u, _ := under(t).(*Basic)
31 return u != nil && u.info&info != 0
32 }
33
34
35
36
37
38
39
40 func allBoolean(t Type) bool { return allBasic(t, IsBoolean) }
41 func allInteger(t Type) bool { return allBasic(t, IsInteger) }
42 func allUnsigned(t Type) bool { return allBasic(t, IsUnsigned) }
43 func allNumeric(t Type) bool { return allBasic(t, IsNumeric) }
44 func allString(t Type) bool { return allBasic(t, IsString) }
45 func allOrdered(t Type) bool { return allBasic(t, IsOrdered) }
46 func allNumericOrString(t Type) bool { return allBasic(t, IsNumeric|IsString) }
47
48
49
50
51
52 func allBasic(t Type, info BasicInfo) bool {
53 if tpar, _ := Unalias(t).(*TypeParam); tpar != nil {
54 return tpar.is(func(t *term) bool { return t != nil && isBasic(t.typ, info) })
55 }
56 return isBasic(t, info)
57 }
58
59
60
61
62 func hasName(t Type) bool {
63 switch Unalias(t).(type) {
64 case *Basic, *Named, *TypeParam:
65 return true
66 }
67 return false
68 }
69
70
71
72
73 func isTypeLit(t Type) bool {
74 switch Unalias(t).(type) {
75 case *Named, *TypeParam:
76 return false
77 }
78 return true
79 }
80
81
82
83
84 func isTyped(t Type) bool {
85
86
87 b, _ := t.(*Basic)
88 return b == nil || b.info&IsUntyped == 0
89 }
90
91
92
93 func isUntyped(t Type) bool {
94 return !isTyped(t)
95 }
96
97
98
99 func isUntypedNumeric(t Type) bool {
100
101
102 b, _ := t.(*Basic)
103 return b != nil && b.info&IsUntyped != 0 && b.info&IsNumeric != 0
104 }
105
106
107 func IsInterface(t Type) bool {
108 _, ok := under(t).(*Interface)
109 return ok
110 }
111
112
113 func isNonTypeParamInterface(t Type) bool {
114 return !isTypeParam(t) && IsInterface(t)
115 }
116
117
118 func isTypeParam(t Type) bool {
119 _, ok := Unalias(t).(*TypeParam)
120 return ok
121 }
122
123
124
125
126
127 func hasEmptyTypeset(t Type) bool {
128 if tpar, _ := Unalias(t).(*TypeParam); tpar != nil && tpar.bound != nil {
129 iface, _ := safeUnderlying(tpar.bound).(*Interface)
130 return iface != nil && iface.tset != nil && iface.tset.IsEmpty()
131 }
132 return false
133 }
134
135
136
137
138 func isGeneric(t Type) bool {
139
140 named := asNamed(t)
141 return named != nil && named.obj != nil && named.inst == nil && named.TypeParams().Len() > 0
142 }
143
144
145 func Comparable(T Type) bool {
146 return comparable(T, true, nil, nil)
147 }
148
149
150
151 func comparable(T Type, dynamic bool, seen map[Type]bool, reportf func(string, ...interface{})) bool {
152 if seen[T] {
153 return true
154 }
155 if seen == nil {
156 seen = make(map[Type]bool)
157 }
158 seen[T] = true
159
160 switch t := under(T).(type) {
161 case *Basic:
162
163
164 return t.kind != UntypedNil
165 case *Pointer, *Chan:
166 return true
167 case *Struct:
168 for _, f := range t.fields {
169 if !comparable(f.typ, dynamic, seen, nil) {
170 if reportf != nil {
171 reportf("struct containing %s cannot be compared", f.typ)
172 }
173 return false
174 }
175 }
176 return true
177 case *Array:
178 if !comparable(t.elem, dynamic, seen, nil) {
179 if reportf != nil {
180 reportf("%s cannot be compared", t)
181 }
182 return false
183 }
184 return true
185 case *Interface:
186 if dynamic && !isTypeParam(T) || t.typeSet().IsComparable(seen) {
187 return true
188 }
189 if reportf != nil {
190 if t.typeSet().IsEmpty() {
191 reportf("empty type set")
192 } else {
193 reportf("incomparable types in type set")
194 }
195 }
196
197 }
198 return false
199 }
200
201
202 func hasNil(t Type) bool {
203 switch u := under(t).(type) {
204 case *Basic:
205 return u.kind == UnsafePointer
206 case *Slice, *Pointer, *Signature, *Map, *Chan:
207 return true
208 case *Interface:
209 return !isTypeParam(t) || u.typeSet().underIs(func(u Type) bool {
210 return u != nil && hasNil(u)
211 })
212 }
213 return false
214 }
215
216
217 func samePkg(a, b *Package) bool {
218
219 if a == nil || b == nil {
220 return a == b
221 }
222
223 return a.path == b.path
224 }
225
226
227 type ifacePair struct {
228 x, y *Interface
229 prev *ifacePair
230 }
231
232 func (p *ifacePair) identical(q *ifacePair) bool {
233 return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x
234 }
235
236
237 type comparer struct {
238 ignoreTags bool
239 ignoreInvalids bool
240 }
241
242
243 func (c *comparer) identical(x, y Type, p *ifacePair) bool {
244 x = Unalias(x)
245 y = Unalias(y)
246
247 if x == y {
248 return true
249 }
250
251 if c.ignoreInvalids && (!isValid(x) || !isValid(y)) {
252 return true
253 }
254
255 switch x := x.(type) {
256 case *Basic:
257
258
259
260 if y, ok := y.(*Basic); ok {
261 return x.kind == y.kind
262 }
263
264 case *Array:
265
266
267 if y, ok := y.(*Array); ok {
268
269
270 return (x.len < 0 || y.len < 0 || x.len == y.len) && c.identical(x.elem, y.elem, p)
271 }
272
273 case *Slice:
274
275 if y, ok := y.(*Slice); ok {
276 return c.identical(x.elem, y.elem, p)
277 }
278
279 case *Struct:
280
281
282
283
284 if y, ok := y.(*Struct); ok {
285 if x.NumFields() == y.NumFields() {
286 for i, f := range x.fields {
287 g := y.fields[i]
288 if f.embedded != g.embedded ||
289 !c.ignoreTags && x.Tag(i) != y.Tag(i) ||
290 !f.sameId(g.pkg, g.name, false) ||
291 !c.identical(f.typ, g.typ, p) {
292 return false
293 }
294 }
295 return true
296 }
297 }
298
299 case *Pointer:
300
301 if y, ok := y.(*Pointer); ok {
302 return c.identical(x.base, y.base, p)
303 }
304
305 case *Tuple:
306
307
308 if y, ok := y.(*Tuple); ok {
309 if x.Len() == y.Len() {
310 if x != nil {
311 for i, v := range x.vars {
312 w := y.vars[i]
313 if !c.identical(v.typ, w.typ, p) {
314 return false
315 }
316 }
317 }
318 return true
319 }
320 }
321
322 case *Signature:
323 y, _ := y.(*Signature)
324 if y == nil {
325 return false
326 }
327
328
329
330
331
332
333
334 if x.TypeParams().Len() != y.TypeParams().Len() {
335 return false
336 }
337
338
339
340 yparams := y.params
341 yresults := y.results
342
343 if x.TypeParams().Len() > 0 {
344
345
346 xtparams := x.TypeParams().list()
347 ytparams := y.TypeParams().list()
348
349 var targs []Type
350 for i := range xtparams {
351 targs = append(targs, x.TypeParams().At(i))
352 }
353 smap := makeSubstMap(ytparams, targs)
354
355 var check *Checker
356 ctxt := NewContext()
357
358
359 for i, xtparam := range xtparams {
360 ybound := check.subst(nopos, ytparams[i].bound, smap, nil, ctxt)
361 if !c.identical(xtparam.bound, ybound, p) {
362 return false
363 }
364 }
365
366 yparams = check.subst(nopos, y.params, smap, nil, ctxt).(*Tuple)
367 yresults = check.subst(nopos, y.results, smap, nil, ctxt).(*Tuple)
368 }
369
370 return x.variadic == y.variadic &&
371 c.identical(x.params, yparams, p) &&
372 c.identical(x.results, yresults, p)
373
374 case *Union:
375 if y, _ := y.(*Union); y != nil {
376
377
378 unionSets := make(map[*Union]*_TypeSet)
379 xset := computeUnionTypeSet(nil, unionSets, nopos, x)
380 yset := computeUnionTypeSet(nil, unionSets, nopos, y)
381 return xset.terms.equal(yset.terms)
382 }
383
384 case *Interface:
385
386
387
388
389
390
391
392 if y, ok := y.(*Interface); ok {
393 xset := x.typeSet()
394 yset := y.typeSet()
395 if xset.comparable != yset.comparable {
396 return false
397 }
398 if !xset.terms.equal(yset.terms) {
399 return false
400 }
401 a := xset.methods
402 b := yset.methods
403 if len(a) == len(b) {
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426 q := &ifacePair{x, y, p}
427 for p != nil {
428 if p.identical(q) {
429 return true
430 }
431 p = p.prev
432 }
433 if debug {
434 assertSortedMethods(a)
435 assertSortedMethods(b)
436 }
437 for i, f := range a {
438 g := b[i]
439 if f.Id() != g.Id() || !c.identical(f.typ, g.typ, q) {
440 return false
441 }
442 }
443 return true
444 }
445 }
446
447 case *Map:
448
449 if y, ok := y.(*Map); ok {
450 return c.identical(x.key, y.key, p) && c.identical(x.elem, y.elem, p)
451 }
452
453 case *Chan:
454
455
456 if y, ok := y.(*Chan); ok {
457 return x.dir == y.dir && c.identical(x.elem, y.elem, p)
458 }
459
460 case *Named:
461
462
463
464 if y := asNamed(y); y != nil {
465
466
467
468 xargs := x.TypeArgs().list()
469 yargs := y.TypeArgs().list()
470 if len(xargs) != len(yargs) {
471 return false
472 }
473 for i, xarg := range xargs {
474 if !Identical(xarg, yargs[i]) {
475 return false
476 }
477 }
478 return identicalOrigin(x, y)
479 }
480
481 case *TypeParam:
482
483
484 case nil:
485
486
487 default:
488 panic("unreachable")
489 }
490
491 return false
492 }
493
494
495 func identicalOrigin(x, y *Named) bool {
496
497 return x.Origin().obj == y.Origin().obj
498 }
499
500
501
502
503 func identicalInstance(xorig Type, xargs []Type, yorig Type, yargs []Type) bool {
504 if len(xargs) != len(yargs) {
505 return false
506 }
507
508 for i, xa := range xargs {
509 if !Identical(xa, yargs[i]) {
510 return false
511 }
512 }
513
514 return Identical(xorig, yorig)
515 }
516
517
518
519
520 func Default(t Type) Type {
521
522
523 if t, _ := t.(*Basic); t != nil {
524 switch t.kind {
525 case UntypedBool:
526 return Typ[Bool]
527 case UntypedInt:
528 return Typ[Int]
529 case UntypedRune:
530 return universeRune
531 case UntypedFloat:
532 return Typ[Float64]
533 case UntypedComplex:
534 return Typ[Complex128]
535 case UntypedString:
536 return Typ[String]
537 }
538 }
539 return t
540 }
541
542
543
544
545
546 func maxType(x, y Type) Type {
547
548
549 if x == y {
550 return x
551 }
552 if isUntypedNumeric(x) && isUntypedNumeric(y) {
553
554 if x.(*Basic).kind > y.(*Basic).kind {
555 return x
556 }
557 return y
558 }
559 return nil
560 }
561
562
563 func clone[P *T, T any](p P) P {
564 c := *p
565 return &c
566 }
567
View as plain text