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