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>
This commit is contained in:
Robert Findley 2021-09-21 11:54:34 -04:00
parent fe076c893b
commit 0d12d39b86
4 changed files with 23 additions and 76 deletions

View File

@ -439,7 +439,7 @@ func doTypeCheck(ctx context.Context, snapshot *snapshot, m *Metadata, mode sour
},
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

View File

@ -299,7 +299,7 @@ func findIdentifier(ctx context.Context, snapshot Snapshot, pkg Package, pgf *Pa
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 @@ func fullNode(snapshot Snapshot, obj types.Object, pkg Package) (ast.Decl, error
}
// 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 {

View File

@ -159,15 +159,12 @@ func NewUnion(terms []*Term) *Union {
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.

View File

@ -136,32 +136,21 @@ func NewUnion(terms []*Term) *Union {
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.