From: Robert Findley Date: Tue, 16 Nov 2021 03:27:19 +0000 (-0500) Subject: go/types: move some functions into different files (cleanup) X-Git-Tag: go1.18beta1~298 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=50dac3b410b9bc47dabc3f3c2afd21f7aecfb118;p=gostls13.git 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 Reviewed-by: Robert Griesemer Run-TryBot: Robert Findley TryBot-Result: Go Bot --- diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go index 8d293a9af3..9b50403d7f 100644 --- a/src/go/types/builtins.go +++ b/src/go/types/builtins.go @@ -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) { diff --git a/src/go/types/type.go b/src/go/types/type.go index b1e2bda4cd..26a605444d 100644 --- a/src/go/types/type.go +++ b/src/go/types/type.go @@ -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