]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: update the recorded function type after inference
authorRobert Findley <rfindley@google.com>
Mon, 4 Oct 2021 18:17:50 +0000 (14:17 -0400)
committerRobert Findley <rfindley@google.com>
Mon, 4 Oct 2021 21:57:33 +0000 (21:57 +0000)
This change preserves the observable invariant that for an *ast.CallExpr
'call', Info.Types[call.Fun] is the signature being called.

Updates #47916

Change-Id: I3e97c712a7ee33a4f29e8cf4c18dc7c946b66cc9
Reviewed-on: https://go-review.googlesource.com/c/go/+/353831
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/types/api_test.go
src/go/types/call.go

index f1a820988fa481526a94fb217057d7a3d1011f15..9044449e5c793f448a37912f54f888dabc109204 100644 (file)
@@ -347,8 +347,10 @@ func TestTypesInfo(t *testing.T) {
                // parameterized functions
                {genericPkg + `p0; func f[T any](T) {}; var _ = f[int]`, `f`, `func[generic_p0.T₁ interface{}](generic_p0.T₁)`},
                {genericPkg + `p1; func f[T any](T) {}; var _ = f[int]`, `f[int]`, `func(int)`},
-               {genericPkg + `p2; func f[T any](T) {}; func _() { f(42) }`, `f`, `func[generic_p2.T₁ interface{}](generic_p2.T₁)`},
-               {genericPkg + `p3; func f[T any](T) {}; func _() { f(42) }`, `f(42)`, `()`},
+               {genericPkg + `p2; func f[T any](T) {}; func _() { f(42) }`, `f`, `func(int)`},
+               {genericPkg + `p3; func f[T any](T) {}; func _() { f[int](42) }`, `f[int]`, `func(int)`},
+               {genericPkg + `p4; func f[T any](T) {}; func _() { f[int](42) }`, `f`, `func[generic_p4.T₁ interface{}](generic_p4.T₁)`},
+               {genericPkg + `p5; func f[T any](T) {}; func _() { f(42) }`, `f(42)`, `()`},
 
                // type parameters
                {genericPkg + `t0; type t[] int; var _ t`, `t`, `generic_t0.t`}, // t[] is a syntax error that is ignored in this test in favor of t
index 98a8fda9d1adaf9a210ac4a3f423c91cc7e74094..a642f6f295e23fabd588c832adeff51f3c0e414f 100644 (file)
@@ -85,7 +85,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
        } else {
                check.exprOrType(x, call.Fun, true)
        }
-       // x.typ map be generic
+       // x.typ may be generic
 
        switch x.mode {
        case invalid:
@@ -177,8 +177,14 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
 
        // evaluate arguments
        args, _ := check.exprList(call.Args, false)
+       isGeneric := sig.TypeParams().Len() > 0
        sig = check.arguments(call, sig, targs, args)
 
+       if isGeneric && sig.TypeParams().Len() == 0 {
+               // Update the recorded type of call.Fun to its instantiated type.
+               check.recordTypeAndValue(call.Fun, value, sig, nil)
+       }
+
        // determine result
        switch sig.results.Len() {
        case 0: