diff options
author | Robert Findley <rfindley@google.com> | 2021-11-09 11:09:13 -0500 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-11-09 19:01:06 +0000 |
commit | 81f37a72ea8a05ea3f5771a92b34b352769518cf (patch) | |
tree | f940e1d27cca8a97df7a7611d9c8bc05514bd465 /src/go/types/predicates.go | |
parent | 01103d533a086afd6c06f3eec5057d46f117d2ff (diff) | |
download | go-81f37a72ea8a05ea3f5771a92b34b352769518cf.tar.gz go-81f37a72ea8a05ea3f5771a92b34b352769518cf.zip |
go/types: minor cleanups in predicates.go
This is a clean port of CL 360956 to go/types.
Change-Id: Iac437e72bb760e7e90236a86e7473d6a440df081
Reviewed-on: https://go-review.googlesource.com/c/go/+/362615
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>
Diffstat (limited to 'src/go/types/predicates.go')
-rw-r--r-- | src/go/types/predicates.go | 87 |
1 files changed, 41 insertions, 46 deletions
diff --git a/src/go/types/predicates.go b/src/go/types/predicates.go index 1ecb6a8c7e..622c773126 100644 --- a/src/go/types/predicates.go +++ b/src/go/types/predicates.go @@ -8,25 +8,6 @@ package types import "go/token" -// hasName reports whether typ has a name. This includes -// predeclared types, defined types, and type parameters. -// hasName may be called with types that are not fully set up. -func hasName(typ Type) bool { - switch typ.(type) { - case *Basic, *Named, *TypeParam: - return true - } - return false -} - -// isGeneric reports whether a type is a generic, uninstantiated type (generic -// signatures are not included). -func isGeneric(typ Type) bool { - // A parameterized type is only instantiated if it doesn't have an instantiation already. - named, _ := typ.(*Named) - return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil -} - // The isX predicates below report whether t is an X. // If t is a type parameter the result is false; i.e., // these predicates don't look inside a type parameter. @@ -39,6 +20,7 @@ func isComplex(t Type) bool { return isBasic(t, IsComplex) } func isNumeric(t Type) bool { return isBasic(t, IsNumeric) } func isString(t Type) bool { return isBasic(t, IsString) } func isIntegerOrFloat(t Type) bool { return isBasic(t, IsInteger|IsFloat) } +func isConstType(t Type) bool { return isBasic(t, IsConstType) } // isBasic reports whether under(t) is a basic type with the specified info. // If t is a type parameter the result is false; i.e., @@ -76,36 +58,50 @@ func allBasic(t Type, info BasicInfo) bool { return false } -// isTyped reports whether typ is typed; i.e., not an untyped +// hasName reports whether t has a name. This includes +// predeclared types, defined types, and type parameters. +// hasName may be called with types that are not fully set up. +func hasName(t Type) bool { + switch t.(type) { + case *Basic, *Named, *TypeParam: + return true + } + return false +} + +// isTyped reports whether t is typed; i.e., not an untyped // constant or boolean. isTyped may be called with types that // are not fully set up. -func isTyped(typ Type) bool { +func isTyped(t Type) bool { // isTyped is called with types that are not fully // set up. Must not call asBasic()! - t, _ := typ.(*Basic) - return t == nil || t.info&IsUntyped == 0 + b, _ := t.(*Basic) + return b == nil || b.info&IsUntyped == 0 } -// isUntyped(typ) is the same as !isTyped(typ). -func isUntyped(typ Type) bool { - return !isTyped(typ) +// isUntyped(t) is the same as !isTyped(t). +func isUntyped(t Type) bool { + return !isTyped(t) } -func isConstType(typ Type) bool { - // Type parameters are never const types. - t := asBasic(typ) - return t != nil && t.info&IsConstType != 0 +// IsInterface reports whether t is an interface type. +func IsInterface(t Type) bool { + return asInterface(t) != nil } -// IsInterface reports whether typ is an interface type. -func IsInterface(typ Type) bool { - return asInterface(typ) != nil +// isTypeParam reports whether t is a type parameter. +func isTypeParam(t Type) bool { + _, ok := under(t).(*TypeParam) + return ok } -// isTypeParam reports whether typ is a type parameter. -func isTypeParam(typ Type) bool { - _, ok := under(typ).(*TypeParam) - return ok +// isGeneric reports whether a type is a generic, uninstantiated type +// (generic signatures are not included). +// TODO(gri) should we include signatures or assert that they are not present? +func isGeneric(t Type) bool { + // A parameterized type is only generic if it doesn't have an instantiation already. + named, _ := t.(*Named) + return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil } // Comparable reports whether values of type T are comparable. @@ -144,15 +140,15 @@ func comparable(T Type, seen map[Type]bool) bool { return false } -// hasNil reports whether a type includes the nil value. -func hasNil(typ Type) bool { - switch t := under(typ).(type) { +// hasNil reports whether type t includes the nil value. +func hasNil(t Type) bool { + switch u := under(t).(type) { case *Basic: - return t.kind == UnsafePointer + return u.kind == UnsafePointer case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan: return true case *TypeParam: - return t.underIs(hasNil) + return u.underIs(hasNil) } return false } @@ -394,9 +390,8 @@ func identicalTParams(x, y []*TypeParam, cmpTags bool, p *ifacePair) bool { // Default returns the default "typed" type for an "untyped" type; // it returns the incoming type for all other types. The default type // for untyped nil is untyped nil. -// -func Default(typ Type) Type { - if t, ok := typ.(*Basic); ok { +func Default(t Type) Type { + if t, ok := t.(*Basic); ok { switch t.kind { case UntypedBool: return Typ[Bool] @@ -412,5 +407,5 @@ func Default(typ Type) Type { return Typ[String] } } - return typ + return t } |