]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/unify.go
go/types, types2: add unifyMode to unifier, pass it through
[gostls13.git] / src / cmd / compile / internal / types2 / unify.go
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.
4
5 // This file implements type unification.
6 //
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.
14 //
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.
20 //
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.
32
33 package types2
34
35 import (
36         "bytes"
37         "fmt"
38         "sort"
39         "strings"
40 )
41
42 const (
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
46
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
51
52         // If enableCoreTypeUnification is set, unification will consider
53         // the core types, if any, of non-local (unbound) type parameters.
54         enableCoreTypeUnification = true
55
56         // If enableInterfaceInference is set, type inference uses
57         // shared methods for improved type inference involving
58         // interfaces.
59         enableInterfaceInference = true
60
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
69 )
70
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.
74 type unifier struct {
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
86 }
87
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 {
100                 var t Type
101                 if i < len(targs) {
102                         t = targs[i]
103                 }
104                 handles[x] = &t
105         }
106         return &unifier{handles, 0}
107 }
108
109 // unifyMode controls the behavior of the unifier.
110 type unifyMode uint
111
112 // unify attempts to unify x and y and reports whether it succeeded.
113 // As a side-effect, types may be inferred for type parameters.
114 func (u *unifier) unify(x, y Type, mode unifyMode) bool {
115         return u.nify(x, y, mode, nil)
116 }
117
118 func (u *unifier) tracef(format string, args ...interface{}) {
119         fmt.Println(strings.Repeat(".  ", u.depth) + sprintf(nil, true, format, args...))
120 }
121
122 // String returns a string representation of the current mapping
123 // from type parameters to types.
124 func (u *unifier) String() string {
125         // sort type parameters for reproducible strings
126         tparams := make(typeParamsById, len(u.handles))
127         i := 0
128         for tpar := range u.handles {
129                 tparams[i] = tpar
130                 i++
131         }
132         sort.Sort(tparams)
133
134         var buf bytes.Buffer
135         w := newTypeWriter(&buf, nil)
136         w.byte('[')
137         for i, x := range tparams {
138                 if i > 0 {
139                         w.string(", ")
140                 }
141                 w.typ(x)
142                 w.string(": ")
143                 w.typ(u.at(x))
144         }
145         w.byte(']')
146         return buf.String()
147 }
148
149 type typeParamsById []*TypeParam
150
151 func (s typeParamsById) Len() int           { return len(s) }
152 func (s typeParamsById) Less(i, j int) bool { return s[i].id < s[j].id }
153 func (s typeParamsById) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
154
155 // join unifies the given type parameters x and y.
156 // If both type parameters already have a type associated with them
157 // and they are not joined, join fails and returns false.
158 func (u *unifier) join(x, y *TypeParam) bool {
159         if traceInference {
160                 u.tracef("%s ⇄ %s", x, y)
161         }
162         switch hx, hy := u.handles[x], u.handles[y]; {
163         case hx == hy:
164                 // Both type parameters already share the same handle. Nothing to do.
165         case *hx != nil && *hy != nil:
166                 // Both type parameters have (possibly different) inferred types. Cannot join.
167                 return false
168         case *hx != nil:
169                 // Only type parameter x has an inferred type. Use handle of x.
170                 u.setHandle(y, hx)
171         // This case is treated like the default case.
172         // case *hy != nil:
173         //      // Only type parameter y has an inferred type. Use handle of y.
174         //      u.setHandle(x, hy)
175         default:
176                 // Neither type parameter has an inferred type. Use handle of y.
177                 u.setHandle(x, hy)
178         }
179         return true
180 }
181
182 // asTypeParam returns x.(*TypeParam) if x is a type parameter recorded with u.
183 // Otherwise, the result is nil.
184 func (u *unifier) asTypeParam(x Type) *TypeParam {
185         if x, _ := x.(*TypeParam); x != nil {
186                 if _, found := u.handles[x]; found {
187                         return x
188                 }
189         }
190         return nil
191 }
192
193 // setHandle sets the handle for type parameter x
194 // (and all its joined type parameters) to h.
195 func (u *unifier) setHandle(x *TypeParam, h *Type) {
196         hx := u.handles[x]
197         assert(hx != nil)
198         for y, hy := range u.handles {
199                 if hy == hx {
200                         u.handles[y] = h
201                 }
202         }
203 }
204
205 // at returns the (possibly nil) type for type parameter x.
206 func (u *unifier) at(x *TypeParam) Type {
207         return *u.handles[x]
208 }
209
210 // set sets the type t for type parameter x;
211 // t must not be nil.
212 func (u *unifier) set(x *TypeParam, t Type) {
213         assert(t != nil)
214         if traceInference {
215                 u.tracef("%s ➞ %s", x, t)
216         }
217         *u.handles[x] = t
218 }
219
220 // unknowns returns the number of type parameters for which no type has been set yet.
221 func (u *unifier) unknowns() int {
222         n := 0
223         for _, h := range u.handles {
224                 if *h == nil {
225                         n++
226                 }
227         }
228         return n
229 }
230
231 // inferred returns the list of inferred types for the given type parameter list.
232 // The result is never nil and has the same length as tparams; result types that
233 // could not be inferred are nil. Corresponding type parameters and result types
234 // have identical indices.
235 func (u *unifier) inferred(tparams []*TypeParam) []Type {
236         list := make([]Type, len(tparams))
237         for i, x := range tparams {
238                 list[i] = u.at(x)
239         }
240         return list
241 }
242
243 // nify implements the core unification algorithm which is an
244 // adapted version of Checker.identical. For changes to that
245 // code the corresponding changes should be made here.
246 // Must not be called directly from outside the unifier.
247 func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
248         u.depth++
249         if traceInference {
250                 u.tracef("%s ≡ %s (mode %d)", x, y, mode)
251         }
252         defer func() {
253                 if traceInference && !result {
254                         u.tracef("%s ≢ %s", x, y)
255                 }
256                 u.depth--
257         }()
258
259         // nothing to do if x == y
260         if x == y {
261                 return true
262         }
263
264         // Stop gap for cases where unification fails.
265         if u.depth > unificationDepthLimit {
266                 if traceInference {
267                         u.tracef("depth %d >= %d", u.depth, unificationDepthLimit)
268                 }
269                 if panicAtUnificationDepthLimit {
270                         panic("unification reached recursion depth limit")
271                 }
272                 return false
273         }
274
275         // Unification is symmetric, so we can swap the operands.
276         // Ensure that if we have at least one
277         // - defined type, make sure one is in y
278         // - type parameter recorded with u, make sure one is in x
279         if _, ok := x.(*Named); ok || u.asTypeParam(y) != nil {
280                 if traceInference {
281                         u.tracef("%s ≡ %s (swap)", y, x)
282                 }
283                 x, y = y, x
284         }
285
286         // Unification will fail if we match a defined type against a type literal.
287         // Per the (spec) assignment rules, assignments of values to variables with
288         // the same type structure are permitted as long as at least one of them
289         // is not a defined type. To accommodate for that possibility, we continue
290         // unification with the underlying type of a defined type if the other type
291         // is a type literal.
292         // We also continue if the other type is a basic type because basic types
293         // are valid underlying types and may appear as core types of type constraints.
294         // If we exclude them, inferred defined types for type parameters may not
295         // match against the core types of their constraints (even though they might
296         // correctly match against some of the types in the constraint's type set).
297         // Finally, if unification (incorrectly) succeeds by matching the underlying
298         // type of a defined type against a basic type (because we include basic types
299         // as type literals here), and if that leads to an incorrectly inferred type,
300         // we will fail at function instantiation or argument assignment time.
301         //
302         // If we have at least one defined type, there is one in y.
303         if ny, _ := y.(*Named); ny != nil && isTypeLit(x) && !(enableInterfaceInference && IsInterface(x)) {
304                 if traceInference {
305                         u.tracef("%s ≡ under %s", x, ny)
306                 }
307                 y = ny.under()
308                 // Per the spec, a defined type cannot have an underlying type
309                 // that is a type parameter.
310                 assert(!isTypeParam(y))
311                 // x and y may be identical now
312                 if x == y {
313                         return true
314                 }
315         }
316
317         // Cases where at least one of x or y is a type parameter recorded with u.
318         // If we have at least one type parameter, there is one in x.
319         // If we have exactly one type parameter, because it is in x,
320         // isTypeLit(x) is false and y was not changed above. In other
321         // words, if y was a defined type, it is still a defined type
322         // (relevant for the logic below).
323         switch px, py := u.asTypeParam(x), u.asTypeParam(y); {
324         case px != nil && py != nil:
325                 // both x and y are type parameters
326                 if u.join(px, py) {
327                         return true
328                 }
329                 // both x and y have an inferred type - they must match
330                 return u.nify(u.at(px), u.at(py), mode, p)
331
332         case px != nil:
333                 // x is a type parameter, y is not
334                 if x := u.at(px); x != nil {
335                         // x has an inferred type which must match y
336                         if u.nify(x, y, mode, p) {
337                                 // If we have a match, possibly through underlying types,
338                                 // and y is a defined type, make sure we record that type
339                                 // for type parameter x, which may have until now only
340                                 // recorded an underlying type (go.dev/issue/43056).
341                                 if _, ok := y.(*Named); ok {
342                                         u.set(px, y)
343                                 }
344                                 return true
345                         }
346                         return false
347                 }
348                 // otherwise, infer type from y
349                 u.set(px, y)
350                 return true
351         }
352
353         // x != y if we get here
354         assert(x != y)
355
356         // If we get here and x or y is a type parameter, they are unbound
357         // (not recorded with the unifier).
358         // Ensure that if we have at least one type parameter, it is in x
359         // (the earlier swap checks for _recorded_ type parameters only).
360         if isTypeParam(y) {
361                 if traceInference {
362                         u.tracef("%s ≡ %s (swap)", y, x)
363                 }
364                 x, y = y, x
365         }
366
367         // Type elements (array, slice, etc. elements) use emode for unification.
368         emode := mode
369
370         // If EnableInterfaceInference is set and both types are interfaces, one
371         // interface must have a subset of the methods of the other and corresponding
372         // method signatures must unify.
373         // If only one type is an interface, all its methods must be present in the
374         // other type and corresponding method signatures must unify.
375         if enableInterfaceInference {
376                 xi, _ := x.(*Interface)
377                 yi, _ := y.(*Interface)
378                 // If we have two interfaces, check the type terms for equivalence,
379                 // and unify common methods if possible.
380                 if xi != nil && yi != nil {
381                         xset := xi.typeSet()
382                         yset := yi.typeSet()
383                         if xset.comparable != yset.comparable {
384                                 return false
385                         }
386                         // For now we require terms to be equal.
387                         // We should be able to relax this as well, eventually.
388                         if !xset.terms.equal(yset.terms) {
389                                 return false
390                         }
391                         // Interface types are the only types where cycles can occur
392                         // that are not "terminated" via named types; and such cycles
393                         // can only be created via method parameter types that are
394                         // anonymous interfaces (directly or indirectly) embedding
395                         // the current interface. Example:
396                         //
397                         //    type T interface {
398                         //        m() interface{T}
399                         //    }
400                         //
401                         // If two such (differently named) interfaces are compared,
402                         // endless recursion occurs if the cycle is not detected.
403                         //
404                         // If x and y were compared before, they must be equal
405                         // (if they were not, the recursion would have stopped);
406                         // search the ifacePair stack for the same pair.
407                         //
408                         // This is a quadratic algorithm, but in practice these stacks
409                         // are extremely short (bounded by the nesting depth of interface
410                         // type declarations that recur via parameter types, an extremely
411                         // rare occurrence). An alternative implementation might use a
412                         // "visited" map, but that is probably less efficient overall.
413                         q := &ifacePair{xi, yi, p}
414                         for p != nil {
415                                 if p.identical(q) {
416                                         return true // same pair was compared before
417                                 }
418                                 p = p.prev
419                         }
420                         // The method set of x must be a subset of the method set
421                         // of y or vice versa, and the common methods must unify.
422                         xmethods := xset.methods
423                         ymethods := yset.methods
424                         // The smaller method set must be the subset, if it exists.
425                         if len(xmethods) > len(ymethods) {
426                                 xmethods, ymethods = ymethods, xmethods
427                         }
428                         // len(xmethods) <= len(ymethods)
429                         // Collect the ymethods in a map for quick lookup.
430                         ymap := make(map[string]*Func, len(ymethods))
431                         for _, ym := range ymethods {
432                                 ymap[ym.Id()] = ym
433                         }
434                         // All xmethods must exist in ymethods and corresponding signatures must unify.
435                         for _, xm := range xmethods {
436                                 if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
437                                         return false
438                                 }
439                         }
440                         return true
441                 }
442
443                 // We don't have two interfaces. If we have one, make sure it's in xi.
444                 if yi != nil {
445                         xi = yi
446                         y = x
447                 }
448
449                 // If we have one interface, at a minimum each of the interface methods
450                 // must be implemented and thus unify with a corresponding method from
451                 // the non-interface type, otherwise unification fails.
452                 if xi != nil {
453                         // All xi methods must exist in y and corresponding signatures must unify.
454                         xmethods := xi.typeSet().methods
455                         for _, xm := range xmethods {
456                                 obj, _, _ := LookupFieldOrMethod(y, false, xm.pkg, xm.name)
457                                 if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
458                                         return false
459                                 }
460                         }
461                         return true
462                 }
463
464                 // Neither x nor y are interface types.
465                 // They must be structurally equivalent to unify.
466         }
467
468         switch x := x.(type) {
469         case *Basic:
470                 // Basic types are singletons except for the rune and byte
471                 // aliases, thus we cannot solely rely on the x == y check
472                 // above. See also comment in TypeName.IsAlias.
473                 if y, ok := y.(*Basic); ok {
474                         return x.kind == y.kind
475                 }
476
477         case *Array:
478                 // Two array types unify if they have the same array length
479                 // and their element types unify.
480                 if y, ok := y.(*Array); ok {
481                         // If one or both array lengths are unknown (< 0) due to some error,
482                         // assume they are the same to avoid spurious follow-on errors.
483                         return (x.len < 0 || y.len < 0 || x.len == y.len) && u.nify(x.elem, y.elem, emode, p)
484                 }
485
486         case *Slice:
487                 // Two slice types unify if their element types unify.
488                 if y, ok := y.(*Slice); ok {
489                         return u.nify(x.elem, y.elem, emode, p)
490                 }
491
492         case *Struct:
493                 // Two struct types unify if they have the same sequence of fields,
494                 // and if corresponding fields have the same names, their (field) types unify,
495                 // and they have identical tags. Two embedded fields are considered to have the same
496                 // name. Lower-case field names from different packages are always different.
497                 if y, ok := y.(*Struct); ok {
498                         if x.NumFields() == y.NumFields() {
499                                 for i, f := range x.fields {
500                                         g := y.fields[i]
501                                         if f.embedded != g.embedded ||
502                                                 x.Tag(i) != y.Tag(i) ||
503                                                 !f.sameId(g.pkg, g.name) ||
504                                                 !u.nify(f.typ, g.typ, emode, p) {
505                                                 return false
506                                         }
507                                 }
508                                 return true
509                         }
510                 }
511
512         case *Pointer:
513                 // Two pointer types unify if their base types unify.
514                 if y, ok := y.(*Pointer); ok {
515                         return u.nify(x.base, y.base, emode, p)
516                 }
517
518         case *Tuple:
519                 // Two tuples types unify if they have the same number of elements
520                 // and the types of corresponding elements unify.
521                 if y, ok := y.(*Tuple); ok {
522                         if x.Len() == y.Len() {
523                                 if x != nil {
524                                         for i, v := range x.vars {
525                                                 w := y.vars[i]
526                                                 if !u.nify(v.typ, w.typ, mode, p) {
527                                                         return false
528                                                 }
529                                         }
530                                 }
531                                 return true
532                         }
533                 }
534
535         case *Signature:
536                 // Two function types unify if they have the same number of parameters
537                 // and result values, corresponding parameter and result types unify,
538                 // and either both functions are variadic or neither is.
539                 // Parameter and result names are not required to match.
540                 // TODO(gri) handle type parameters or document why we can ignore them.
541                 if y, ok := y.(*Signature); ok {
542                         return x.variadic == y.variadic &&
543                                 u.nify(x.params, y.params, emode, p) &&
544                                 u.nify(x.results, y.results, emode, p)
545                 }
546
547         case *Interface:
548                 assert(!enableInterfaceInference) // handled before this switch
549
550                 // Two interface types unify if they have the same set of methods with
551                 // the same names, and corresponding function types unify.
552                 // Lower-case method names from different packages are always different.
553                 // The order of the methods is irrelevant.
554                 if y, ok := y.(*Interface); ok {
555                         xset := x.typeSet()
556                         yset := y.typeSet()
557                         if xset.comparable != yset.comparable {
558                                 return false
559                         }
560                         if !xset.terms.equal(yset.terms) {
561                                 return false
562                         }
563                         a := xset.methods
564                         b := yset.methods
565                         if len(a) == len(b) {
566                                 // Interface types are the only types where cycles can occur
567                                 // that are not "terminated" via named types; and such cycles
568                                 // can only be created via method parameter types that are
569                                 // anonymous interfaces (directly or indirectly) embedding
570                                 // the current interface. Example:
571                                 //
572                                 //    type T interface {
573                                 //        m() interface{T}
574                                 //    }
575                                 //
576                                 // If two such (differently named) interfaces are compared,
577                                 // endless recursion occurs if the cycle is not detected.
578                                 //
579                                 // If x and y were compared before, they must be equal
580                                 // (if they were not, the recursion would have stopped);
581                                 // search the ifacePair stack for the same pair.
582                                 //
583                                 // This is a quadratic algorithm, but in practice these stacks
584                                 // are extremely short (bounded by the nesting depth of interface
585                                 // type declarations that recur via parameter types, an extremely
586                                 // rare occurrence). An alternative implementation might use a
587                                 // "visited" map, but that is probably less efficient overall.
588                                 q := &ifacePair{x, y, p}
589                                 for p != nil {
590                                         if p.identical(q) {
591                                                 return true // same pair was compared before
592                                         }
593                                         p = p.prev
594                                 }
595                                 if debug {
596                                         assertSortedMethods(a)
597                                         assertSortedMethods(b)
598                                 }
599                                 for i, f := range a {
600                                         g := b[i]
601                                         if f.Id() != g.Id() || !u.nify(f.typ, g.typ, emode, q) {
602                                                 return false
603                                         }
604                                 }
605                                 return true
606                         }
607                 }
608
609         case *Map:
610                 // Two map types unify if their key and value types unify.
611                 if y, ok := y.(*Map); ok {
612                         return u.nify(x.key, y.key, emode, p) && u.nify(x.elem, y.elem, emode, p)
613                 }
614
615         case *Chan:
616                 // Two channel types unify if their value types unify.
617                 if y, ok := y.(*Chan); ok {
618                         return u.nify(x.elem, y.elem, emode, p)
619                 }
620
621         case *Named:
622                 // Two named non-interface types unify if their type names originate
623                 // in the same type declaration. If they are instantiated, their type
624                 // argument lists must unify.
625                 // If one or both named types are interfaces, the types unify if the
626                 // respective methods unify (per the rules for interface unification).
627                 if y, ok := y.(*Named); ok {
628                         if enableInterfaceInference {
629                                 xi, _ := x.under().(*Interface)
630                                 yi, _ := y.under().(*Interface)
631                                 // If one or both of x and y are interfaces, use interface unification.
632                                 switch {
633                                 case xi != nil && yi != nil:
634                                         return u.nify(xi, yi, mode, p)
635                                 case xi != nil:
636                                         return u.nify(xi, y, mode, p)
637                                 case yi != nil:
638                                         return u.nify(x, yi, mode, p)
639                                 }
640                                 // In all other cases, the type arguments and origins must match.
641                         }
642
643                         // Check type arguments before origins so they unify
644                         // even if the origins don't match; for better error
645                         // messages (see go.dev/issue/53692).
646                         xargs := x.TypeArgs().list()
647                         yargs := y.TypeArgs().list()
648                         if len(xargs) != len(yargs) {
649                                 return false
650                         }
651                         for i, xarg := range xargs {
652                                 if !u.nify(xarg, yargs[i], mode, p) {
653                                         return false
654                                 }
655                         }
656                         return indenticalOrigin(x, y)
657                 }
658
659         case *TypeParam:
660                 // x must be an unbound type parameter (see comment above).
661                 if debug {
662                         assert(u.asTypeParam(x) == nil)
663                 }
664                 // By definition, a valid type argument must be in the type set of
665                 // the respective type constraint. Therefore, the type argument's
666                 // underlying type must be in the set of underlying types of that
667                 // constraint. If there is a single such underlying type, it's the
668                 // constraint's core type. It must match the type argument's under-
669                 // lying type, irrespective of whether the actual type argument,
670                 // which may be a defined type, is actually in the type set (that
671                 // will be determined at instantiation time).
672                 // Thus, if we have the core type of an unbound type parameter,
673                 // we know the structure of the possible types satisfying such
674                 // parameters. Use that core type for further unification
675                 // (see go.dev/issue/50755 for a test case).
676                 if enableCoreTypeUnification {
677                         // Because the core type is always an underlying type,
678                         // unification will take care of matching against a
679                         // defined or literal type automatically.
680                         // If y is also an unbound type parameter, we will end
681                         // up here again with x and y swapped, so we don't
682                         // need to take care of that case separately.
683                         if cx := coreType(x); cx != nil {
684                                 if traceInference {
685                                         u.tracef("core %s ≡ %s", x, y)
686                                 }
687                                 return u.nify(cx, y, mode, p)
688                         }
689                 }
690                 // x != y and there's nothing to do
691
692         case nil:
693                 // avoid a crash in case of nil type
694
695         default:
696                 panic(sprintf(nil, true, "u.nify(%s, %s, %d)", x, y, mode))
697         }
698
699         return false
700 }