]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/lookup.go
go/types, types2: introduce _Alias type node
[gostls13.git] / src / cmd / compile / internal / types2 / lookup.go
index c19a6571c36288e2d362715e611440bf5eed4fb0..b8926250cf2a4a98b8bbe91912256f55d69c284a 100644 (file)
@@ -8,6 +8,7 @@ package types2
 
 import (
        "bytes"
+       "cmd/compile/internal/syntax"
        "strings"
 )
 
@@ -53,7 +54,7 @@ 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 go.dev/issue/8590).
-       if t, _ := T.(*Named); t != nil {
+       if t := asNamed(T); t != nil {
                if p, _ := t.Underlying().(*Pointer); p != nil {
                        obj, index, indirect = lookupFieldOrMethodImpl(p, false, pkg, name, false)
                        if _, ok := obj.(*Func); ok {
@@ -137,7 +138,7 @@ func lookupFieldOrMethodImpl(T Type, addressable bool, pkg *Package, name string
 
                        // If we have a named type, we may have associated methods.
                        // Look for those first.
-                       if named, _ := typ.(*Named); named != nil {
+                       if named := asNamed(typ); named != nil {
                                if alt := seen.lookup(named); alt != nil {
                                        // We have seen this type before, at a more shallow depth
                                        // (note that multiples of this type at the current depth
@@ -343,13 +344,14 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
                notFound
                wrongName
                wrongSig
+               ambigSel
                ptrRecv
                field
        )
 
        state := ok
        var m *Func // method on T we're trying to implement
-       var f *Func // method on V, if found (state is one of ok, wrongName, wrongSig, ptrRecv)
+       var f *Func // method on V, if found (state is one of ok, wrongName, wrongSig)
 
        if u, _ := under(V).(*Interface); u != nil {
                tset := u.typeSet()
@@ -371,28 +373,22 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
                }
        } else {
                for _, m = range methods {
-                       obj, _, _ := lookupFieldOrMethodImpl(V, false, m.pkg, m.name, false)
+                       obj, index, indirect := lookupFieldOrMethodImpl(V, false, m.pkg, m.name, false)
 
-                       // check if m is on *V, or on V with case-folding
+                       // check if m is ambiguous, on *V, or on V with case-folding
                        if obj == nil {
-                               state = notFound
-                               // TODO(gri) Instead of NewPointer(V) below, can we just set the "addressable" argument?
-                               obj, _, _ = lookupFieldOrMethodImpl(NewPointer(V), false, m.pkg, m.name, false)
-                               if obj != nil {
-                                       f, _ = obj.(*Func)
-                                       if f != nil {
-                                               state = ptrRecv
-                                       }
-                                       // otherwise we found a field, keep state == notFound
-                                       break
-                               }
-                               obj, _, _ = lookupFieldOrMethodImpl(V, false, m.pkg, m.name, true /* fold case */)
-                               if obj != nil {
+                               switch {
+                               case index != nil:
+                                       state = ambigSel
+                               case indirect:
+                                       state = ptrRecv
+                               default:
+                                       state = notFound
+                                       obj, _, _ = lookupFieldOrMethodImpl(V, false, m.pkg, m.name, true /* fold case */)
                                        f, _ = obj.(*Func)
                                        if f != nil {
                                                state = wrongName
                                        }
-                                       // otherwise we found a (differently spelled) field, keep state == notFound
                                }
                                break
                        }
@@ -421,6 +417,13 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
        }
 
        if cause != nil {
+               if f != nil {
+                       // This method may be formatted in funcString below, so must have a fully
+                       // set up signature.
+                       if check != nil {
+                               check.objDecl(f, nil)
+                       }
+               }
                switch state {
                case notFound:
                        switch {
@@ -444,6 +447,8 @@ func (check *Checker) missingMethod(V, T Type, static bool, equivalent func(x, y
                        }
                        *cause = check.sprintf("(wrong type for method %s)\n\t\thave %s\n\t\twant %s",
                                m.Name(), fs, ms)
+               case ambigSel:
+                       *cause = check.sprintf("(ambiguous selector %s.%s)", V, m.Name())
                case ptrRecv:
                        *cause = check.sprintf("(method %s has pointer receiver)", m.Name())
                case field:
@@ -508,21 +513,21 @@ func (check *Checker) assertableTo(V, T Type, cause *string) bool {
 // in constraint position (we have not yet defined that behavior in the spec).
 // The underlying type of V must be an interface.
 // If the result is false and cause is not nil, *cause is set to the error cause.
-func (check *Checker) newAssertableTo(V, T Type, cause *string) bool {
+func (check *Checker) newAssertableTo(pos syntax.Pos, V, T Type, cause *string) bool {
        // 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 IsInterface(T) {
                return true
        }
-       return check.implements(T, V, false, cause)
+       return check.implements(pos, T, V, false, cause)
 }
 
 // deref dereferences typ if it is a *Pointer (but not a *Named type
 // with an underlying pointer type!) and returns its base and true.
 // Otherwise it returns (typ, false).
 func deref(typ Type) (Type, bool) {
-       if p, _ := typ.(*Pointer); p != nil {
+       if p, _ := _Unalias(typ).(*Pointer); p != nil {
                // p.base should never be nil, but be conservative
                if p.base == nil {
                        if debug {