internal/typeparams: update to the new Instances API

This CL updates the internal/typeparams package with the new API from CL
349629, switching Info.Inferred to Info.Instances.

Change-Id: I8f98dc39c844ef8891863c08b747f63924ab1c53
Reviewed-on: https://go-review.googlesource.com/c/tools/+/351250
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>
diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index 2eb2d1e..4f27ed7 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -439,7 +439,7 @@
 		},
 		typesSizes: m.TypesSizes,
 	}
-	typeparams.InitInferred(pkg.typesInfo)
+	typeparams.InitInstanceInfo(pkg.typesInfo)
 
 	for _, gf := range pkg.m.GoFiles {
 		// In the presence of line directives, we may need to report errors in
diff --git a/internal/lsp/source/identifier.go b/internal/lsp/source/identifier.go
index 2bc3431..c709d8d 100644
--- a/internal/lsp/source/identifier.go
+++ b/internal/lsp/source/identifier.go
@@ -299,7 +299,7 @@
 		return result, nil
 	}
 
-	result.Inferred = inferredSignature(pkg.GetTypesInfo(), path)
+	result.Inferred = inferredSignature(pkg.GetTypesInfo(), ident)
 
 	result.Type.Object = typeToObject(typ)
 	if result.Type.Object != nil {
@@ -347,52 +347,13 @@
 }
 
 // inferredSignature determines the resolved non-generic signature for an
-// identifier with a generic signature that is the operand of an IndexExpr or
-// CallExpr.
+// identifier in an instantiation expression.
 //
 // If no such signature exists, it returns nil.
-func inferredSignature(info *types.Info, path []ast.Node) *types.Signature {
-	if len(path) < 2 {
-		return nil
-	}
-	// There are four ways in which a signature may be resolved:
-	//  1. It has no explicit type arguments, but the CallExpr can be fully
-	//     inferred from function arguments.
-	//  2. It has full type arguments, and the IndexExpr has a non-generic type.
-	//  3. For a partially instantiated IndexExpr representing a function-valued
-	//     expression (i.e. not part of a CallExpr), type arguments may be
-	//     inferred using constraint type inference.
-	//  4. For a partially instantiated IndexExpr that is part of a CallExpr,
-	//     type arguments may be inferred using both constraint type inference
-	//     and function argument inference.
-	//
-	// These branches are handled below.
-	switch n := path[1].(type) {
-	case *ast.CallExpr:
-		_, sig := typeparams.GetInferred(info, n)
-		return sig
-	default:
-		if ix := typeparams.GetIndexExprData(n); ix != nil {
-			e := n.(ast.Expr)
-			// If the IndexExpr is fully instantiated, we consider that 'inference' for
-			// gopls' purposes.
-			sig, _ := info.TypeOf(e).(*types.Signature)
-			if sig != nil && typeparams.ForSignature(sig).Len() == 0 {
-				return sig
-			}
-			_, sig = typeparams.GetInferred(info, e)
-			if sig != nil {
-				return sig
-			}
-			if len(path) >= 2 {
-				if call, _ := path[2].(*ast.CallExpr); call != nil {
-					_, sig := typeparams.GetInferred(info, call)
-					return sig
-				}
-			}
-		}
-	}
-	return nil
+func inferredSignature(info *types.Info, id *ast.Ident) *types.Signature {
+	_, typ := typeparams.GetInstance(info, id)
+	sig, _ := typ.(*types.Signature)
+	return sig
 }
 
 func searchForEnclosing(info *types.Info, path []ast.Node) types.Type {
diff --git a/internal/typeparams/typeparams_go117.go b/internal/typeparams/typeparams_go117.go
index 5b8a35c..6ff07ea 100644
--- a/internal/typeparams/typeparams_go117.go
+++ b/internal/typeparams/typeparams_go117.go
@@ -159,15 +159,12 @@
 	return nil
 }
 
-// InitInferred is a noop at this Go version.
-func InitInferred(*types.Info) {
-}
+// InitInstanceInfo is a noop at this Go version.
+func InitInstanceInfo(*types.Info) {}
 
-// GetInferred returns nothing, as type parameters are not supported at this Go
+// GetInstance returns nothing, as type parameters are not supported at this Go
 // version.
-func GetInferred(*types.Info, ast.Expr) ([]types.Type, *types.Signature) {
-	return nil, nil
-}
+func GetInstance(*types.Info, *ast.Ident) (*TypeList, types.Type) { return nil, nil }
 
 // Environment is a placeholder type, as type parameters are not supported at
 // this Go version.
diff --git a/internal/typeparams/typeparams_go118.go b/internal/typeparams/typeparams_go118.go
index b7547d7..953e5c6 100644
--- a/internal/typeparams/typeparams_go118.go
+++ b/internal/typeparams/typeparams_go118.go
@@ -136,32 +136,21 @@
 	return types.NewUnion(terms)
 }
 
-// InitInferred initializes info to record inferred type information.
-func InitInferred(info *types.Info) {
-	info.Inferred = make(map[ast.Expr]types.Inferred)
+// InitInstanceInfo initializes info to record information about type and
+// function instances.
+func InitInstanceInfo(info *types.Info) {
+	info.Instances = make(map[*ast.Ident]types.Instance)
 }
 
-// GetInferred extracts inferred type information from info for e.
-//
-// The expression e may have an inferred type if it is an *ast.IndexExpr
-// representing partial instantiation of a generic function type for which type
-// arguments have been inferred using constraint type inference, or if it is an
-// *ast.CallExpr for which type type arguments have be inferred using both
-// constraint type inference and function argument inference.
-func GetInferred(info *types.Info, e ast.Expr) ([]types.Type, *types.Signature) {
-	if info.Inferred == nil {
-		return nil, nil
+// GetInstance extracts information about the instantiation occurring at the
+// identifier id. id should be the identifier denoting a parameterized type or
+// function in an instantiation expression or function call.
+func GetInstance(info *types.Info, id *ast.Ident) (*TypeList, types.Type) {
+	if info.Instances != nil {
+		inf := info.Instances[id]
+		return inf.TypeArgs, inf.Type
 	}
-	inf := info.Inferred[e]
-
-	length := inf.TArgs.Len()
-
-	typs := make([]types.Type, length)
-	for i := 0; i < length; i++ {
-		typs[i] = inf.TArgs.At(i)
-	}
-
-	return typs, inf.Sig
+	return nil, nil
 }
 
 // Environment is an alias for types.Environment.