1
0
mirror of https://github.com/golang/go synced 2024-11-26 16:46:58 -07:00

go/types, types2: generalize tparamIndex to arbitrary type parameter lists

tparamIndex returns the index of a type parameter given the type
parameter and a list of type parameters. If an index >= 0 is returned,
it is the index assigned to the type parameter (TypeParam.index), and
the index of the type parameter in the provided list of parameters.
For it to work correctly, the type parameter list must be from a single
type parameter declaration.

To allow for lists of arbitrary type parameters (from different generic
signatures), change the implementation to do a linear search. The result
is the index of the type parameter in the provided type parameter list,
which may be different from the index assigned to the type parameter.

The linear search is likely fast enough since type parameter lists tend
to be very short.

Change-Id: I913f97fa4c042abeb535ee86ca6657241a4cf796
Reviewed-on: https://go-review.googlesource.com/c/go/+/483995
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Robert Griesemer 2023-04-12 09:43:41 -07:00 committed by Gopher Robot
parent c38d122fd4
commit f05c1941dd
2 changed files with 12 additions and 20 deletions

View File

@ -694,17 +694,13 @@ func (w *cycleFinder) varList(list []*Var) {
}
}
// If tpar is a type parameter in list, tparamIndex returns the type parameter index.
// Otherwise, the result is < 0. tpar must not be nil.
// If tpar is a type parameter in list, tparamIndex returns the index
// of the type parameter in list. Otherwise the result is < 0.
func tparamIndex(list []*TypeParam, tpar *TypeParam) int {
// Once a type parameter is bound its index is >= 0. However, there are some
// code paths (namely tracing and type hashing) by which it is possible to
// arrive here with a type parameter that has not been bound, hence the check
// for 0 <= i below.
// TODO(rfindley): investigate a better approach for guarding against using
// unbound type parameters.
if i := tpar.index; 0 <= i && i < len(list) && list[i] == tpar {
return i
for i, p := range list {
if p == tpar {
return i
}
}
return -1
}

View File

@ -696,17 +696,13 @@ func (w *cycleFinder) varList(list []*Var) {
}
}
// If tpar is a type parameter in list, tparamIndex returns the type parameter index.
// Otherwise, the result is < 0. tpar must not be nil.
// If tpar is a type parameter in list, tparamIndex returns the index
// of the type parameter in list. Otherwise the result is < 0.
func tparamIndex(list []*TypeParam, tpar *TypeParam) int {
// Once a type parameter is bound its index is >= 0. However, there are some
// code paths (namely tracing and type hashing) by which it is possible to
// arrive here with a type parameter that has not been bound, hence the check
// for 0 <= i below.
// TODO(rfindley): investigate a better approach for guarding against using
// unbound type parameters.
if i := tpar.index; 0 <= i && i < len(list) && list[i] == tpar {
return i
for i, p := range list {
if p == tpar {
return i
}
}
return -1
}