]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types: remove asNamed
authorRobert Findley <rfindley@google.com>
Thu, 18 Nov 2021 00:04:17 +0000 (19:04 -0500)
committerRobert Findley <rfindley@google.com>
Thu, 18 Nov 2021 02:11:51 +0000 (02:11 +0000)
This is a port of CL 363441 from types2 to go/types, with an additional
adjustment in methodset.go.

Change-Id: Ia04dcfb70bb9f6af6f45175dee3334dba7b2768e
Reviewed-on: https://go-review.googlesource.com/c/go/+/364895
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/decl.go
src/go/types/lookup.go
src/go/types/methodset.go
src/go/types/type.go
src/go/types/unify.go
src/go/types/universe.go

index 4f28553aa6af08b0916897d98f598662c757f5a5..600467620cce46d070794728c5c3c2028bcd5df3 100644 (file)
@@ -795,7 +795,7 @@ func (check *Checker) collectMethods(obj *TypeName) {
 
        // spec: "If the base type is a struct type, the non-blank method
        // and field names must be distinct."
-       base := asNamed(obj.typ) // shouldn't fail but be conservative
+       base, _ := obj.typ.(*Named) // shouldn't fail but be conservative
        if base != nil {
                u := base.under()
                if t, _ := u.(*Struct); t != nil {
index 16a989019919d7363f65449225fd645cdb100da8..1462d30b309d43ea3624455ca5e7ab662bf47250 100644 (file)
@@ -50,8 +50,8 @@ func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
        // Thus, if we have a named pointer type, proceed with the underlying
        // pointer type but discard the result if it is a method since we would
        // not have found it for T (see also issue 8590).
-       if t := asNamed(T); t != nil {
-               if p, _ := safeUnderlying(t).(*Pointer); p != nil {
+       if t, _ := T.(*Named); t != nil {
+               if p, _ := t.Underlying().(*Pointer); p != nil {
                        obj, index, indirect = lookupFieldOrMethod(p, false, pkg, name)
                        if _, ok := obj.(*Func); ok {
                                return nil, nil, false
@@ -114,7 +114,7 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named := asNamed(typ); named != nil {
+                       if named, _ := typ.(*Named); named != nil {
                                if seen[named] {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
@@ -129,6 +129,7 @@ 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
index 89e4b57627f9e337f77e7a9eff0832cd03130e1d..e17be7c41af748b53e9fac3cb7bbd0e18422a4f5 100644 (file)
@@ -111,7 +111,7 @@ func NewMethodSet(T Type) *MethodSet {
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named := asNamed(typ); named != nil {
+                       if named, _ := typ.(*Named); named != nil {
                                if seen[named] {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
index dcf678a27aa57e937ca64ceb9aea7cecab4642a4..1d672135b8b69818b3c084b34637c719a3d259be 100644 (file)
@@ -87,12 +87,3 @@ func structuralType(typ Type) Type {
        }
        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 {
-               e.resolve(nil)
-       }
-       return e
-}
index 6cd653aee1bd9977fd701580a9838ddaba19d0d2..5dcb35f6ecaec759b0b53ae631892b523e261668 100644 (file)
@@ -241,14 +241,12 @@ func (u *unifier) nify(x, y Type, p *ifacePair) bool {
                // If exact unification is known to fail because we attempt to
                // match a type name against an unnamed type literal, consider
                // the underlying type of the named type.
-               // (Subtle: We use hasName to include any type with a name (incl.
-               // basic types and type parameters. We use asNamed because we only
-               // want *Named types.)
-               switch {
-               case !hasName(x) && y != nil && asNamed(y) != nil:
-                       return u.nify(x, under(y), p)
-               case x != nil && asNamed(x) != nil && !hasName(y):
-                       return u.nify(under(x), y, p)
+               // (We use !hasName to exclude any type with a name, including
+               // basic types and type parameters; the rest are unamed types.)
+               if nx, _ := x.(*Named); nx != nil && !hasName(y) {
+                       return u.nify(nx.under(), y, p)
+               } else if ny, _ := y.(*Named); ny != nil && !hasName(x) {
+                       return u.nify(x, ny.under(), p)
                }
        }
 
index e30ab12bc356cab050173e7f4dec541a57fe08ec..edda56fc0dbf174998c970b2d901a394a5e51fbf 100644 (file)
@@ -244,7 +244,7 @@ func def(obj Object) {
                return // nothing to do
        }
        // fix Obj link for named types
-       if typ := asNamed(obj.Type()); typ != nil {
+       if typ, _ := obj.Type().(*Named); typ != nil {
                typ.obj = obj.(*TypeName)
        }
        // exported identifiers go into package unsafe