go/types: move some functions into different files (cleanup)

This is a clean port of CL 362995 from types2 to go/types.

Change-Id: Iefc37b28178795ea944e0bc0ff91982251de2944
Reviewed-on: https://go-review.googlesource.com/c/go/+/363989
Trust: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Robert Findley 2021-11-15 22:27:19 -05:00
parent 67c1556815
commit 50dac3b410
2 changed files with 41 additions and 43 deletions

View File

@ -776,46 +776,6 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
return true
}
// If typ is a type parameter, structuralType returns the single underlying
// type of all types in the corresponding type constraint if it exists, or
// nil otherwise. If typ is not a type parameter, structuralType returns
// the underlying type.
func structuralType(typ Type) Type {
var su Type
if underIs(typ, func(u Type) bool {
if su != nil && !Identical(su, u) {
return false
}
// su == nil || Identical(su, u)
su = u
return true
}) {
return su
}
return nil
}
// structuralString is like structuralType but also considers []byte
// and string as "identical". In this case, if successful, the result
// is always []byte.
func structuralString(typ Type) Type {
var su Type
if underIs(typ, func(u Type) bool {
if isString(u) {
u = NewSlice(universeByte)
}
if su != nil && !Identical(su, u) {
return false
}
// su == nil || Identical(su, u)
su = u
return true
}) {
return su
}
return nil
}
// hasVarSize reports if the size of type t is variable due to type parameters.
func hasVarSize(t Type) bool {
switch t := under(t).(type) {

View File

@ -27,10 +27,47 @@ func under(t Type) Type {
return t
}
// If the argument to asNamed, or asTypeParam is of the respective type
// (possibly after resolving a *Named type), these methods return that type.
// Otherwise the result is nil.
// If typ is a type parameter, structuralType returns the single underlying
// type of all types in the corresponding type constraint if it exists,
// or nil otherwise. If typ is not a type parameter, structuralType returns
// the underlying type.
func structuralType(typ Type) Type {
var su Type
if underIs(typ, func(u Type) bool {
if su != nil && !Identical(su, u) {
return false
}
// su == nil || Identical(su, u)
su = u
return true
}) {
return su
}
return nil
}
// structuralString is like structuralType but also considers []byte
// and string as "identical". In this case, if successful, the result
// is always []byte.
func structuralString(typ Type) Type {
var su Type
if underIs(typ, func(u Type) bool {
if isString(u) {
u = NewSlice(universeByte)
}
if su != nil && !Identical(su, u) {
return false
}
// su == nil || Identical(su, u)
su = u
return true
}) {
return su
}
return nil
}
// If t is a defined type, asNamed returns that type (possibly after resolving it), otherwise it returns nil.
func asNamed(t Type) *Named {
e, _ := t.(*Named)
if e != nil {
@ -39,6 +76,7 @@ func asNamed(t Type) *Named {
return e
}
// If t is a type parameter, asTypeParam returns that type, otherwise it returns nil.
func asTypeParam(t Type) *TypeParam {
u, _ := under(t).(*TypeParam)
return u