]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: adjust Check.funcInst signature
authorRobert Griesemer <gri@golang.org>
Wed, 29 Mar 2023 20:56:31 +0000 (13:56 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 29 Mar 2023 21:10:30 +0000 (21:10 +0000)
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>
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/expr.go
src/go/types/call.go
src/go/types/expr.go

index ec814995a94621c9a6c5e33761c96e7cca9c63c9..a47deb9a225d38f7d98f1bea8779c336ecb37026 100644 (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.
index bab52b253b69181adf5b294f802ca48c36d1d4fa..1424e43876f34956d6eddc73066374bb105ff588 100644 (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
index bdcfd9d56b3e568219dba77eb27a4f4583b8bdd0..fb0a6cea3cf90912e65617d2532b08914e71504b 100644 (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.
index 219a392b88af40b2fdf197fe41ccf7cf83cd9db5..04e6f6d9f7a565643da44961b2d44c123d338f35 100644 (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