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.
5 // This file implements type parameter inference.
10 "cmd/compile/internal/syntax"
12 . "internal/types/errors"
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
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 {
36 assert(inferred == nil || len(inferred) == len(tparams) && !containsNil(inferred))
41 check.dump("== infer : %s%s ➞ %s", tparams, params, targs) // aligned with rename print below
43 check.dump("=> %s ➞ %s\n", tparams, inferred)
47 // There must be at least one type parameter, and no more type arguments than type parameters.
49 assert(n > 0 && len(targs) <= n)
51 // Parameters and arguments must match in number.
52 assert(params.Len() == len(args))
54 // If we already have all type arguments, we're done.
55 if len(targs) == n && !containsNil(targs) {
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.
62 targs2 := make([]Type, n)
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.
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:
79 // func f[P, Q any](P, Q) {}
82 // f[int](s, s) // ERROR
85 // With substitution, we get the error:
86 // "cannot use s (variable of type string) as int value in argument to f[int]"
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.
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).
96 smap := makeSubstMap(tparams, targs)
97 params = check.subst(nopos, params, smap, nil, check.context()).(*Tuple)
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))
105 errorf := func(kind string, tpar, targ Type, arg *operand) {
106 // provide a better error message if we can
107 targs := u.inferred(tparams)
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.
113 for _, targ := range targs {
120 check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match %s (cannot infer %s)", kind, targ, arg.expr, tpar, typeParamsString(tparams))
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)
134 check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match %s", kind, targ, arg.expr, tpar)
138 // indices of generic parameters with untyped arguments, for later use
142 // use information from function arguments
145 u.tracef("== function parameters: %s", params)
146 u.tracef("-- function arguments : %s", args)
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
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)
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)
179 inferred := u.inferred(tparams)
180 u.tracef("=> %s ➞ %s\n", tparams, inferred)
184 // use information from type parameter constraints
187 u.tracef("== type parameters: %s", tparams)
190 // Unify type parameters with their constraints as long
191 // as progress is being made.
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.
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.)
211 u.tracef("-- iteration %d", i)
214 for _, tpar := range tparams {
216 core, single := coreTerm(tpar)
218 u.tracef("-- type parameter %s = %s: core(%s) = %s, single = %v", tpar, tx, tpar, core, single)
221 // If there is a core term (i.e., a core type with tilde information)
222 // unify the type parameter with the core type.
224 // A type parameter can be unified with its core type in two cases.
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
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())
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)
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
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.
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)
273 if u.unknowns() == nn {
279 inferred := u.inferred(tparams)
280 u.tracef("=> %s ➞ %s\n", tparams, inferred)
284 // use information from untyped constants
287 u.tracef("== untyped arguments: %v", untyped)
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)
301 max := maxUntyped[tpar]
305 m := maxType(max, arg.typ)
307 check.errorf(arg, CannotInferTypeArgs, "mismatched types %s and %s (cannot infer %s)", max, arg.typ, tpar)
312 maxUntyped[tpar] = max
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 {
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)
334 for i, targ := range targs {
335 assert(targ == nil || inferred[i] == targ)
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.
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
362 killCycles(tparams, inferred)
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.
368 for i, typ := range inferred {
369 if typ != nil && (i >= len(targs) || targs[i] == nil) {
370 dirty = append(dirty, i)
376 u.tracef("-- simplify %s ➞ %s", tparams, inferred)
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)
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) {
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)
424 // containsNil reports whether list contains a nil entry.
425 func containsNil(list []Type) bool {
426 for _, t := range list {
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:
447 // func f[P constraint](x P) {
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:
459 // func f[P constraint](x P) {
463 // func f2[P2 constraint](x P2) {
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
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
480 renameMap := makeRenameMap(tparams, tparams2)
481 for i, tparam := range tparams {
482 tparams2[i].bound = check.subst(pos, tparam.bound, renameMap, nil, check.context())
485 return tparams2, check.subst(pos, typ, renameMap, nil, check.context())
488 // typeParamsString produces a string containing all the type parameter names
489 // in list suitable for human consumption.
490 func typeParamsString(list []*TypeParam) string {
497 return list[0].obj.name
499 return list[0].obj.name + " and " + list[1].obj.name
502 // general case (n > 2)
503 var buf strings.Builder
504 for i, tname := range list[:n-1] {
506 buf.WriteString(", ")
508 buf.WriteString(tname.obj.name)
510 buf.WriteString(", and ")
511 buf.WriteString(list[n-1].obj.name)
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 {
521 seen: make(map[Type]bool),
523 return w.isParameterized(typ)
526 type tpWalker struct {
531 func (w *tpWalker) isParameterized(typ Type) (res bool) {
533 if x, ok := w.seen[typ]; ok {
541 switch t := typ.(type) {
546 return w.isParameterized(_Unalias(t))
549 return w.isParameterized(t.elem)
552 return w.isParameterized(t.elem)
555 return w.varList(t.fields)
558 return w.isParameterized(t.base)
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)
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
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)
580 for _, m := range tset.methods {
581 if w.isParameterized(m.typ) {
585 return tset.is(func(t *term) bool {
586 return t != nil && w.isParameterized(t.typ)
590 return w.isParameterized(t.key) || w.isParameterized(t.elem)
593 return w.isParameterized(t.elem)
596 for _, t := range t.TypeArgs().list() {
597 if w.isParameterized(t) {
603 return tparamIndex(w.tparams, t) >= 0
606 panic(fmt.Sprintf("unexpected %T", typ))
612 func (w *tpWalker) varList(list []*Var) bool {
613 for _, v := range list {
614 if w.isParameterized(v.typ) {
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) {
627 var single *term // valid if n == 1
629 tpar.is(func(t *term) bool {
632 return false // no terms
643 assert(debug && under(single.typ) == coreType(tpar))
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
652 return &term{tilde, typ}, false
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.
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 {
671 type cycleFinder struct {
677 func (w *cycleFinder) typ(typ Type) {
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
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.
693 defer delete(w.seen, typ)
695 switch t := typ.(type) {
715 // This case should not occur because tuples only appear
716 // in signatures where they are handled explicitly.
720 w.varList(t.params.vars)
722 if t.results != nil {
723 w.varList(t.results.vars)
727 for _, t := range t.terms {
732 for _, m := range t.methods {
735 for _, t := range t.embeddeds {
747 for _, tpar := range t.TypeArgs().list() {
752 if i := tparamIndex(w.tparams, t); i >= 0 && w.inferred[i] != nil {
757 panic(fmt.Sprintf("unexpected %T", typ))
761 func (w *cycleFinder) varList(list []*Var) {
762 for _, v := range list {
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 {