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

View as plain text