]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: remove most asX converters (cleanup)
authorRobert Findley <rfindley@google.com>
Tue, 9 Nov 2021 21:41:01 +0000 (16:41 -0500)
committerRobert Findley <rfindley@google.com>
Wed, 10 Nov 2021 00:44:59 +0000 (00:44 +0000)
This is a port of CL 362118 to go/types, which is itself a roll-forward
of CL 362254, containing a bugfix.

Change-Id: I20067c7adf56bf64fe9ad080d998a7aefbdc1053
Reviewed-on: https://go-review.googlesource.com/c/go/+/362617
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>
13 files changed:
src/go/types/assignments.go
src/go/types/builtins.go
src/go/types/call.go
src/go/types/context.go
src/go/types/conversions.go
src/go/types/expr.go
src/go/types/index.go
src/go/types/lookup.go
src/go/types/predicates.go
src/go/types/sizes.go
src/go/types/type.go
src/go/types/typestring.go
src/go/types/typexpr.go

index 2810133a1f93bcc53b7641069b30ab4206eac548..923bd43b49992c1484994cea15948b98e1e79bb6 100644 (file)
@@ -71,7 +71,7 @@ func (check *Checker) assignment(x *operand, T Type, context string) {
        }
 
        // A generic (non-instantiated) function value cannot be assigned to a variable.
-       if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
+       if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
                check.errorf(x, _Todo, "cannot use generic function %s without instantiation in %s", x, context)
        }
 
index 577a71fd6042f07d57c8c1216ce602d69b739fd8..4d3ff26b1437d1bcfdbd11626fa5cb82e083345f 100644 (file)
@@ -299,7 +299,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                // (applyTypeFunc never calls f with a type parameter)
                f := func(typ Type) Type {
                        assert(asTypeParam(typ) == nil)
-                       if t := asBasic(typ); t != nil {
+                       if t, _ := under(typ).(*Basic); t != nil {
                                switch t.kind {
                                case Float32:
                                        return Typ[Complex64]
@@ -423,7 +423,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                // (applyTypeFunc never calls f with a type parameter)
                f := func(typ Type) Type {
                        assert(asTypeParam(typ) == nil)
-                       if t := asBasic(typ); t != nil {
+                       if t, _ := under(typ).(*Basic); t != nil {
                                switch t.kind {
                                case Complex64:
                                        return Typ[Float32]
@@ -713,7 +713,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                        return
                }
 
-               typ := asPointer(x.typ)
+               typ, _ := under(x.typ).(*Pointer)
                if typ == nil {
                        check.invalidArg(x, _InvalidUnsafeSlice, "%s is not a pointer", x)
                        return
@@ -893,7 +893,7 @@ func makeSig(res Type, args ...Type) *Signature {
 // otherwise it returns typ.
 func arrayPtrDeref(typ Type) Type {
        if p, ok := typ.(*Pointer); ok {
-               if a := asArray(p.base); a != nil {
+               if a, _ := under(p.base).(*Array); a != nil {
                        return a
                }
        }
index a7024f5f9c5c6baa3dfb311f89fdb0b4b693f233..890a2c7c5ad0c7dd8daa29c002f23cbf904e31fb 100644 (file)
@@ -141,7 +141,7 @@ func (check *Checker) callExpr(x *operand, call *ast.CallExpr) exprKind {
                                        check.errorf(call.Args[0], _BadDotDotDotSyntax, "invalid use of ... in conversion to %s", T)
                                        break
                                }
-                               if t := asInterface(T); t != nil {
+                               if t, _ := under(T).(*Interface); t != nil {
                                        if !t.IsMethodSet() {
                                                check.errorf(call, _Todo, "cannot use interface %s in conversion (contains specific type constraints or is comparable)", T)
                                                break
index 99baad8d0f6b95a86750c46e2252c7339f8923f4..7caf631b57ecaadedafd871eaf129a307468ac7b 100644 (file)
@@ -40,6 +40,9 @@ func (ctxt *Context) typeHash(typ Type, targs []Type) string {
        var buf bytes.Buffer
 
        h := newTypeHasher(&buf, ctxt)
+       // Caution: don't use asNamed here. TypeHash may be called for unexpanded
+       // types. We don't need anything other than name and type arguments below,
+       // which do not require expansion.
        if named, _ := typ.(*Named); named != nil && len(targs) > 0 {
                // Don't use WriteType because we need to use the provided targs
                // and not any targs that might already be with the *Named type.
index f73e6a0964830326b425058092f2eb78c75c9613..26bebd4adebb82f09676eb16982c5ec31f0b6cd4 100644 (file)
@@ -17,7 +17,7 @@ func (check *Checker) conversion(x *operand, T Type) {
        constArg := x.mode == constant_
 
        constConvertibleTo := func(T Type, val *constant.Value) bool {
-               switch t := asBasic(T); {
+               switch t, _ := under(T).(*Basic); {
                case t == nil:
                        // nothing to do
                case representableConst(x.val, check, t, val):
@@ -170,9 +170,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
 
        // "V is a slice, T is a pointer-to-array type,
        // and the slice and array types have identical element types."
-       if s := asSlice(V); s != nil {
-               if p := asPointer(T); p != nil {
-                       if a := asArray(p.Elem()); a != nil {
+       if s, _ := under(V).(*Slice); s != nil {
+               if p, _ := under(T).(*Pointer); p != nil {
+                       if a, _ := under(p.Elem()).(*Array); a != nil {
                                if Identical(s.Elem(), a.Elem()) {
                                        if check == nil || check.allowVersion(check.pkg, 1, 17) {
                                                return true
@@ -254,26 +254,27 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
 // use the toT convenience converters in the predicates below.
 
 func isUintptr(typ Type) bool {
-       t := asBasic(typ)
+       t, _ := under(typ).(*Basic)
        return t != nil && t.kind == Uintptr
 }
 
 func isUnsafePointer(typ Type) bool {
-       // TODO(gri): Is this asBasic(typ) instead of typ.(*Basic) correct?
+       // TODO(gri): Is this under(typ).(*Basic) instead of typ.(*Basic) correct?
        //            (The former calls under(), while the latter doesn't.)
        //            The spec does not say so, but gc claims it is. See also
        //            issue 6326.
-       t := asBasic(typ)
+       t, _ := under(typ).(*Basic)
        return t != nil && t.kind == UnsafePointer
 }
 
 func isPointer(typ Type) bool {
-       return asPointer(typ) != nil
+       _, ok := under(typ).(*Pointer)
+       return ok
 }
 
 func isBytesOrRunes(typ Type) bool {
-       if s := asSlice(typ); s != nil {
-               t := asBasic(s.elem)
+       if s, _ := under(typ).(*Slice); s != nil {
+               t, _ := under(s.elem).(*Basic)
                return t != nil && (t.kind == Byte || t.kind == Rune)
        }
        return false
index 224185b6a9b05d238785955190d57eeda24bfada..138eb2f5214ada4cc9313d8b15370f437994a59a 100644 (file)
@@ -103,7 +103,7 @@ func (check *Checker) overflow(x *operand, op token.Token, opPos token.Pos) {
        // x.typ cannot be a type parameter (type
        // parameters cannot be constant types).
        if isTyped(x.typ) {
-               check.representable(x, asBasic(x.typ))
+               check.representable(x, under(x.typ).(*Basic))
                return
        }
 
@@ -556,7 +556,7 @@ func (check *Checker) updateExprType(x ast.Expr, typ Type, final bool) {
        // If the new type is not final and still untyped, just
        // update the recorded type.
        if !final && isUntyped(typ) {
-               old.typ = asBasic(typ)
+               old.typ = under(typ).(*Basic)
                check.untyped[x] = old
                return
        }
@@ -1362,7 +1362,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
                                        duplicate := false
                                        // if the key is of interface type, the type is also significant when checking for duplicates
                                        xkey := keyVal(x.val)
-                                       if asInterface(utyp.key) != nil {
+                                       if IsInterface(utyp.key) {
                                                for _, vtyp := range visited[xkey] {
                                                        if Identical(vtyp, x.typ) {
                                                                duplicate = true
index 7ef8231f0b4d1674fa58a8546f67b46e58e1b8e0..cd19f50627f44c9c77df217149159a2c946946ea 100644 (file)
@@ -35,7 +35,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
                return false
 
        case value:
-               if sig := asSignature(x.typ); sig != nil && sig.TypeParams().Len() > 0 {
+               if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
                        // function instantiation
                        return true
                }
@@ -73,7 +73,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
                x.typ = typ.elem
 
        case *Pointer:
-               if typ := asArray(typ.base); typ != nil {
+               if typ, _ := under(typ.base).(*Array); typ != nil {
                        valid = true
                        length = typ.len
                        x.mode = variable
@@ -121,7 +121,7 @@ func (check *Checker) indexExpr(x *operand, e *typeparams.IndexExpr) (isFuncInst
                                        mode = value
                                }
                        case *Pointer:
-                               if t := asArray(t.base); t != nil {
+                               if t, _ := under(t.base).(*Array); t != nil {
                                        l = t.len
                                        e = t.elem
                                }
@@ -246,7 +246,7 @@ func (check *Checker) sliceExpr(x *operand, e *ast.SliceExpr) {
                x.typ = &Slice{elem: u.elem}
 
        case *Pointer:
-               if u := asArray(u.base); u != nil {
+               if u, _ := under(u.base).(*Array); u != nil {
                        valid = true
                        length = u.len
                        x.typ = &Slice{elem: u.elem}
index afb1215af28639359e692ce5d475ac2b342f413c..aae6fa206df03e4bd7984fb502512eef45c17c32 100644 (file)
@@ -122,7 +122,6 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
                                seen[named] = true
 
                                // look for a matching attached method
-                               named.resolve(nil)
                                if i, m := lookupMethod(named.methods, pkg, name); m != nil {
                                        // potential match
                                        // caution: method may not have a proper signature yet
@@ -302,7 +301,7 @@ func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method,
                return
        }
 
-       if ityp := asInterface(V); ityp != nil {
+       if ityp, _ := under(V).(*Interface); ityp != nil {
                // TODO(gri) the methods are sorted - could do this more efficiently
                for _, m := range T.typeSet().methods {
                        _, f := ityp.typeSet().LookupMethod(m.pkg, m.name)
@@ -400,7 +399,7 @@ func (check *Checker) assertableTo(V *Interface, T Type) (method, wrongType *Fun
        // no static check is required if T is an interface
        // spec: "If T is an interface type, x.(T) asserts that the
        //        dynamic type of x implements the interface T."
-       if asInterface(T) != nil && !forceStrict {
+       if IsInterface(T) && !forceStrict {
                return
        }
        return check.missingMethod(T, V, false)
@@ -418,8 +417,8 @@ func deref(typ Type) (Type, bool) {
 // derefStructPtr dereferences typ if it is a (named or unnamed) pointer to a
 // (named or unnamed) struct and returns its base. Otherwise it returns typ.
 func derefStructPtr(typ Type) Type {
-       if p := asPointer(typ); p != nil {
-               if asStruct(p.base) != nil {
+       if p, _ := under(typ).(*Pointer); p != nil {
+               if _, ok := under(p.base).(*Struct); ok {
                        return p.base
                }
        }
index 622c773126dfe20ef8dda978ba7d99a58ed5b486..e8689a12cc5c8ebe91b4de9138a0f02a27111a1b 100644 (file)
@@ -74,7 +74,7 @@ func hasName(t Type) bool {
 // are not fully set up.
 func isTyped(t Type) bool {
        // isTyped is called with types that are not fully
-       // set up. Must not call asBasic()!
+       // set up. Must not call under()!
        b, _ := t.(*Basic)
        return b == nil || b.info&IsUntyped == 0
 }
@@ -86,7 +86,8 @@ func isUntyped(t Type) bool {
 
 // IsInterface reports whether t is an interface type.
 func IsInterface(t Type) bool {
-       return asInterface(t) != nil
+       _, ok := under(t).(*Interface)
+       return ok
 }
 
 // isTypeParam reports whether t is a type parameter.
index 4c85bfe057f8ddd7c37858aac7df59c29c3ba22c..9a119138dd5536a5ab84b8fab884ff76273162ae 100644 (file)
@@ -243,7 +243,7 @@ func (conf *Config) offsetsof(T *Struct) []int64 {
 func (conf *Config) offsetof(typ Type, index []int) int64 {
        var o int64
        for _, i := range index {
-               s := asStruct(typ)
+               s := under(typ).(*Struct)
                o += conf.offsetsof(s)[i]
                typ = s.fields[i].typ
        }
index 4247f52c31f3bbbb78b5d9e48d0c67c155e6e0f5..b1e2bda4cd6b3f078ac6ad8c1be7d101c385eba6 100644 (file)
@@ -27,45 +27,8 @@ func under(t Type) Type {
        return t
 }
 
-// Convenience converters
-
-func asBasic(t Type) *Basic {
-       op, _ := under(t).(*Basic)
-       return op
-}
-
-func asArray(t Type) *Array {
-       op, _ := under(t).(*Array)
-       return op
-}
-
-func asSlice(t Type) *Slice {
-       op, _ := under(t).(*Slice)
-       return op
-}
-
-func asStruct(t Type) *Struct {
-       op, _ := under(t).(*Struct)
-       return op
-}
-
-func asPointer(t Type) *Pointer {
-       op, _ := under(t).(*Pointer)
-       return op
-}
-
-func asSignature(t Type) *Signature {
-       op, _ := under(t).(*Signature)
-       return op
-}
-
-func asInterface(t Type) *Interface {
-       op, _ := under(t).(*Interface)
-       return op
-}
-
 // If the argument to asNamed, or asTypeParam is of the respective type
-// (possibly after expanding resolving a *Named type), these methods return that type.
+// (possibly after resolving a *Named type), these methods return that type.
 // Otherwise the result is nil.
 
 func asNamed(t Type) *Named {
index e138af64880f58ac70d7d3bdc158b0f52f578e09..c448d254587fb4fd8e71be2302b5b3c3ef85caa4 100644 (file)
@@ -362,7 +362,7 @@ func (w *typeWriter) tuple(tup *Tuple, variadic bool) {
                                } else {
                                        // special case:
                                        // append(s, "foo"...) leads to signature func([]byte, string...)
-                                       if t := asBasic(typ); t == nil || t.kind != String {
+                                       if t, _ := under(typ).(*Basic); t == nil || t.kind != String {
                                                w.error("expected string type")
                                                continue
                                        }
index cc2bd622098ea398626bab3602367a4c7aa018e8..12e0f968c21d26867e05c312c44fb5efb165fdc5 100644 (file)
@@ -145,7 +145,7 @@ func (check *Checker) varType(e ast.Expr) Type {
        // are in the middle of type-checking parameter declarations that might belong to
        // interface methods. Delay this check to the end of type-checking.
        check.later(func() {
-               if t := asInterface(typ); t != nil {
+               if t, _ := under(typ).(*Interface); t != nil {
                        tset := computeInterfaceTypeSet(check, e.Pos(), t) // TODO(gri) is this the correct position?
                        if !tset.IsMethodSet() {
                                if tset.comparable {