go/types, types2: reverse inference of function type arguments
This CL implements type inference for generic functions used in
assignments: variable init expressions, regular assignments, and
return statements, but (not yet) function arguments passed to
functions. For instance, given a generic function
func f[P any](x P)
and a variable of function type
var v func(x int)
the assignment
v = f
is valid w/o explicit instantiation of f, and the missing type
argument for f is inferred from the type of v. More generally,
the function f may have multiple type arguments, and it may be
partially instantiated.
This new form of inference is not enabled by default (it needs
to go through the proposal process first). It can be enabled
by setting Config.EnableReverseTypeInference.
The mechanism is implemented as follows:
- The various expression evaluation functions take an additional
(first) argument T, which is the target type for the expression.
If not nil, it is the type of the LHS in an assignment.
- The method Checker.funcInst is changed such that it uses both,
provided type arguments (if any), and a target type (if any)
to augment type inference.
Change-Id: Idfde61078e1ee4f22abcca894a4c84d681734ff6
Reviewed-on: https://go-review.googlesource.com/c/go/+/476075
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>