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.
7 // Type unification attempts to make two types x and y structurally
8 // equivalent by determining the types for a given list of (bound)
9 // type parameters which may occur within x and y. If x and y are
10 // structurally different (say []T vs chan T), or conflicting
11 // types are determined for type parameters, unification fails.
12 // If unification succeeds, as a side-effect, the types of the
13 // bound type parameters may be determined.
15 // Unification typically requires multiple calls u.unify(x, y) to
16 // a given unifier u, with various combinations of types x and y.
17 // In each call, additional type parameter types may be determined
18 // as a side effect and recorded in u.
19 // If a call fails (returns false), unification fails.
21 // In the unification context, structural equivalence of two types
22 // ignores the difference between a defined type and its underlying
23 // type if one type is a defined type and the other one is not.
24 // It also ignores the difference between an (external, unbound)
25 // type parameter and its core type.
26 // If two types are not structurally equivalent, they cannot be Go
27 // identical types. On the other hand, if they are structurally
28 // equivalent, they may be Go identical or at least assignable, or
29 // they may be in the type set of a constraint.
30 // Whether they indeed are identical or assignable is determined
31 // upon instantiation and function argument passing.
43 // Upper limit for recursion depth. Used to catch infinite recursions
44 // due to implementation issues (e.g., see issues go.dev/issue/48619, go.dev/issue/48656).
45 unificationDepthLimit = 50
47 // Whether to panic when unificationDepthLimit is reached.
48 // If disabled, a recursion depth overflow results in a (quiet)
49 // unification failure.
50 panicAtUnificationDepthLimit = false // go.dev/issue/59740
52 // If enableCoreTypeUnification is set, unification will consider
53 // the core types, if any, of non-local (unbound) type parameters.
54 enableCoreTypeUnification = true
56 // If enableInterfaceInference is set, type inference uses
57 // shared methods for improved type inference involving
59 enableInterfaceInference = true
61 // If traceInference is set, unification will print a trace of its operation.
62 // Interpretation of trace:
63 // x ≡ y attempt to unify types x and y
64 // p ➞ y type parameter p is set to type y (p is inferred to be y)
65 // p ⇄ q type parameters p and q match (p is inferred to be q and vice versa)
66 // x ≢ y types x and y cannot be unified
67 // [p, q, ...] ➞ [x, y, ...] mapping from type parameters to types
68 traceInference = false
71 // A unifier maintains a list of type parameters and
72 // corresponding types inferred for each type parameter.
73 // A unifier is created by calling newUnifier.
75 // handles maps each type parameter to its inferred type through
76 // an indirection *Type called (inferred type) "handle".
77 // Initially, each type parameter has its own, separate handle,
78 // with a nil (i.e., not yet inferred) type.
79 // After a type parameter P is unified with a type parameter Q,
80 // P and Q share the same handle (and thus type). This ensures
81 // that inferring the type for a given type parameter P will
82 // automatically infer the same type for all other parameters
83 // unified (joined) with P.
84 handles map[*TypeParam]*Type
85 depth int // recursion depth during unification
88 // newUnifier returns a new unifier initialized with the given type parameter
89 // and corresponding type argument lists. The type argument list may be shorter
90 // than the type parameter list, and it may contain nil types. Matching type
91 // parameters and arguments must have the same index.
92 func newUnifier(tparams []*TypeParam, targs []Type) *unifier {
93 assert(len(tparams) >= len(targs))
94 handles := make(map[*TypeParam]*Type, len(tparams))
95 // Allocate all handles up-front: in a correct program, all type parameters
96 // must be resolved and thus eventually will get a handle.
97 // Also, sharing of handles caused by unified type parameters is rare and
98 // so it's ok to not optimize for that case (and delay handle allocation).
99 for i, x := range tparams {
106 return &unifier{handles, 0}
109 // unify attempts to unify x and y and reports whether it succeeded.
110 // As a side-effect, types may be inferred for type parameters.
111 func (u *unifier) unify(x, y Type) bool {
112 return u.nify(x, y, nil)
115 func (u *unifier) tracef(format string, args ...interface{}) {
116 fmt.Println(strings.Repeat(". ", u.depth) + sprintf(nil, true, format, args...))
119 // String returns a string representation of the current mapping
120 // from type parameters to types.
121 func (u *unifier) String() string {
122 // sort type parameters for reproducible strings
123 tparams := make(typeParamsById, len(u.handles))
125 for tpar := range u.handles {
132 w := newTypeWriter(&buf, nil)
134 for i, x := range tparams {
146 type typeParamsById []*TypeParam
148 func (s typeParamsById) Len() int { return len(s) }
149 func (s typeParamsById) Less(i, j int) bool { return s[i].id < s[j].id }
150 func (s typeParamsById) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
152 // join unifies the given type parameters x and y.
153 // If both type parameters already have a type associated with them
154 // and they are not joined, join fails and returns false.
155 func (u *unifier) join(x, y *TypeParam) bool {
157 u.tracef("%s ⇄ %s", x, y)
159 switch hx, hy := u.handles[x], u.handles[y]; {
161 // Both type parameters already share the same handle. Nothing to do.
162 case *hx != nil && *hy != nil:
163 // Both type parameters have (possibly different) inferred types. Cannot join.
166 // Only type parameter x has an inferred type. Use handle of x.
168 // This case is treated like the default case.
170 // // Only type parameter y has an inferred type. Use handle of y.
171 // u.setHandle(x, hy)
173 // Neither type parameter has an inferred type. Use handle of y.
179 // asTypeParam returns x.(*TypeParam) if x is a type parameter recorded with u.
180 // Otherwise, the result is nil.
181 func (u *unifier) asTypeParam(x Type) *TypeParam {
182 if x, _ := x.(*TypeParam); x != nil {
183 if _, found := u.handles[x]; found {
190 // setHandle sets the handle for type parameter x
191 // (and all its joined type parameters) to h.
192 func (u *unifier) setHandle(x *TypeParam, h *Type) {
195 for y, hy := range u.handles {
202 // at returns the (possibly nil) type for type parameter x.
203 func (u *unifier) at(x *TypeParam) Type {
207 // set sets the type t for type parameter x;
208 // t must not be nil.
209 func (u *unifier) set(x *TypeParam, t Type) {
212 u.tracef("%s ➞ %s", x, t)
217 // unknowns returns the number of type parameters for which no type has been set yet.
218 func (u *unifier) unknowns() int {
220 for _, h := range u.handles {
228 // inferred returns the list of inferred types for the given type parameter list.
229 // The result is never nil and has the same length as tparams; result types that
230 // could not be inferred are nil. Corresponding type parameters and result types
231 // have identical indices.
232 func (u *unifier) inferred(tparams []*TypeParam) []Type {
233 list := make([]Type, len(tparams))
234 for i, x := range tparams {
240 // nify implements the core unification algorithm which is an
241 // adapted version of Checker.identical. For changes to that
242 // code the corresponding changes should be made here.
243 // Must not be called directly from outside the unifier.
244 func (u *unifier) nify(x, y Type, p *ifacePair) (result bool) {
247 u.tracef("%s ≡ %s", x, y)
250 if traceInference && !result {
251 u.tracef("%s ≢ %s", x, y)
256 // nothing to do if x == y
261 // Stop gap for cases where unification fails.
262 if u.depth > unificationDepthLimit {
264 u.tracef("depth %d >= %d", u.depth, unificationDepthLimit)
266 if panicAtUnificationDepthLimit {
267 panic("unification reached recursion depth limit")
272 // Unification is symmetric, so we can swap the operands.
273 // Ensure that if we have at least one
274 // - defined type, make sure one is in y
275 // - type parameter recorded with u, make sure one is in x
276 if _, ok := x.(*Named); ok || u.asTypeParam(y) != nil {
278 u.tracef("%s ≡ %s (swap)", y, x)
283 // Unification will fail if we match a defined type against a type literal.
284 // Per the (spec) assignment rules, assignments of values to variables with
285 // the same type structure are permitted as long as at least one of them
286 // is not a defined type. To accommodate for that possibility, we continue
287 // unification with the underlying type of a defined type if the other type
288 // is a type literal.
289 // We also continue if the other type is a basic type because basic types
290 // are valid underlying types and may appear as core types of type constraints.
291 // If we exclude them, inferred defined types for type parameters may not
292 // match against the core types of their constraints (even though they might
293 // correctly match against some of the types in the constraint's type set).
294 // Finally, if unification (incorrectly) succeeds by matching the underlying
295 // type of a defined type against a basic type (because we include basic types
296 // as type literals here), and if that leads to an incorrectly inferred type,
297 // we will fail at function instantiation or argument assignment time.
299 // If we have at least one defined type, there is one in y.
300 if ny, _ := y.(*Named); ny != nil && isTypeLit(x) && !(enableInterfaceInference && IsInterface(x)) {
302 u.tracef("%s ≡ under %s", x, ny)
305 // Per the spec, a defined type cannot have an underlying type
306 // that is a type parameter.
307 assert(!isTypeParam(y))
308 // x and y may be identical now
314 // Cases where at least one of x or y is a type parameter recorded with u.
315 // If we have at least one type parameter, there is one in x.
316 // If we have exactly one type parameter, because it is in x,
317 // isTypeLit(x) is false and y was not changed above. In other
318 // words, if y was a defined type, it is still a defined type
319 // (relevant for the logic below).
320 switch px, py := u.asTypeParam(x), u.asTypeParam(y); {
321 case px != nil && py != nil:
322 // both x and y are type parameters
326 // both x and y have an inferred type - they must match
327 return u.nify(u.at(px), u.at(py), p)
330 // x is a type parameter, y is not
331 if x := u.at(px); x != nil {
332 // x has an inferred type which must match y
334 // If we have a match, possibly through underlying types,
335 // and y is a defined type, make sure we record that type
336 // for type parameter x, which may have until now only
337 // recorded an underlying type (go.dev/issue/43056).
338 if _, ok := y.(*Named); ok {
345 // otherwise, infer type from y
350 // x != y if we get here
353 // If we get here and x or y is a type parameter, they are unbound
354 // (not recorded with the unifier).
355 // Ensure that if we have at least one type parameter, it is in x
356 // (the earlier swap checks for _recorded_ type parameters only).
359 u.tracef("%s ≡ %s (swap)", y, x)
364 // If EnableInterfaceInference is set and both types are interfaces, one
365 // interface must have a subset of the methods of the other and corresponding
366 // method signatures must unify.
367 // If only one type is an interface, all its methods must be present in the
368 // other type and corresponding method signatures must unify.
369 if enableInterfaceInference {
370 xi, _ := x.(*Interface)
371 yi, _ := y.(*Interface)
372 // If we have two interfaces, check the type terms for equivalence,
373 // and unify common methods if possible.
374 if xi != nil && yi != nil {
377 if xset.comparable != yset.comparable {
380 // For now we require terms to be equal.
381 // We should be able to relax this as well, eventually.
382 if !xset.terms.equal(yset.terms) {
385 // Interface types are the only types where cycles can occur
386 // that are not "terminated" via named types; and such cycles
387 // can only be created via method parameter types that are
388 // anonymous interfaces (directly or indirectly) embedding
389 // the current interface. Example:
391 // type T interface {
395 // If two such (differently named) interfaces are compared,
396 // endless recursion occurs if the cycle is not detected.
398 // If x and y were compared before, they must be equal
399 // (if they were not, the recursion would have stopped);
400 // search the ifacePair stack for the same pair.
402 // This is a quadratic algorithm, but in practice these stacks
403 // are extremely short (bounded by the nesting depth of interface
404 // type declarations that recur via parameter types, an extremely
405 // rare occurrence). An alternative implementation might use a
406 // "visited" map, but that is probably less efficient overall.
407 q := &ifacePair{xi, yi, p}
410 return true // same pair was compared before
414 // The method set of x must be a subset of the method set
415 // of y or vice versa, and the common methods must unify.
416 xmethods := xset.methods
417 ymethods := yset.methods
418 // The smaller method set must be the subset, if it exists.
419 if len(xmethods) > len(ymethods) {
420 xmethods, ymethods = ymethods, xmethods
422 // len(xmethods) <= len(ymethods)
423 // Collect the ymethods in a map for quick lookup.
424 ymap := make(map[string]*Func, len(ymethods))
425 for _, ym := range ymethods {
428 // All xmethods must exist in ymethods and corresponding signatures must unify.
429 for _, xm := range xmethods {
430 if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, p) {
437 // We don't have two interfaces. If we have one, make sure it's in xi.
443 // If we have one interface, at a minimum each of the interface methods
444 // must be implemented and thus unify with a corresponding method from
445 // the non-interface type, otherwise unification fails.
447 // All xi methods must exist in y and corresponding signatures must unify.
448 xmethods := xi.typeSet().methods
449 for _, xm := range xmethods {
450 obj, _, _ := LookupFieldOrMethod(y, false, xm.pkg, xm.name)
451 if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, p) {
458 // Neither x nor y are interface types.
459 // They must be structurally equivalent to unify.
462 switch x := x.(type) {
464 // Basic types are singletons except for the rune and byte
465 // aliases, thus we cannot solely rely on the x == y check
466 // above. See also comment in TypeName.IsAlias.
467 if y, ok := y.(*Basic); ok {
468 return x.kind == y.kind
472 // Two array types unify if they have the same array length
473 // and their element types unify.
474 if y, ok := y.(*Array); ok {
475 // If one or both array lengths are unknown (< 0) due to some error,
476 // assume they are the same to avoid spurious follow-on errors.
477 return (x.len < 0 || y.len < 0 || x.len == y.len) && u.nify(x.elem, y.elem, p)
481 // Two slice types unify if their element types unify.
482 if y, ok := y.(*Slice); ok {
483 return u.nify(x.elem, y.elem, p)
487 // Two struct types unify if they have the same sequence of fields,
488 // and if corresponding fields have the same names, their (field) types unify,
489 // and they have identical tags. Two embedded fields are considered to have the same
490 // name. Lower-case field names from different packages are always different.
491 if y, ok := y.(*Struct); ok {
492 if x.NumFields() == y.NumFields() {
493 for i, f := range x.fields {
495 if f.embedded != g.embedded ||
496 x.Tag(i) != y.Tag(i) ||
497 !f.sameId(g.pkg, g.name) ||
498 !u.nify(f.typ, g.typ, p) {
507 // Two pointer types unify if their base types unify.
508 if y, ok := y.(*Pointer); ok {
509 return u.nify(x.base, y.base, p)
513 // Two tuples types unify if they have the same number of elements
514 // and the types of corresponding elements unify.
515 if y, ok := y.(*Tuple); ok {
516 if x.Len() == y.Len() {
518 for i, v := range x.vars {
520 if !u.nify(v.typ, w.typ, p) {
530 // Two function types unify if they have the same number of parameters
531 // and result values, corresponding parameter and result types unify,
532 // and either both functions are variadic or neither is.
533 // Parameter and result names are not required to match.
534 // TODO(gri) handle type parameters or document why we can ignore them.
535 if y, ok := y.(*Signature); ok {
536 return x.variadic == y.variadic &&
537 u.nify(x.params, y.params, p) &&
538 u.nify(x.results, y.results, p)
542 assert(!enableInterfaceInference) // handled before this switch
544 // Two interface types unify if they have the same set of methods with
545 // the same names, and corresponding function types unify.
546 // Lower-case method names from different packages are always different.
547 // The order of the methods is irrelevant.
548 if y, ok := y.(*Interface); ok {
551 if xset.comparable != yset.comparable {
554 if !xset.terms.equal(yset.terms) {
559 if len(a) == len(b) {
560 // Interface types are the only types where cycles can occur
561 // that are not "terminated" via named types; and such cycles
562 // can only be created via method parameter types that are
563 // anonymous interfaces (directly or indirectly) embedding
564 // the current interface. Example:
566 // type T interface {
570 // If two such (differently named) interfaces are compared,
571 // endless recursion occurs if the cycle is not detected.
573 // If x and y were compared before, they must be equal
574 // (if they were not, the recursion would have stopped);
575 // search the ifacePair stack for the same pair.
577 // This is a quadratic algorithm, but in practice these stacks
578 // are extremely short (bounded by the nesting depth of interface
579 // type declarations that recur via parameter types, an extremely
580 // rare occurrence). An alternative implementation might use a
581 // "visited" map, but that is probably less efficient overall.
582 q := &ifacePair{x, y, p}
585 return true // same pair was compared before
590 assertSortedMethods(a)
591 assertSortedMethods(b)
593 for i, f := range a {
595 if f.Id() != g.Id() || !u.nify(f.typ, g.typ, q) {
604 // Two map types unify if their key and value types unify.
605 if y, ok := y.(*Map); ok {
606 return u.nify(x.key, y.key, p) && u.nify(x.elem, y.elem, p)
610 // Two channel types unify if their value types unify.
611 if y, ok := y.(*Chan); ok {
612 return u.nify(x.elem, y.elem, p)
616 // Two named types unify if their type names originate
617 // in the same type declaration. If they are instantiated,
618 // their type argument lists must unify.
619 if y, ok := y.(*Named); ok {
620 sameOrig := indenticalOrigin(x, y)
621 if enableInterfaceInference {
624 xi, _ := xu.(*Interface)
625 yi, _ := yu.(*Interface)
626 // If one or both defined types are interfaces, use interface unification,
627 // unless they originated in the same type declaration.
628 if xi != nil && yi != nil {
629 // If both interfaces originate in the same declaration,
630 // their methods unify if the type parameters unify.
631 // Unify the type parameters rather than the methods in
632 // case the type parameters are not used in the methods
633 // (and to preserve existing behavior in this case).
635 xargs := x.TypeArgs().list()
636 yargs := y.TypeArgs().list()
637 assert(len(xargs) == len(yargs))
638 for i, xarg := range xargs {
639 if !u.nify(xarg, yargs[i], p) {
645 return u.nify(xu, yu, p)
647 // We don't have two interfaces. If we have one, make sure it's in xi.
652 // If xi is an interface, use interface unification.
654 return u.nify(xi, y, p)
656 // In all other cases, the type arguments and origins must match.
659 // Check type arguments before origins so they unify
660 // even if the origins don't match; for better error
661 // messages (see go.dev/issue/53692).
662 xargs := x.TypeArgs().list()
663 yargs := y.TypeArgs().list()
664 if len(xargs) != len(yargs) {
667 for i, xarg := range xargs {
668 if !u.nify(xarg, yargs[i], p) {
676 // x must be an unbound type parameter (see comment above).
678 assert(u.asTypeParam(x) == nil)
680 // By definition, a valid type argument must be in the type set of
681 // the respective type constraint. Therefore, the type argument's
682 // underlying type must be in the set of underlying types of that
683 // constraint. If there is a single such underlying type, it's the
684 // constraint's core type. It must match the type argument's under-
685 // lying type, irrespective of whether the actual type argument,
686 // which may be a defined type, is actually in the type set (that
687 // will be determined at instantiation time).
688 // Thus, if we have the core type of an unbound type parameter,
689 // we know the structure of the possible types satisfying such
690 // parameters. Use that core type for further unification
691 // (see go.dev/issue/50755 for a test case).
692 if enableCoreTypeUnification {
693 // Because the core type is always an underlying type,
694 // unification will take care of matching against a
695 // defined or literal type automatically.
696 // If y is also an unbound type parameter, we will end
697 // up here again with x and y swapped, so we don't
698 // need to take care of that case separately.
699 if cx := coreType(x); cx != nil {
701 u.tracef("core %s ≡ %s", x, y)
703 return u.nify(cx, y, p)
706 // x != y and there's nothing to do
709 // avoid a crash in case of nil type
712 panic(sprintf(nil, true, "u.nify(%s, %s)", x, y))