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