]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types2/infer.go
49d4ed7fe81c47a859d8ed565656274e7fb0b74e
[gostls13.git] / src / cmd / compile / internal / types2 / infer.go
1 // Copyright 2018 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 parameter inference.
6
7 package types2
8
9 import (
10         "cmd/compile/internal/syntax"
11         "fmt"
12         . "internal/types/errors"
13         "strings"
14 )
15
16 // If enableReverseTypeInference is set, uninstantiated and
17 // partially instantiated generic functions may be assigned
18 // (incl. returned) to variables of function type and type
19 // inference will attempt to infer the missing type arguments.
20 // Available with go1.21.
21 const enableReverseTypeInference = true // disable for debugging
22
23 // infer attempts to infer the complete set of type arguments for generic function instantiation/call
24 // based on the given type parameters tparams, type arguments targs, function parameters params, and
25 // function arguments args, if any. There must be at least one type parameter, no more type arguments
26 // than type parameters, and params and args must match in number (incl. zero).
27 // If successful, infer returns the complete list of given and inferred type arguments, one for each
28 // type parameter. Otherwise the result is nil and appropriate errors will be reported.
29 func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, params *Tuple, args []*operand) (inferred []Type) {
30         // Don't verify result conditions if there's no error handler installed:
31         // in that case, an error leads to an exit panic and the result value may
32         // be incorrect. But in that case it doesn't matter because callers won't
33         // be able to use it either.
34         if check.conf.Error != nil {
35                 defer func() {
36                         assert(inferred == nil || len(inferred) == len(tparams) && !containsNil(inferred))
37                 }()
38         }
39
40         if traceInference {
41                 check.dump("== infer : %s%s ➞ %s", tparams, params, targs) // aligned with rename print below
42                 defer func() {
43                         check.dump("=> %s ➞ %s\n", tparams, inferred)
44                 }()
45         }
46
47         // There must be at least one type parameter, and no more type arguments than type parameters.
48         n := len(tparams)
49         assert(n > 0 && len(targs) <= n)
50
51         // Parameters and arguments must match in number.
52         assert(params.Len() == len(args))
53
54         // If we already have all type arguments, we're done.
55         if len(targs) == n && !containsNil(targs) {
56                 return targs
57         }
58
59         // Make sure we have a "full" list of type arguments, some of which may
60         // be nil (unknown). Make a copy so as to not clobber the incoming slice.
61         if len(targs) < n {
62                 targs2 := make([]Type, n)
63                 copy(targs2, targs)
64                 targs = targs2
65         }
66         // len(targs) == n
67
68         // Continue with the type arguments we have. Avoid matching generic
69         // parameters that already have type arguments against function arguments:
70         // It may fail because matching uses type identity while parameter passing
71         // uses assignment rules. Instantiate the parameter list with the type
72         // arguments we have, and continue with that parameter list.
73
74         // Substitute type arguments for their respective type parameters in params,
75         // if any. Note that nil targs entries are ignored by check.subst.
76         // We do this for better error messages; it's not needed for correctness.
77         // For instance, given:
78         //
79         //   func f[P, Q any](P, Q) {}
80         //
81         //   func _(s string) {
82         //           f[int](s, s) // ERROR
83         //   }
84         //
85         // With substitution, we get the error:
86         //   "cannot use s (variable of type string) as int value in argument to f[int]"
87         //
88         // Without substitution we get the (worse) error:
89         //   "type string of s does not match inferred type int for P"
90         // even though the type int was provided (not inferred) for P.
91         //
92         // TODO(gri) We might be able to finesse this in the error message reporting
93         //           (which only happens in case of an error) and then avoid doing
94         //           the substitution (which always happens).
95         if params.Len() > 0 {
96                 smap := makeSubstMap(tparams, targs)
97                 params = check.subst(nopos, params, smap, nil, check.context()).(*Tuple)
98         }
99
100         // Unify parameter and argument types for generic parameters with typed arguments
101         // and collect the indices of generic parameters with untyped arguments.
102         // Terminology: generic parameter = function parameter with a type-parameterized type
103         u := newUnifier(tparams, targs, check.allowVersion(check.pkg, pos, go1_21))
104
105         errorf := func(kind string, tpar, targ Type, arg *operand) {
106                 // provide a better error message if we can
107                 targs := u.inferred(tparams)
108                 if targs[0] == nil {
109                         // The first type parameter couldn't be inferred.
110                         // If none of them could be inferred, don't try
111                         // to provide the inferred type in the error msg.
112                         allFailed := true
113                         for _, targ := range targs {
114                                 if targ != nil {
115                                         allFailed = false
116                                         break
117                                 }
118                         }
119                         if allFailed {
120                                 check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match %s (cannot infer %s)", kind, targ, arg.expr, tpar, typeParamsString(tparams))
121                                 return
122                         }
123                 }
124                 smap := makeSubstMap(tparams, targs)
125                 // TODO(gri): pass a poser here, rather than arg.Pos().
126                 inferred := check.subst(arg.Pos(), tpar, smap, nil, check.context())
127                 // CannotInferTypeArgs indicates a failure of inference, though the actual
128                 // error may be better attributed to a user-provided type argument (hence
129                 // InvalidTypeArg). We can't differentiate these cases, so fall back on
130                 // the more general CannotInferTypeArgs.
131                 if inferred != tpar {
132                         check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match inferred type %s for %s", kind, targ, arg.expr, inferred, tpar)
133                 } else {
134                         check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match %s", kind, targ, arg.expr, tpar)
135                 }
136         }
137
138         // indices of generic parameters with untyped arguments, for later use
139         var untyped []int
140
141         // --- 1 ---
142         // use information from function arguments
143
144         if traceInference {
145                 u.tracef("== function parameters: %s", params)
146                 u.tracef("-- function arguments : %s", args)
147         }
148
149         for i, arg := range args {
150                 if arg.mode == invalid {
151                         // An error was reported earlier. Ignore this arg
152                         // and continue, we may still be able to infer all
153                         // targs resulting in fewer follow-on errors.
154                         // TODO(gri) determine if we still need this check
155                         continue
156                 }
157                 par := params.At(i)
158                 if isParameterized(tparams, par.typ) || isParameterized(tparams, arg.typ) {
159                         // Function parameters are always typed. Arguments may be untyped.
160                         // Collect the indices of untyped arguments and handle them later.
161                         if isTyped(arg.typ) {
162                                 if !u.unify(par.typ, arg.typ, assign) {
163                                         errorf("type", par.typ, arg.typ, arg)
164                                         return nil
165                                 }
166                         } else if _, ok := par.typ.(*TypeParam); ok && !arg.isNil() {
167                                 // Since default types are all basic (i.e., non-composite) types, an
168                                 // untyped argument will never match a composite parameter type; the
169                                 // only parameter type it can possibly match against is a *TypeParam.
170                                 // Thus, for untyped arguments we only need to look at parameter types
171                                 // that are single type parameters.
172                                 // Also, untyped nils don't have a default type and can be ignored.
173                                 untyped = append(untyped, i)
174                         }
175                 }
176         }
177
178         if traceInference {
179                 inferred := u.inferred(tparams)
180                 u.tracef("=> %s ➞ %s\n", tparams, inferred)
181         }
182
183         // --- 2 ---
184         // use information from type parameter constraints
185
186         if traceInference {
187                 u.tracef("== type parameters: %s", tparams)
188         }
189
190         // Unify type parameters with their constraints as long
191         // as progress is being made.
192         //
193         // This is an O(n^2) algorithm where n is the number of
194         // type parameters: if there is progress, at least one
195         // type argument is inferred per iteration, and we have
196         // a doubly nested loop.
197         //
198         // In practice this is not a problem because the number
199         // of type parameters tends to be very small (< 5 or so).
200         // (It should be possible for unification to efficiently
201         // signal newly inferred type arguments; then the loops
202         // here could handle the respective type parameters only,
203         // but that will come at a cost of extra complexity which
204         // may not be worth it.)
205         for i := 0; ; i++ {
206                 nn := u.unknowns()
207                 if traceInference {
208                         if i > 0 {
209                                 fmt.Println()
210                         }
211                         u.tracef("-- iteration %d", i)
212                 }
213
214                 for _, tpar := range tparams {
215                         tx := u.at(tpar)
216                         core, single := coreTerm(tpar)
217                         if traceInference {
218                                 u.tracef("-- type parameter %s = %s: core(%s) = %s, single = %v", tpar, tx, tpar, core, single)
219                         }
220
221                         // If there is a core term (i.e., a core type with tilde information)
222                         // unify the type parameter with the core type.
223                         if core != nil {
224                                 // A type parameter can be unified with its core type in two cases.
225                                 switch {
226                                 case tx != nil:
227                                         // The corresponding type argument tx is known. There are 2 cases:
228                                         // 1) If the core type has a tilde, per spec requirement for tilde
229                                         //    elements, the core type is an underlying (literal) type.
230                                         //    And because of the tilde, the underlying type of tx must match
231                                         //    against the core type.
232                                         //    But because unify automatically matches a defined type against
233                                         //    an underlying literal type, we can simply unify tx with the
234                                         //    core type.
235                                         // 2) If the core type doesn't have a tilde, we also must unify tx
236                                         //    with the core type.
237                                         if !u.unify(tx, core.typ, 0) {
238                                                 // TODO(gri) Type parameters that appear in the constraint and
239                                                 //           for which we have type arguments inferred should
240                                                 //           use those type arguments for a better error message.
241                                                 check.errorf(pos, CannotInferTypeArgs, "%s (type %s) does not satisfy %s", tpar, tx, tpar.Constraint())
242                                                 return nil
243                                         }
244                                 case single && !core.tilde:
245                                         // The corresponding type argument tx is unknown and there's a single
246                                         // specific type and no tilde.
247                                         // In this case the type argument must be that single type; set it.
248                                         u.set(tpar, core.typ)
249                                 }
250                         } else {
251                                 if tx != nil {
252                                         // We don't have a core type, but the type argument tx is known.
253                                         // It must have (at least) all the methods of the type constraint,
254                                         // and the method signatures must unify; otherwise tx cannot satisfy
255                                         // the constraint.
256                                         // TODO(gri) Now that unification handles interfaces, this code can
257                                         //           be reduced to calling u.unify(tx, tpar.iface(), assign)
258                                         //           (which will compare signatures exactly as we do below).
259                                         //           We leave it as is for now because missingMethod provides
260                                         //           a failure cause which allows for a better error message.
261                                         //           Eventually, unify should return an error with cause.
262                                         var cause string
263                                         constraint := tpar.iface()
264                                         if m, _ := check.missingMethod(tx, constraint, true, func(x, y Type) bool { return u.unify(x, y, exact) }, &cause); m != nil {
265                                                 // TODO(gri) better error message (see TODO above)
266                                                 check.errorf(pos, CannotInferTypeArgs, "%s (type %s) does not satisfy %s %s", tpar, tx, tpar.Constraint(), cause)
267                                                 return nil
268                                         }
269                                 }
270                         }
271                 }
272
273                 if u.unknowns() == nn {
274                         break // no progress
275                 }
276         }
277
278         if traceInference {
279                 inferred := u.inferred(tparams)
280                 u.tracef("=> %s ➞ %s\n", tparams, inferred)
281         }
282
283         // --- 3 ---
284         // use information from untyped constants
285
286         if traceInference {
287                 u.tracef("== untyped arguments: %v", untyped)
288         }
289
290         // Some generic parameters with untyped arguments may have been given a type by now.
291         // Collect all remaining parameters that don't have a type yet and determine the
292         // maximum untyped type for each of those parameters, if possible.
293         var maxUntyped map[*TypeParam]Type // lazily allocated (we may not need it)
294         for _, index := range untyped {
295                 tpar := params.At(index).typ.(*TypeParam) // is type parameter by construction of untyped
296                 if u.at(tpar) == nil {
297                         arg := args[index] // arg corresponding to tpar
298                         if maxUntyped == nil {
299                                 maxUntyped = make(map[*TypeParam]Type)
300                         }
301                         max := maxUntyped[tpar]
302                         if max == nil {
303                                 max = arg.typ
304                         } else {
305                                 m := maxType(max, arg.typ)
306                                 if m == nil {
307                                         check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
308                                         return nil
309                                 }
310                                 max = m
311                         }
312                         maxUntyped[tpar] = max
313                 }
314         }
315         // maxUntyped contains the maximum untyped type for each type parameter
316         // which doesn't have a type yet. Set the respective default types.
317         for tpar, typ := range maxUntyped {
318                 d := Default(typ)
319                 assert(isTyped(d))
320                 u.set(tpar, d)
321         }
322
323         // --- simplify ---
324
325         // u.inferred(tparams) now contains the incoming type arguments plus any additional type
326         // arguments which were inferred. The inferred non-nil entries may still contain
327         // references to other type parameters found in constraints.
328         // For instance, for [A any, B interface{ []C }, C interface{ *A }], if A == int
329         // was given, unification produced the type list [int, []C, *A]. We eliminate the
330         // remaining type parameters by substituting the type parameters in this type list
331         // until nothing changes anymore.
332         inferred = u.inferred(tparams)
333         if debug {
334                 for i, targ := range targs {
335                         assert(targ == nil || inferred[i] == targ)
336                 }
337         }
338
339         // The data structure of each (provided or inferred) type represents a graph, where
340         // each node corresponds to a type and each (directed) vertex points to a component
341         // type. The substitution process described above repeatedly replaces type parameter
342         // nodes in these graphs with the graphs of the types the type parameters stand for,
343         // which creates a new (possibly bigger) graph for each type.
344         // The substitution process will not stop if the replacement graph for a type parameter
345         // also contains that type parameter.
346         // For instance, for [A interface{ *A }], without any type argument provided for A,
347         // unification produces the type list [*A]. Substituting A in *A with the value for
348         // A will lead to infinite expansion by producing [**A], [****A], [********A], etc.,
349         // because the graph A -> *A has a cycle through A.
350         // Generally, cycles may occur across multiple type parameters and inferred types
351         // (for instance, consider [P interface{ *Q }, Q interface{ func(P) }]).
352         // We eliminate cycles by walking the graphs for all type parameters. If a cycle
353         // through a type parameter is detected, killCycles nils out the respective type
354         // (in the inferred list) which kills the cycle, and marks the corresponding type
355         // parameter as not inferred.
356         //
357         // TODO(gri) If useful, we could report the respective cycle as an error. We don't
358         //           do this now because type inference will fail anyway, and furthermore,
359         //           constraints with cycles of this kind cannot currently be satisfied by
360         //           any user-supplied type. But should that change, reporting an error
361         //           would be wrong.
362         killCycles(tparams, inferred)
363
364         // dirty tracks the indices of all types that may still contain type parameters.
365         // We know that nil type entries and entries corresponding to provided (non-nil)
366         // type arguments are clean, so exclude them from the start.
367         var dirty []int
368         for i, typ := range inferred {
369                 if typ != nil && (i >= len(targs) || targs[i] == nil) {
370                         dirty = append(dirty, i)
371                 }
372         }
373
374         for len(dirty) > 0 {
375                 if traceInference {
376                         u.tracef("-- simplify %s ➞ %s", tparams, inferred)
377                 }
378                 // TODO(gri) Instead of creating a new substMap for each iteration,
379                 // provide an update operation for substMaps and only change when
380                 // needed. Optimization.
381                 smap := makeSubstMap(tparams, inferred)
382                 n := 0
383                 for _, index := range dirty {
384                         t0 := inferred[index]
385                         if t1 := check.subst(nopos, t0, smap, nil, check.context()); t1 != t0 {
386                                 // t0 was simplified to t1.
387                                 // If t0 was a generic function, but the simplified signature t1 does
388                                 // not contain any type parameters anymore, the function is not generic
389                                 // anymore. Remove it's type parameters. (go.dev/issue/59953)
390                                 // Note that if t0 was a signature, t1 must be a signature, and t1
391                                 // can only be a generic signature if it originated from a generic
392                                 // function argument. Those signatures are never defined types and
393                                 // thus there is no need to call under below.
394                                 // TODO(gri) Consider doing this in Checker.subst.
395                                 //           Then this would fall out automatically here and also
396                                 //           in instantiation (where we also explicitly nil out
397                                 //           type parameters). See the *Signature TODO in subst.
398                                 if sig, _ := t1.(*Signature); sig != nil && sig.TypeParams().Len() > 0 && !isParameterized(tparams, sig) {
399                                         sig.tparams = nil
400                                 }
401                                 inferred[index] = t1
402                                 dirty[n] = index
403                                 n++
404                         }
405                 }
406                 dirty = dirty[:n]
407         }
408
409         // Once nothing changes anymore, we may still have type parameters left;
410         // e.g., a constraint with core type *P may match a type parameter Q but
411         // we don't have any type arguments to fill in for *P or Q (go.dev/issue/45548).
412         // Don't let such inferences escape; instead treat them as unresolved.
413         for i, typ := range inferred {
414                 if typ == nil || isParameterized(tparams, typ) {
415                         obj := tparams[i].obj
416                         check.errorf(pos, CannotInferTypeArgs, "cannot infer %s (%s)", obj.name, obj.pos)
417                         return nil
418                 }
419         }
420
421         return
422 }
423
424 // containsNil reports whether list contains a nil entry.
425 func containsNil(list []Type) bool {
426         for _, t := range list {
427                 if t == nil {
428                         return true
429                 }
430         }
431         return false
432 }
433
434 // renameTParams renames the type parameters in the given type such that each type
435 // parameter is given a new identity. renameTParams returns the new type parameters
436 // and updated type. If the result type is unchanged from the argument type, none
437 // of the type parameters in tparams occurred in the type.
438 // If typ is a generic function, type parameters held with typ are not changed and
439 // must be updated separately if desired.
440 // The positions is only used for debug traces.
441 func (check *Checker) renameTParams(pos syntax.Pos, tparams []*TypeParam, typ Type) ([]*TypeParam, Type) {
442         // For the purpose of type inference we must differentiate type parameters
443         // occurring in explicit type or value function arguments from the type
444         // parameters we are solving for via unification because they may be the
445         // same in self-recursive calls:
446         //
447         //   func f[P constraint](x P) {
448         //           f(x)
449         //   }
450         //
451         // In this example, without type parameter renaming, the P used in the
452         // instantiation f[P] has the same pointer identity as the P we are trying
453         // to solve for through type inference. This causes problems for type
454         // unification. Because any such self-recursive call is equivalent to
455         // a mutually recursive call, type parameter renaming can be used to
456         // create separate, disentangled type parameters. The above example
457         // can be rewritten into the following equivalent code:
458         //
459         //   func f[P constraint](x P) {
460         //           f2(x)
461         //   }
462         //
463         //   func f2[P2 constraint](x P2) {
464         //           f(x)
465         //   }
466         //
467         // Type parameter renaming turns the first example into the second
468         // example by renaming the type parameter P into P2.
469         if len(tparams) == 0 {
470                 return nil, typ // nothing to do
471         }
472
473         tparams2 := make([]*TypeParam, len(tparams))
474         for i, tparam := range tparams {
475                 tname := NewTypeName(tparam.Obj().Pos(), tparam.Obj().Pkg(), tparam.Obj().Name(), nil)
476                 tparams2[i] = NewTypeParam(tname, nil)
477                 tparams2[i].index = tparam.index // == i
478         }
479
480         renameMap := makeRenameMap(tparams, tparams2)
481         for i, tparam := range tparams {
482                 tparams2[i].bound = check.subst(pos, tparam.bound, renameMap, nil, check.context())
483         }
484
485         return tparams2, check.subst(pos, typ, renameMap, nil, check.context())
486 }
487
488 // typeParamsString produces a string containing all the type parameter names
489 // in list suitable for human consumption.
490 func typeParamsString(list []*TypeParam) string {
491         // common cases
492         n := len(list)
493         switch n {
494         case 0:
495                 return ""
496         case 1:
497                 return list[0].obj.name
498         case 2:
499                 return list[0].obj.name + " and " + list[1].obj.name
500         }
501
502         // general case (n > 2)
503         var buf strings.Builder
504         for i, tname := range list[:n-1] {
505                 if i > 0 {
506                         buf.WriteString(", ")
507                 }
508                 buf.WriteString(tname.obj.name)
509         }
510         buf.WriteString(", and ")
511         buf.WriteString(list[n-1].obj.name)
512         return buf.String()
513 }
514
515 // isParameterized reports whether typ contains any of the type parameters of tparams.
516 // If typ is a generic function, isParameterized ignores the type parameter declarations;
517 // it only considers the signature proper (incoming and result parameters).
518 func isParameterized(tparams []*TypeParam, typ Type) bool {
519         w := tpWalker{
520                 tparams: tparams,
521                 seen:    make(map[Type]bool),
522         }
523         return w.isParameterized(typ)
524 }
525
526 type tpWalker struct {
527         tparams []*TypeParam
528         seen    map[Type]bool
529 }
530
531 func (w *tpWalker) isParameterized(typ Type) (res bool) {
532         // detect cycles
533         if x, ok := w.seen[typ]; ok {
534                 return x
535         }
536         w.seen[typ] = false
537         defer func() {
538                 w.seen[typ] = res
539         }()
540
541         switch t := typ.(type) {
542         case *Basic:
543                 // nothing to do
544
545         case *_Alias:
546                 return w.isParameterized(_Unalias(t))
547
548         case *Array:
549                 return w.isParameterized(t.elem)
550
551         case *Slice:
552                 return w.isParameterized(t.elem)
553
554         case *Struct:
555                 return w.varList(t.fields)
556
557         case *Pointer:
558                 return w.isParameterized(t.base)
559
560         case *Tuple:
561                 // This case does not occur from within isParameterized
562                 // because tuples only appear in signatures where they
563                 // are handled explicitly. But isParameterized is also
564                 // called by Checker.callExpr with a function result tuple
565                 // if instantiation failed (go.dev/issue/59890).
566                 return t != nil && w.varList(t.vars)
567
568         case *Signature:
569                 // t.tparams may not be nil if we are looking at a signature
570                 // of a generic function type (or an interface method) that is
571                 // part of the type we're testing. We don't care about these type
572                 // parameters.
573                 // Similarly, the receiver of a method may declare (rather than
574                 // use) type parameters, we don't care about those either.
575                 // Thus, we only need to look at the input and result parameters.
576                 return t.params != nil && w.varList(t.params.vars) || t.results != nil && w.varList(t.results.vars)
577
578         case *Interface:
579                 tset := t.typeSet()
580                 for _, m := range tset.methods {
581                         if w.isParameterized(m.typ) {
582                                 return true
583                         }
584                 }
585                 return tset.is(func(t *term) bool {
586                         return t != nil && w.isParameterized(t.typ)
587                 })
588
589         case *Map:
590                 return w.isParameterized(t.key) || w.isParameterized(t.elem)
591
592         case *Chan:
593                 return w.isParameterized(t.elem)
594
595         case *Named:
596                 for _, t := range t.TypeArgs().list() {
597                         if w.isParameterized(t) {
598                                 return true
599                         }
600                 }
601
602         case *TypeParam:
603                 return tparamIndex(w.tparams, t) >= 0
604
605         default:
606                 panic(fmt.Sprintf("unexpected %T", typ))
607         }
608
609         return false
610 }
611
612 func (w *tpWalker) varList(list []*Var) bool {
613         for _, v := range list {
614                 if w.isParameterized(v.typ) {
615                         return true
616                 }
617         }
618         return false
619 }
620
621 // If the type parameter has a single specific type S, coreTerm returns (S, true).
622 // Otherwise, if tpar has a core type T, it returns a term corresponding to that
623 // core type and false. In that case, if any term of tpar has a tilde, the core
624 // term has a tilde. In all other cases coreTerm returns (nil, false).
625 func coreTerm(tpar *TypeParam) (*term, bool) {
626         n := 0
627         var single *term // valid if n == 1
628         var tilde bool
629         tpar.is(func(t *term) bool {
630                 if t == nil {
631                         assert(n == 0)
632                         return false // no terms
633                 }
634                 n++
635                 single = t
636                 if t.tilde {
637                         tilde = true
638                 }
639                 return true
640         })
641         if n == 1 {
642                 if debug {
643                         assert(debug && under(single.typ) == coreType(tpar))
644                 }
645                 return single, true
646         }
647         if typ := coreType(tpar); typ != nil {
648                 // A core type is always an underlying type.
649                 // If any term of tpar has a tilde, we don't
650                 // have a precise core type and we must return
651                 // a tilde as well.
652                 return &term{tilde, typ}, false
653         }
654         return nil, false
655 }
656
657 // killCycles walks through the given type parameters and looks for cycles
658 // created by type parameters whose inferred types refer back to that type
659 // parameter, either directly or indirectly. If such a cycle is detected,
660 // it is killed by setting the corresponding inferred type to nil.
661 //
662 // TODO(gri) Determine if we can simply abort inference as soon as we have
663 // found a single cycle.
664 func killCycles(tparams []*TypeParam, inferred []Type) {
665         w := cycleFinder{tparams, inferred, make(map[Type]bool)}
666         for _, t := range tparams {
667                 w.typ(t) // t != nil
668         }
669 }
670
671 type cycleFinder struct {
672         tparams  []*TypeParam
673         inferred []Type
674         seen     map[Type]bool
675 }
676
677 func (w *cycleFinder) typ(typ Type) {
678         if w.seen[typ] {
679                 // We have seen typ before. If it is one of the type parameters
680                 // in w.tparams, iterative substitution will lead to infinite expansion.
681                 // Nil out the corresponding type which effectively kills the cycle.
682                 if tpar, _ := typ.(*TypeParam); tpar != nil {
683                         if i := tparamIndex(w.tparams, tpar); i >= 0 {
684                                 // cycle through tpar
685                                 w.inferred[i] = nil
686                         }
687                 }
688                 // If we don't have one of our type parameters, the cycle is due
689                 // to an ordinary recursive type and we can just stop walking it.
690                 return
691         }
692         w.seen[typ] = true
693         defer delete(w.seen, typ)
694
695         switch t := typ.(type) {
696         case *Basic:
697                 // nothing to do
698
699         case *_Alias:
700                 w.typ(_Unalias(t))
701
702         case *Array:
703                 w.typ(t.elem)
704
705         case *Slice:
706                 w.typ(t.elem)
707
708         case *Struct:
709                 w.varList(t.fields)
710
711         case *Pointer:
712                 w.typ(t.base)
713
714         // case *Tuple:
715         //      This case should not occur because tuples only appear
716         //      in signatures where they are handled explicitly.
717
718         case *Signature:
719                 if t.params != nil {
720                         w.varList(t.params.vars)
721                 }
722                 if t.results != nil {
723                         w.varList(t.results.vars)
724                 }
725
726         case *Union:
727                 for _, t := range t.terms {
728                         w.typ(t.typ)
729                 }
730
731         case *Interface:
732                 for _, m := range t.methods {
733                         w.typ(m.typ)
734                 }
735                 for _, t := range t.embeddeds {
736                         w.typ(t)
737                 }
738
739         case *Map:
740                 w.typ(t.key)
741                 w.typ(t.elem)
742
743         case *Chan:
744                 w.typ(t.elem)
745
746         case *Named:
747                 for _, tpar := range t.TypeArgs().list() {
748                         w.typ(tpar)
749                 }
750
751         case *TypeParam:
752                 if i := tparamIndex(w.tparams, t); i >= 0 && w.inferred[i] != nil {
753                         w.typ(w.inferred[i])
754                 }
755
756         default:
757                 panic(fmt.Sprintf("unexpected %T", typ))
758         }
759 }
760
761 func (w *cycleFinder) varList(list []*Var) {
762         for _, v := range list {
763                 w.typ(v.typ)
764         }
765 }
766
767 // If tpar is a type parameter in list, tparamIndex returns the index
768 // of the type parameter in list. Otherwise the result is < 0.
769 func tparamIndex(list []*TypeParam, tpar *TypeParam) int {
770         for i, p := range list {
771                 if p == tpar {
772                         return i
773                 }
774         }
775         return -1
776 }