]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: call recordInstance in instantiateSignature
authorRobert Griesemer <gri@golang.org>
Thu, 11 May 2023 20:55:27 +0000 (13:55 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 12 May 2023 15:20:47 +0000 (15:20 +0000)
This matches the pattern we use for type instantiations and
factors out the recordInstance and associated assert calls.

Change-Id: Ib7731c0e619aca42f418cb2d9a153785aaf014cb
Reviewed-on: https://go-review.googlesource.com/c/go/+/494457
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/call.go
src/go/types/call.go

index 075dfd1efa419e785764db1dba2045bc59f0efc6..4b854df774ec00e1e90fbe9dd6ffacb97f251462 100644 (file)
@@ -128,16 +128,15 @@ func (check *Checker) funcInst(tsig *Signature, pos syntax.Pos, x *operand, inst
        assert(got == want)
 
        // instantiate function signature
-       sig = check.instantiateSignature(x.Pos(), sig, targs, xlist)
-       assert(sig.TypeParams().Len() == 0) // signature is not generic anymore
+       expr := x.expr // if we don't have an index expression, keep the existing expression of x
+       if inst != nil {
+               expr = inst
+       }
+       sig = check.instantiateSignature(x.Pos(), expr, sig, targs, xlist)
 
        x.typ = sig
        x.mode = value
-       // If we don't have an index expression, keep the existing expression of x.
-       if inst != nil {
-               x.expr = inst
-       }
-       check.recordInstance(x.expr, targs, sig)
+       x.expr = expr
        return nil, nil
 }
 
@@ -160,7 +159,7 @@ func nth(n int) string {
        return fmt.Sprintf("%dth", n)
 }
 
-func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs []Type, xlist []syntax.Expr) (res *Signature) {
+func (check *Checker) instantiateSignature(pos syntax.Pos, expr syntax.Expr, typ *Signature, targs []Type, xlist []syntax.Expr) (res *Signature) {
        assert(check != nil)
        assert(len(targs) == typ.TypeParams().Len())
 
@@ -174,6 +173,8 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs
        }
 
        inst := check.instance(pos, typ, targs, nil, check.context()).(*Signature)
+       assert(inst.TypeParams().Len() == 0) // signature is not generic anymore
+       check.recordInstance(expr, targs, inst)
        assert(len(xlist) <= len(targs))
 
        // verify instantiation lazily (was go.dev/issue/50450)
@@ -311,11 +312,7 @@ func (check *Checker) callExpr(x *operand, call *syntax.CallExpr) exprKind {
                // of arguments is supplied).
                if got == want && want > 0 {
                        check.verifyVersionf(check.pkg, inst, go1_18, "function instantiation")
-
-                       sig = check.instantiateSignature(inst.Pos(), sig, targs, xlist)
-                       assert(sig.TypeParams().Len() == 0) // signature is not generic anymore
-                       check.recordInstance(inst, targs, sig)
-
+                       sig = check.instantiateSignature(inst.Pos(), inst, sig, targs, xlist)
                        // targs have been consumed; proceed with checking arguments of the
                        // non-generic signature.
                        targs = nil
@@ -572,9 +569,7 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
                // compute result signature: instantiate if needed
                rsig = sig
                if n > 0 {
-                       rsig = check.instantiateSignature(call.Pos(), sig, targs[:n], xlist)
-                       assert(rsig.TypeParams().Len() == 0) // signature is not generic anymore
-                       check.recordInstance(call.Fun, targs[:n], rsig)
+                       rsig = check.instantiateSignature(call.Pos(), call.Fun, sig, targs[:n], xlist)
                }
 
                // Optimization: Only if the callee's parameter list was adjusted do we need to
@@ -593,10 +588,8 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
                        asig := args[i].typ.(*Signature)
                        k := j + asig.TypeParams().Len()
                        // targs[j:k] are the inferred type arguments for asig
-                       asig = check.instantiateSignature(call.Pos(), asig, targs[j:k], nil) // TODO(gri) provide xlist if possible (partial instantiations)
-                       assert(asig.TypeParams().Len() == 0)                                 // signature is not generic anymore
+                       asig = check.instantiateSignature(call.Pos(), args[i].expr, asig, targs[j:k], nil) // TODO(gri) provide xlist if possible (partial instantiations)
                        args[i].typ = asig
-                       check.recordInstance(args[i].expr, targs[j:k], asig)
                        j = k
                }
        }
index 640910424e98c6ef843789794636aa2965ec0ecc..313b4693460acde5efc94ee01f24154e52f0c607 100644 (file)
@@ -132,16 +132,15 @@ func (check *Checker) funcInst(tsig *Signature, pos token.Pos, x *operand, ix *t
        assert(got == want)
 
        // instantiate function signature
-       sig = check.instantiateSignature(x.Pos(), sig, targs, xlist)
-       assert(sig.TypeParams().Len() == 0) // signature is not generic anymore
+       expr := x.expr // if we don't have an index expression, keep the existing expression of x
+       if ix != nil {
+               expr = ix.Orig
+       }
+       sig = check.instantiateSignature(x.Pos(), expr, sig, targs, xlist)
 
        x.typ = sig
        x.mode = value
-       // If we don't have an index expression, keep the existing expression of x.
-       if ix != nil {
-               x.expr = ix.Orig
-       }
-       check.recordInstance(x.expr, targs, sig)
+       x.expr = expr
        return nil, nil
 }
 
@@ -164,7 +163,7 @@ func nth(n int) string {
        return fmt.Sprintf("%dth", n)
 }
 
-func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs []Type, xlist []ast.Expr) (res *Signature) {
+func (check *Checker) instantiateSignature(pos token.Pos, expr ast.Expr, typ *Signature, targs []Type, xlist []ast.Expr) (res *Signature) {
        assert(check != nil)
        assert(len(targs) == typ.TypeParams().Len())
 
@@ -178,6 +177,8 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs
        }
 
        inst := check.instance(pos, typ, targs, nil, check.context()).(*Signature)
+       assert(inst.TypeParams().Len() == 0) // signature is not generic anymore
+       check.recordInstance(expr, targs, inst)
        assert(len(xlist) <= len(targs))
 
        // verify instantiation lazily (was go.dev/issue/50450)
@@ -316,11 +317,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
                // of arguments is supplied).
                if got == want && want > 0 {
                        check.verifyVersionf(check.pkg, atPos(ix.Lbrack), go1_18, "function instantiation")
-
-                       sig = check.instantiateSignature(ix.Pos(), sig, targs, xlist)
-                       assert(sig.TypeParams().Len() == 0) // signature is not generic anymore
-                       check.recordInstance(ix.Orig, targs, sig)
-
+                       sig = check.instantiateSignature(ix.Pos(), ix.Orig, sig, targs, xlist)
                        // targs have been consumed; proceed with checking arguments of the
                        // non-generic signature.
                        targs = nil
@@ -575,9 +572,7 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
                // compute result signature: instantiate if needed
                rsig = sig
                if n > 0 {
-                       rsig = check.instantiateSignature(call.Pos(), sig, targs[:n], xlist)
-                       assert(rsig.TypeParams().Len() == 0) // signature is not generic anymore
-                       check.recordInstance(call.Fun, targs[:n], rsig)
+                       rsig = check.instantiateSignature(call.Pos(), call.Fun, sig, targs[:n], xlist)
                }
 
                // Optimization: Only if the callee's parameter list was adjusted do we need to
@@ -596,10 +591,8 @@ func (check *Checker) arguments(call *ast.CallExpr, sig *Signature, targs []Type
                        asig := args[i].typ.(*Signature)
                        k := j + asig.TypeParams().Len()
                        // targs[j:k] are the inferred type arguments for asig
-                       asig = check.instantiateSignature(call.Pos(), asig, targs[j:k], nil) // TODO(gri) provide xlist if possible (partial instantiations)
-                       assert(asig.TypeParams().Len() == 0)                                 // signature is not generic anymore
+                       asig = check.instantiateSignature(call.Pos(), args[i].expr, asig, targs[j:k], nil) // TODO(gri) provide xlist if possible (partial instantiations)
                        args[i].typ = asig
-                       check.recordInstance(args[i].expr, targs[j:k], asig)
                        j = k
                }
        }