From: Robert Griesemer Date: Thu, 11 May 2023 20:55:27 +0000 (-0700) Subject: go/types, types2: call recordInstance in instantiateSignature X-Git-Tag: go1.21rc1~548 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=803a7dfb80ace1fe31528a81ed7bb6ab56b5d930;p=gostls13.git go/types, types2: call recordInstance in instantiateSignature 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 Reviewed-by: Robert Griesemer Auto-Submit: Robert Griesemer Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer --- diff --git a/src/cmd/compile/internal/types2/call.go b/src/cmd/compile/internal/types2/call.go index 075dfd1efa..4b854df774 100644 --- a/src/cmd/compile/internal/types2/call.go +++ b/src/cmd/compile/internal/types2/call.go @@ -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 } } diff --git a/src/go/types/call.go b/src/go/types/call.go index 640910424e..313b469346 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -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 } }