]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: remove asNamed
authorRobert Griesemer <gri@golang.org>
Fri, 12 Nov 2021 06:21:48 +0000 (22:21 -0800)
committerRobert Griesemer <gri@golang.org>
Sat, 13 Nov 2021 00:26:23 +0000 (00:26 +0000)
In the few remaining places where we use asNamed, if the argument
is indeed a *Named, we either don't need to look "inside" it, or
we call under() (which calls Named.underlying() which does resolve);
so there's no need for an implicit resolution (which was done by
asNamed). The only place where we do need to resolve is in lookup,
so added the explicit resolve call in that case.

Change-Id: Iff0a19fde7581e94149e89b9e48157c1981db105
Reviewed-on: https://go-review.googlesource.com/c/go/+/363441
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/lookup.go
src/cmd/compile/internal/types2/type.go
src/cmd/compile/internal/types2/unify.go
src/cmd/compile/internal/types2/universe.go

index bab90fbd9aa30611fedbbff7797f44ec67ecade5..d58fac5dbb514e25ac2705f8f348c166db6f8151 100644 (file)
@@ -727,7 +727,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 cf6c6c7111b5243278edfc3ad12cb95150626ffc..fbfe3c81ffa8a14421749683cef347e062531137 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, false, pkg, name)
                        if _, ok := obj.(*Func); ok {
                                return nil, nil, false
@@ -114,7 +114,7 @@ func lookupFieldOrMethod(T Type, addressable, checkFold bool, pkg *Package, name
 
                        // 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, checkFold bool, pkg *Package, name
                                seen[named] = true
 
                                // look for a matching attached method
+                               named.resolve(nil)
                                if i, m := lookupMethodFold(named.methods, pkg, name, checkFold); m != nil {
                                        // potential match
                                        // caution: method may not have a proper signature yet
index 77dc7db8964605c12b3a583f4a1cbb5839f658e4..3fea8d1776558cdc372975b14b001a3c089e00bd 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 7f636c30d3f9a5c598075865a058721bbfa9ae75..ccb6ee870935a506d9db767ac8905cdef33fd251 100644 (file)
@@ -235,14 +235,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 92fa32524c03d158681d465d28a6cb929406c00f..fccab145f853f55e0cd5257c60f20d3216130c99 100644 (file)
@@ -240,7 +240,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