go/types, types2: adjust Check.funcInst signature

Per feedback from prior CL.

Change-Id: Icbf6149c3b61e26085caf6f368d22ad4f02c75fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/480316
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2023-03-29 13:56:31 -07:00 committed by Gopher Robot
parent f46ea60f2b
commit 9c75c4b6d7
4 changed files with 28 additions and 36 deletions

View file

@ -15,24 +15,18 @@ import (
)
// funcInst type-checks a function instantiation and returns the result in x.
// The incoming x must be an uninstantiated generic function. If inst != 0,
// The incoming x must be an uninstantiated generic function. If inst != nil,
// it provides (some or all of) the type arguments (inst.Index) for the
// instantiation. If the target type T != nil and is a (non-generic) function
// signature, the signature's parameter types are used to infer additional
// missing type arguments of x, if any.
// At least one of inst or T must be provided.
func (check *Checker) funcInst(T Type, pos syntax.Pos, x *operand, inst *syntax.IndexExpr) {
// instantiation. If the target type tsig != nil, the signature's parameter
// types are used to infer additional missing type arguments of x, if any.
// At least one of tsig or inst must be provided.
func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst *syntax.IndexExpr) {
assert(tsig != nil || inst != nil)
if !check.allowVersion(check.pkg, 1, 18) {
check.versionErrorf(inst.Pos(), "go1.18", "function instantiation")
}
// tsig is the (assignment) target function signature, or nil.
// TODO(gri) refactor and pass in tsig to funcInst instead
var tsig *Signature
if check.conf.EnableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
// targs and xlist are the type arguments and corresponding type expressions, or nil.
var targs []Type
var xlist []syntax.Expr
@ -47,8 +41,6 @@ func (check *Checker) funcInst(T Type, pos syntax.Pos, x *operand, inst *syntax.
assert(len(targs) == len(xlist))
}
assert(tsig != nil || targs != nil)
// Check the number of type arguments (got) vs number of type parameters (want).
// Note that x is a function value, not a type expression, so we don't need to
// call under below.

View file

@ -1293,8 +1293,8 @@ func (check *Checker) nonGeneric(T Type, x *operand) {
case *Signature:
if t.tparams != nil {
if check.conf.EnableReverseTypeInference && T != nil {
if _, ok := under(T).(*Signature); ok {
check.funcInst(T, x.Pos(), x, nil)
if tsig, _ := under(T).(*Signature); tsig != nil {
check.funcInst(tsig, x.Pos(), x, nil)
return
}
}
@ -1617,7 +1617,11 @@ func (check *Checker) exprInternal(T Type, x *operand, e syntax.Expr, hint Type)
case *syntax.IndexExpr:
if check.indexExpr(x, e) {
check.funcInst(T, e.Pos(), x, e)
var tsig *Signature
if check.conf.EnableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
check.funcInst(tsig, e.Pos(), x, e)
}
if x.mode == invalid {
goto Error

View file

@ -17,24 +17,18 @@ import (
)
// funcInst type-checks a function instantiation and returns the result in x.
// The incoming x must be an uninstantiated generic function. If ix != 0,
// The incoming x must be an uninstantiated generic function. If ix != nil,
// it provides (some or all of) the type arguments (ix.Indices) for the
// instantiation. If the target type T != nil and is a (non-generic) function
// signature, the signature's parameter types are used to infer additional
// missing type arguments of x, if any.
// At least one of inst or T must be provided.
func (check *Checker) funcInst(T Type, pos token.Pos, x *operand, ix *typeparams.IndexExpr) {
// instantiation. If the target type tsig != nil, the signature's parameter
// types are used to infer additional missing type arguments of x, if any.
// At least one of tsig or ix must be provided.
func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *typeparams.IndexExpr) {
assert(tsig != nil || ix != nil)
if !check.allowVersion(check.pkg, 1, 18) {
check.softErrorf(inNode(ix.Orig, ix.Lbrack), UnsupportedFeature, "function instantiation requires go1.18 or later")
}
// tsig is the (assignment) target function signature, or nil.
// TODO(gri) refactor and pass in tsig to funcInst instead
var tsig *Signature
if check.conf._EnableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
// targs and xlist are the type arguments and corresponding type expressions, or nil.
var targs []Type
var xlist []ast.Expr
@ -49,8 +43,6 @@ func (check *Checker) funcInst(T Type, pos token.Pos, x *operand, ix *typeparams
assert(len(targs) == len(xlist))
}
assert(tsig != nil || targs != nil)
// Check the number of type arguments (got) vs number of type parameters (want).
// Note that x is a function value, not a type expression, so we don't need to
// call under below.

View file

@ -1278,8 +1278,8 @@ func (check *Checker) nonGeneric(T Type, x *operand) {
case *Signature:
if t.tparams != nil {
if check.conf._EnableReverseTypeInference && T != nil {
if _, ok := under(T).(*Signature); ok {
check.funcInst(T, x.Pos(), x, nil)
if tsig, _ := under(T).(*Signature); tsig != nil {
check.funcInst(tsig, x.Pos(), x, nil)
return
}
}
@ -1600,7 +1600,11 @@ func (check *Checker) exprInternal(T Type, x *operand, e ast.Expr, hint Type) ex
case *ast.IndexExpr, *ast.IndexListExpr:
ix := typeparams.UnpackIndexExpr(e)
if check.indexExpr(x, ix) {
check.funcInst(T, e.Pos(), x, ix)
var tsig *Signature
if check.conf._EnableReverseTypeInference && T != nil {
tsig, _ = under(T).(*Signature)
}
check.funcInst(tsig, e.Pos(), x, ix)
}
if x.mode == invalid {
goto Error