]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: minor cleanups in predicates.go
authorRobert Findley <rfindley@google.com>
Tue, 9 Nov 2021 16:09:13 +0000 (11:09 -0500)
committerRobert Findley <rfindley@google.com>
Tue, 9 Nov 2021 19:01:06 +0000 (19:01 +0000)
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>
src/go/types/predicates.go

index 1ecb6a8c7ebdfec6365ecfa3507fa5ec094a38e2..622c773126dfe20ef8dda978ba7d99a58ed5b486 100644 (file)
@@ -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
 }