1 // Copyright 2020 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.
5 // This file implements type unification.
15 // The unifier maintains two separate sets of type parameters x and y
16 // which are used to resolve type parameters in the x and y arguments
17 // provided to the unify call. For unidirectional unification, only
18 // one of these sets (say x) is provided, and then type parameters are
19 // only resolved for the x argument passed to unify, not the y argument
20 // (even if that also contains possibly the same type parameters). This
21 // is crucial to infer the type parameters of self-recursive calls:
23 // func f[P any](a P) { f(a) }
25 // For the call f(a) we want to infer that the type argument for P is P.
26 // During unification, the parameter type P must be resolved to the type
27 // parameter P ("x" side), but the argument type P must be left alone so
28 // that unification resolves the type parameter P to P.
30 // For bidirectional unification, both sets are provided. This enables
31 // unification to go from argument to parameter type and vice versa.
32 // For constraint type inference, we use bidirectional unification
33 // where both the x and y type parameters are identical. This is done
34 // by setting up one of them (using init) and then assigning its value
38 // Upper limit for recursion depth. Used to catch infinite recursions
39 // due to implementation issues (e.g., see issues #48619, #48656).
40 unificationDepthLimit = 50
42 // Whether to panic when unificationDepthLimit is reached. Turn on when
43 // investigating infinite recursion.
44 panicAtUnificationDepthLimit = false
46 // If enableCoreTypeUnification is set, unification will consider
47 // the core types, if any, of non-local (unbound) type parameters.
48 enableCoreTypeUnification = true
50 // If traceInference is set, unification will print a trace of its operation.
51 // Interpretation of trace:
52 // x ≡ y attempt to unify types x and y
53 // p ➞ y type parameter p is set to type y (p is inferred to be y)
54 // p ⇄ q type parameters p and q match (p is inferred to be q and vice versa)
55 // x ≢ y types x and y cannot be unified
56 // [p, q, ...] ➞ [x, y, ...] mapping from type parameters to types
57 traceInference = false
60 // A unifier maintains the current type parameters for x and y
61 // and the respective types inferred for each type parameter.
62 // A unifier is created by calling newUnifier.
65 x, y tparamsList // x and y must initialized via tparamsList.init
66 types []Type // inferred types, shared by x and y
67 depth int // recursion depth during unification
70 // newUnifier returns a new unifier.
71 // If exact is set, unification requires unified types to match
72 // exactly. If exact is not set, a named type's underlying type
73 // is considered if unification would fail otherwise, and the
74 // direction of channels is ignored.
75 // TODO(gri) exact is not set anymore by a caller. Consider removing it.
76 func newUnifier(exact bool) *unifier {
77 u := &unifier{exact: exact}
83 // unify attempts to unify x and y and reports whether it succeeded.
84 func (u *unifier) unify(x, y Type) bool {
85 return u.nify(x, y, nil)
88 func (u *unifier) tracef(format string, args ...interface{}) {
89 fmt.Println(strings.Repeat(". ", u.depth) + sprintf(nil, true, format, args...))
92 // A tparamsList describes a list of type parameters and the types inferred for them.
93 type tparamsList struct {
96 // For each tparams element, there is a corresponding type slot index in indices.
97 // index < 0: unifier.types[-index-1] == nil
98 // index == 0: no type slot allocated yet
99 // index > 0: unifier.types[index-1] == typ
100 // Joined tparams elements share the same type slot and thus have the same index.
101 // By using a negative index for nil types we don't need to check unifier.types
102 // to see if we have a type or not.
103 indices []int // len(d.indices) == len(d.tparams)
106 // String returns a string representation for a tparamsList. For debugging.
107 func (d *tparamsList) String() string {
109 w := newTypeWriter(&buf, nil)
111 for i, tpar := range d.tparams {
123 // init initializes d with the given type parameters.
124 // The type parameters must be in the order in which they appear in their declaration
125 // (this ensures that the tparams indices match the respective type parameter index).
126 func (d *tparamsList) init(tparams []*TypeParam) {
127 if len(tparams) == 0 {
131 for i, tpar := range tparams {
132 assert(i == tpar.index)
136 d.indices = make([]int, len(tparams))
139 // join unifies the i'th type parameter of x with the j'th type parameter of y.
140 // If both type parameters already have a type associated with them and they are
141 // not joined, join fails and returns false.
142 func (u *unifier) join(i, j int) bool {
144 u.tracef("%s ⇄ %s", u.x.tparams[i], u.y.tparams[j])
149 case ti == 0 && tj == 0:
150 // Neither type parameter has a type slot associated with them.
151 // Allocate a new joined nil type slot (negative index).
152 u.types = append(u.types, nil)
153 u.x.indices[i] = -len(u.types)
154 u.y.indices[j] = -len(u.types)
156 // The type parameter for x has no type slot yet. Use slot of y.
159 // The type parameter for y has no type slot yet. Use slot of x.
162 // Both type parameters have a slot: ti != 0 && tj != 0.
164 // Both type parameters already share the same slot. Nothing to do.
166 case ti > 0 && tj > 0:
167 // Both type parameters have (possibly different) inferred types. Cannot join.
168 // TODO(gri) Should we check if types are identical? Investigate.
171 // Only the type parameter for x has an inferred type. Use x slot for y.
173 // This case is handled like the default case.
175 // // Only the type parameter for y has an inferred type. Use y slot for x.
176 // u.x.setIndex(i, tj)
178 // Neither type parameter has an inferred type. Use y slot for x
179 // (or x slot for y, it doesn't matter).
185 // If typ is a type parameter of d, index returns the type parameter index.
186 // Otherwise, the result is < 0.
187 func (d *tparamsList) index(typ Type) int {
188 if tpar, ok := typ.(*TypeParam); ok {
189 return tparamIndex(d.tparams, tpar)
194 // If tpar is a type parameter in list, tparamIndex returns the type parameter index.
195 // Otherwise, the result is < 0. tpar must not be nil.
196 func tparamIndex(list []*TypeParam, tpar *TypeParam) int {
197 // Once a type parameter is bound its index is >= 0. However, there are some
198 // code paths (namely tracing and type hashing) by which it is possible to
199 // arrive here with a type parameter that has not been bound, hence the check
201 // TODO(rfindley): investigate a better approach for guarding against using
202 // unbound type parameters.
203 if i := tpar.index; 0 <= i && i < len(list) && list[i] == tpar {
209 // setIndex sets the type slot index for the i'th type parameter
210 // (and all its joined parameters) to tj. The type parameter
211 // must have a (possibly nil) type slot associated with it.
212 func (d *tparamsList) setIndex(i, tj int) {
214 assert(ti != 0 && tj != 0)
215 for k, tk := range d.indices {
222 // at returns the type set for the i'th type parameter; or nil.
223 func (d *tparamsList) at(i int) Type {
224 if ti := d.indices[i]; ti > 0 {
225 return d.unifier.types[ti-1]
230 // set sets the type typ for the i'th type parameter;
231 // typ must not be nil and it must not have been set before.
232 func (d *tparamsList) set(i int, typ Type) {
236 u.tracef("%s ➞ %s", d.tparams[i], typ)
238 switch ti := d.indices[i]; {
243 u.types = append(u.types, typ)
244 d.indices[i] = len(u.types)
246 panic("type already set")
250 // unknowns returns the number of type parameters for which no type has been set yet.
251 func (d *tparamsList) unknowns() int {
253 for _, ti := range d.indices {
261 // types returns the list of inferred types (via unification) for the type parameters
262 // described by d, and an index. If all types were inferred, the returned index is < 0.
263 // Otherwise, it is the index of the first type parameter which couldn't be inferred;
264 // i.e., for which list[index] is nil.
265 func (d *tparamsList) types() (list []Type, index int) {
266 list = make([]Type, len(d.tparams))
268 for i := range d.tparams {
271 if index < 0 && t == nil {
278 func (u *unifier) nifyEq(x, y Type, p *ifacePair) bool {
279 return x == y || u.nify(x, y, p)
282 // nify implements the core unification algorithm which is an
283 // adapted version of Checker.identical. For changes to that
284 // code the corresponding changes should be made here.
285 // Must not be called directly from outside the unifier.
286 func (u *unifier) nify(x, y Type, p *ifacePair) (result bool) {
288 u.tracef("%s ≡ %s", x, y)
291 // Stop gap for cases where unification fails.
292 if u.depth >= unificationDepthLimit {
294 u.tracef("depth %d >= %d", u.depth, unificationDepthLimit)
296 if panicAtUnificationDepthLimit {
297 panic("unification reached recursion depth limit")
304 if traceInference && !result {
305 u.tracef("%s ≢ %s", x, y)
310 // If exact unification is known to fail because we attempt to
311 // match a type name against an unnamed type literal, consider
312 // the underlying type of the named type.
313 // (We use !hasName to exclude any type with a name, including
314 // basic types and type parameters; the rest are unamed types.)
315 if nx, _ := x.(*Named); nx != nil && !hasName(y) {
317 u.tracef("under %s ≡ %s", nx, y)
319 return u.nify(nx.under(), y, p)
320 } else if ny, _ := y.(*Named); ny != nil && !hasName(x) {
322 u.tracef("%s ≡ under %s", x, ny)
324 return u.nify(x, ny.under(), p)
328 // Cases where at least one of x or y is a type parameter.
329 switch i, j := u.x.index(x), u.y.index(y); {
330 case i >= 0 && j >= 0:
331 // both x and y are type parameters
335 // both x and y have an inferred type - they must match
336 return u.nifyEq(u.x.at(i), u.y.at(j), p)
339 // x is a type parameter, y is not
340 if tx := u.x.at(i); tx != nil {
341 return u.nifyEq(tx, y, p)
343 // otherwise, infer type from y
348 // y is a type parameter, x is not
349 if ty := u.y.at(j); ty != nil {
350 return u.nifyEq(x, ty, p)
352 // otherwise, infer type from x
357 // If we get here and x or y is a type parameter, they are type parameters
358 // from outside our declaration list. Try to unify their core types, if any
359 // (see issue #50755 for a test case).
360 if enableCoreTypeUnification && !u.exact {
361 if isTypeParam(x) && !hasName(y) {
362 // Caution: This may not be correct in light of ~ constraints.
364 // TODO(gri) investigate!
366 // When considering the type parameter for unification
367 // we look at the adjusted core type (coreTerm).
368 // If the adjusted core type is a named type N; the
369 // corresponding core type is under(N). Since !u.exact
370 // and y doesn't have a name, unification will end up
371 // comparing under(N) to y, so we can just use the core
372 // type instead. Optimization.
373 if cx := coreType(x); cx != nil {
375 u.tracef("core %s ≡ %s", x, y)
377 return u.nify(cx, y, p)
379 } else if isTypeParam(y) && !hasName(x) {
381 if cy := coreType(y); cy != nil {
383 u.tracef("%s ≡ core %s", x, y)
385 return u.nify(x, cy, p)
390 // For type unification, do not shortcut (x == y) for identical
391 // types. Instead keep comparing them element-wise to unify the
392 // matching (and equal type parameter types). A simple test case
393 // where this matters is: func f[P any](a P) { f(a) } .
395 switch x := x.(type) {
397 // Basic types are singletons except for the rune and byte
398 // aliases, thus we cannot solely rely on the x == y check
399 // above. See also comment in TypeName.IsAlias.
400 if y, ok := y.(*Basic); ok {
401 return x.kind == y.kind
405 // Two array types are identical if they have identical element types
406 // and the same array length.
407 if y, ok := y.(*Array); ok {
408 // If one or both array lengths are unknown (< 0) due to some error,
409 // assume they are the same to avoid spurious follow-on errors.
410 return (x.len < 0 || y.len < 0 || x.len == y.len) && u.nify(x.elem, y.elem, p)
414 // Two slice types are identical if they have identical element types.
415 if y, ok := y.(*Slice); ok {
416 return u.nify(x.elem, y.elem, p)
420 // Two struct types are identical if they have the same sequence of fields,
421 // and if corresponding fields have the same names, and identical types,
422 // and identical tags. Two embedded fields are considered to have the same
423 // name. Lower-case field names from different packages are always different.
424 if y, ok := y.(*Struct); ok {
425 if x.NumFields() == y.NumFields() {
426 for i, f := range x.fields {
428 if f.embedded != g.embedded ||
429 x.Tag(i) != y.Tag(i) ||
430 !f.sameId(g.pkg, g.name) ||
431 !u.nify(f.typ, g.typ, p) {
440 // Two pointer types are identical if they have identical base types.
441 if y, ok := y.(*Pointer); ok {
442 return u.nify(x.base, y.base, p)
446 // Two tuples types are identical if they have the same number of elements
447 // and corresponding elements have identical types.
448 if y, ok := y.(*Tuple); ok {
449 if x.Len() == y.Len() {
451 for i, v := range x.vars {
453 if !u.nify(v.typ, w.typ, p) {
463 // Two function types are identical if they have the same number of parameters
464 // and result values, corresponding parameter and result types are identical,
465 // and either both functions are variadic or neither is. Parameter and result
466 // names are not required to match.
467 // TODO(gri) handle type parameters or document why we can ignore them.
468 if y, ok := y.(*Signature); ok {
469 return x.variadic == y.variadic &&
470 u.nify(x.params, y.params, p) &&
471 u.nify(x.results, y.results, p)
475 // Two interface types are identical if they have the same set of methods with
476 // the same names and identical function types. Lower-case method names from
477 // different packages are always different. The order of the methods is irrelevant.
478 if y, ok := y.(*Interface); ok {
481 if xset.comparable != yset.comparable {
484 if !xset.terms.equal(yset.terms) {
489 if len(a) == len(b) {
490 // Interface types are the only types where cycles can occur
491 // that are not "terminated" via named types; and such cycles
492 // can only be created via method parameter types that are
493 // anonymous interfaces (directly or indirectly) embedding
494 // the current interface. Example:
496 // type T interface {
500 // If two such (differently named) interfaces are compared,
501 // endless recursion occurs if the cycle is not detected.
503 // If x and y were compared before, they must be equal
504 // (if they were not, the recursion would have stopped);
505 // search the ifacePair stack for the same pair.
507 // This is a quadratic algorithm, but in practice these stacks
508 // are extremely short (bounded by the nesting depth of interface
509 // type declarations that recur via parameter types, an extremely
510 // rare occurrence). An alternative implementation might use a
511 // "visited" map, but that is probably less efficient overall.
512 q := &ifacePair{x, y, p}
515 return true // same pair was compared before
520 assertSortedMethods(a)
521 assertSortedMethods(b)
523 for i, f := range a {
525 if f.Id() != g.Id() || !u.nify(f.typ, g.typ, q) {
534 // Two map types are identical if they have identical key and value types.
535 if y, ok := y.(*Map); ok {
536 return u.nify(x.key, y.key, p) && u.nify(x.elem, y.elem, p)
540 // Two channel types are identical if they have identical value types.
541 if y, ok := y.(*Chan); ok {
542 return (!u.exact || x.dir == y.dir) && u.nify(x.elem, y.elem, p)
546 // TODO(gri) This code differs now from the parallel code in Checker.identical. Investigate.
547 if y, ok := y.(*Named); ok {
548 xargs := x.targs.list()
549 yargs := y.targs.list()
551 if len(xargs) != len(yargs) {
555 // TODO(gri) This is not always correct: two types may have the same names
556 // in the same package if one of them is nested in a function.
557 // Extremely unlikely but we need an always correct solution.
558 if x.obj.pkg == y.obj.pkg && x.obj.name == y.obj.name {
559 for i, x := range xargs {
560 if !u.nify(x, yargs[i], p) {
569 // Two type parameters (which are not part of the type parameters of the
570 // enclosing type as those are handled in the beginning of this function)
571 // are identical if they originate in the same declaration.
575 // avoid a crash in case of nil type
578 panic(sprintf(nil, true, "u.nify(%s, %s), u.x.tparams = %s", x, y, u.x.tparams))