]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/unify.go
go/types, types2: enable interface inference
[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 // 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)
113 }
114
115 func (u *unifier) tracef(format string, args ...interface{}) {
116         fmt.Println(strings.Repeat(".  ", u.depth) + sprintf(nil, true, format, args...))
117 }
118
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))
124         i := 0
125         for tpar := range u.handles {
126                 tparams[i] = tpar
127                 i++
128         }
129         sort.Sort(tparams)
130
131         var buf bytes.Buffer
132         w := newTypeWriter(&buf, nil)
133         w.byte('[')
134         for i, x := range tparams {
135                 if i > 0 {
136                         w.string(", ")
137                 }
138                 w.typ(x)
139                 w.string(": ")
140                 w.typ(u.at(x))
141         }
142         w.byte(']')
143         return buf.String()
144 }
145
146 type typeParamsById []*TypeParam
147
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] }
151
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 {
156         if traceInference {
157                 u.tracef("%s ⇄ %s", x, y)
158         }
159         switch hx, hy := u.handles[x], u.handles[y]; {
160         case hx == hy:
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.
164                 return false
165         case *hx != nil:
166                 // Only type parameter x has an inferred type. Use handle of x.
167                 u.setHandle(y, hx)
168         // This case is treated like the default case.
169         // case *hy != nil:
170         //      // Only type parameter y has an inferred type. Use handle of y.
171         //      u.setHandle(x, hy)
172         default:
173                 // Neither type parameter has an inferred type. Use handle of y.
174                 u.setHandle(x, hy)
175         }
176         return true
177 }
178
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 {
184                         return x
185                 }
186         }
187         return nil
188 }
189
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) {
193         hx := u.handles[x]
194         assert(hx != nil)
195         for y, hy := range u.handles {
196                 if hy == hx {
197                         u.handles[y] = h
198                 }
199         }
200 }
201
202 // at returns the (possibly nil) type for type parameter x.
203 func (u *unifier) at(x *TypeParam) Type {
204         return *u.handles[x]
205 }
206
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) {
210         assert(t != nil)
211         if traceInference {
212                 u.tracef("%s ➞ %s", x, t)
213         }
214         *u.handles[x] = t
215 }
216
217 // unknowns returns the number of type parameters for which no type has been set yet.
218 func (u *unifier) unknowns() int {
219         n := 0
220         for _, h := range u.handles {
221                 if *h == nil {
222                         n++
223                 }
224         }
225         return n
226 }
227
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 {
235                 list[i] = u.at(x)
236         }
237         return list
238 }
239
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) {
245         u.depth++
246         if traceInference {
247                 u.tracef("%s ≡ %s", x, y)
248         }
249         defer func() {
250                 if traceInference && !result {
251                         u.tracef("%s ≢ %s", x, y)
252                 }
253                 u.depth--
254         }()
255
256         // nothing to do if x == y
257         if x == y {
258                 return true
259         }
260
261         // Stop gap for cases where unification fails.
262         if u.depth > unificationDepthLimit {
263                 if traceInference {
264                         u.tracef("depth %d >= %d", u.depth, unificationDepthLimit)
265                 }
266                 if panicAtUnificationDepthLimit {
267                         panic("unification reached recursion depth limit")
268                 }
269                 return false
270         }
271
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 {
277                 if traceInference {
278                         u.tracef("%s ≡ %s (swap)", y, x)
279                 }
280                 x, y = y, x
281         }
282
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.
298         //
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)) {
301                 if traceInference {
302                         u.tracef("%s ≡ under %s", x, ny)
303                 }
304                 y = ny.under()
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
309                 if x == y {
310                         return true
311                 }
312         }
313
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
323                 if u.join(px, py) {
324                         return true
325                 }
326                 // both x and y have an inferred type - they must match
327                 return u.nify(u.at(px), u.at(py), p)
328
329         case px != nil:
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
333                         if u.nify(x, y, p) {
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 {
339                                         u.set(px, y)
340                                 }
341                                 return true
342                         }
343                         return false
344                 }
345                 // otherwise, infer type from y
346                 u.set(px, y)
347                 return true
348         }
349
350         // x != y if we get here
351         assert(x != y)
352
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).
357         if isTypeParam(y) {
358                 if traceInference {
359                         u.tracef("%s ≡ %s (swap)", y, x)
360                 }
361                 x, y = y, x
362         }
363
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 {
375                         xset := xi.typeSet()
376                         yset := yi.typeSet()
377                         if xset.comparable != yset.comparable {
378                                 return false
379                         }
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) {
383                                 return false
384                         }
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:
390                         //
391                         //    type T interface {
392                         //        m() interface{T}
393                         //    }
394                         //
395                         // If two such (differently named) interfaces are compared,
396                         // endless recursion occurs if the cycle is not detected.
397                         //
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.
401                         //
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}
408                         for p != nil {
409                                 if p.identical(q) {
410                                         return true // same pair was compared before
411                                 }
412                                 p = p.prev
413                         }
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
421                         }
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 {
426                                 ymap[ym.Id()] = ym
427                         }
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) {
431                                         return false
432                                 }
433                         }
434                         return true
435                 }
436
437                 // We don't have two interfaces. If we have one, make sure it's in xi.
438                 if yi != nil {
439                         xi = yi
440                         y = x
441                 }
442
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.
446                 if xi != nil {
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) {
452                                         return false
453                                 }
454                         }
455                         return true
456                 }
457
458                 // Neither x nor y are interface types.
459                 // They must be structurally equivalent to unify.
460         }
461
462         switch x := x.(type) {
463         case *Basic:
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
469                 }
470
471         case *Array:
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)
478                 }
479
480         case *Slice:
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)
484                 }
485
486         case *Struct:
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 {
494                                         g := y.fields[i]
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) {
499                                                 return false
500                                         }
501                                 }
502                                 return true
503                         }
504                 }
505
506         case *Pointer:
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)
510                 }
511
512         case *Tuple:
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() {
517                                 if x != nil {
518                                         for i, v := range x.vars {
519                                                 w := y.vars[i]
520                                                 if !u.nify(v.typ, w.typ, p) {
521                                                         return false
522                                                 }
523                                         }
524                                 }
525                                 return true
526                         }
527                 }
528
529         case *Signature:
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)
539                 }
540
541         case *Interface:
542                 assert(!enableInterfaceInference) // handled before this switch
543
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 {
549                         xset := x.typeSet()
550                         yset := y.typeSet()
551                         if xset.comparable != yset.comparable {
552                                 return false
553                         }
554                         if !xset.terms.equal(yset.terms) {
555                                 return false
556                         }
557                         a := xset.methods
558                         b := yset.methods
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:
565                                 //
566                                 //    type T interface {
567                                 //        m() interface{T}
568                                 //    }
569                                 //
570                                 // If two such (differently named) interfaces are compared,
571                                 // endless recursion occurs if the cycle is not detected.
572                                 //
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.
576                                 //
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}
583                                 for p != nil {
584                                         if p.identical(q) {
585                                                 return true // same pair was compared before
586                                         }
587                                         p = p.prev
588                                 }
589                                 if debug {
590                                         assertSortedMethods(a)
591                                         assertSortedMethods(b)
592                                 }
593                                 for i, f := range a {
594                                         g := b[i]
595                                         if f.Id() != g.Id() || !u.nify(f.typ, g.typ, q) {
596                                                 return false
597                                         }
598                                 }
599                                 return true
600                         }
601                 }
602
603         case *Map:
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)
607                 }
608
609         case *Chan:
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)
613                 }
614
615         case *Named:
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 {
622                                 xu := x.under()
623                                 yu := y.under()
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).
634                                         if sameOrig {
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) {
640                                                                 return false
641                                                         }
642                                                 }
643                                                 return true
644                                         }
645                                         return u.nify(xu, yu, p)
646                                 }
647                                 // We don't have two interfaces. If we have one, make sure it's in xi.
648                                 if yi != nil {
649                                         xi = yi
650                                         y = x
651                                 }
652                                 // If xi is an interface, use interface unification.
653                                 if xi != nil {
654                                         return u.nify(xi, y, p)
655                                 }
656                                 // In all other cases, the type arguments and origins must match.
657                         }
658
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) {
665                                 return false
666                         }
667                         for i, xarg := range xargs {
668                                 if !u.nify(xarg, yargs[i], p) {
669                                         return false
670                                 }
671                         }
672                         return sameOrig
673                 }
674
675         case *TypeParam:
676                 // x must be an unbound type parameter (see comment above).
677                 if debug {
678                         assert(u.asTypeParam(x) == nil)
679                 }
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 {
700                                 if traceInference {
701                                         u.tracef("core %s ≡ %s", x, y)
702                                 }
703                                 return u.nify(cx, y, p)
704                         }
705                 }
706                 // x != y and there's nothing to do
707
708         case nil:
709                 // avoid a crash in case of nil type
710
711         default:
712                 panic(sprintf(nil, true, "u.nify(%s, %s)", x, y))
713         }
714
715         return false
716 }