]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/unify.go
go/types, types2: implement Alias proposal (export API)
[gostls13.git] / src / cmd / compile / internal / types2 / unify.go
index 3f54a2c2f2267f4bb723bdeae8d6e11fa9e1c748..8218939b6834771987ef77e2eaa5c7e82c662f66 100644 (file)
@@ -53,11 +53,6 @@ const (
        // the core types, if any, of non-local (unbound) type parameters.
        enableCoreTypeUnification = true
 
-       // If enableInterfaceInference is set, type inference uses
-       // shared methods for improved type inference involving
-       // interfaces.
-       enableInterfaceInference = true
-
        // If traceInference is set, unification will print a trace of its operation.
        // Interpretation of trace:
        //   x ≡ y    attempt to unify types x and y
@@ -81,15 +76,16 @@ type unifier struct {
        // that inferring the type for a given type parameter P will
        // automatically infer the same type for all other parameters
        // unified (joined) with P.
-       handles map[*TypeParam]*Type
-       depth   int // recursion depth during unification
+       handles                  map[*TypeParam]*Type
+       depth                    int  // recursion depth during unification
+       enableInterfaceInference bool // use shared methods for better inference
 }
 
 // newUnifier returns a new unifier initialized with the given type parameter
 // and corresponding type argument lists. The type argument list may be shorter
 // than the type parameter list, and it may contain nil types. Matching type
 // parameters and arguments must have the same index.
-func newUnifier(tparams []*TypeParam, targs []Type) *unifier {
+func newUnifier(tparams []*TypeParam, targs []Type, enableInterfaceInference bool) *unifier {
        assert(len(tparams) >= len(targs))
        handles := make(map[*TypeParam]*Type, len(tparams))
        // Allocate all handles up-front: in a correct program, all type parameters
@@ -103,7 +99,7 @@ func newUnifier(tparams []*TypeParam, targs []Type) *unifier {
                }
                handles[x] = &t
        }
-       return &unifier{handles, 0}
+       return &unifier{handles, 0, enableInterfaceInference}
 }
 
 // unifyMode controls the behavior of the unifier.
@@ -124,6 +120,20 @@ const (
        exact
 )
 
+func (m unifyMode) String() string {
+       switch m {
+       case 0:
+               return "inexact"
+       case assign:
+               return "assign"
+       case exact:
+               return "exact"
+       case assign | exact:
+               return "assign, exact"
+       }
+       return fmt.Sprintf("mode %d", m)
+}
+
 // unify attempts to unify x and y and reports whether it succeeded.
 // As a side-effect, types may be inferred for type parameters.
 // The mode parameter controls how types are compared.
@@ -256,6 +266,15 @@ func (u *unifier) inferred(tparams []*TypeParam) []Type {
        return list
 }
 
+// asInterface returns the underlying type of x as an interface if
+// it is a non-type parameter interface. Otherwise it returns nil.
+func asInterface(x Type) (i *Interface) {
+       if _, ok := x.(*TypeParam); !ok {
+               i, _ = under(x).(*Interface)
+       }
+       return i
+}
+
 // nify implements the core unification algorithm which is an
 // adapted version of Checker.identical. For changes to that
 // code the corresponding changes should be made here.
@@ -263,7 +282,7 @@ func (u *unifier) inferred(tparams []*TypeParam) []Type {
 func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        u.depth++
        if traceInference {
-               u.tracef("%s ≡ %s (mode %d)", x, y, mode)
+               u.tracef("%s ≡ %s\t// %s", x, y, mode)
        }
        defer func() {
                if traceInference && !result {
@@ -272,6 +291,9 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                u.depth--
        }()
 
+       x = Unalias(x)
+       y = Unalias(y)
+
        // nothing to do if x == y
        if x == y {
                return true
@@ -292,9 +314,9 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // Ensure that if we have at least one
        // - defined type, make sure one is in y
        // - type parameter recorded with u, make sure one is in x
-       if _, ok := x.(*Named); ok || u.asTypeParam(y) != nil {
+       if asNamed(x) != nil || u.asTypeParam(y) != nil {
                if traceInference {
-                       u.tracef("%s ≡ %s (swap)", y, x)
+                       u.tracef("%s ≡ %s\t// swap", y, x)
                }
                x, y = y, x
        }
@@ -316,7 +338,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // we will fail at function instantiation or argument assignment time.
        //
        // If we have at least one defined type, there is one in y.
-       if ny, _ := y.(*Named); mode&exact == 0 && ny != nil && isTypeLit(x) && !(enableInterfaceInference && IsInterface(x)) {
+       if ny := asNamed(y); mode&exact == 0 && ny != nil && isTypeLit(x) && !(u.enableInterfaceInference && IsInterface(x)) {
                if traceInference {
                        u.tracef("%s ≡ under %s", x, ny)
                }
@@ -350,12 +372,72 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                if x := u.at(px); x != nil {
                        // x has an inferred type which must match y
                        if u.nify(x, y, mode, p) {
-                               // If we have a match, possibly through underlying types,
-                               // and y is a defined type, make sure we record that type
-                               // for type parameter x, which may have until now only
-                               // recorded an underlying type (go.dev/issue/43056).
-                               if _, ok := y.(*Named); ok {
-                                       u.set(px, y)
+                               // We have a match, possibly through underlying types.
+                               xi := asInterface(x)
+                               yi := asInterface(y)
+                               xn := asNamed(x) != nil
+                               yn := asNamed(y) != nil
+                               // If we have two interfaces, what to do depends on
+                               // whether they are named and their method sets.
+                               if xi != nil && yi != nil {
+                                       // Both types are interfaces.
+                                       // If both types are defined types, they must be identical
+                                       // because unification doesn't know which type has the "right" name.
+                                       if xn && yn {
+                                               return Identical(x, y)
+                                       }
+                                       // In all other cases, the method sets must match.
+                                       // The types unified so we know that corresponding methods
+                                       // match and we can simply compare the number of methods.
+                                       // TODO(gri) We may be able to relax this rule and select
+                                       // the more general interface. But if one of them is a defined
+                                       // type, it's not clear how to choose and whether we introduce
+                                       // an order dependency or not. Requiring the same method set
+                                       // is conservative.
+                                       if len(xi.typeSet().methods) != len(yi.typeSet().methods) {
+                                               return false
+                                       }
+                               } else if xi != nil || yi != nil {
+                                       // One but not both of them are interfaces.
+                                       // In this case, either x or y could be viable matches for the corresponding
+                                       // type parameter, which means choosing either introduces an order dependence.
+                                       // Therefore, we must fail unification (go.dev/issue/60933).
+                                       return false
+                               }
+                               // If we have inexact unification and one of x or y is a defined type, select the
+                               // defined type. This ensures that in a series of types, all matching against the
+                               // same type parameter, we infer a defined type if there is one, independent of
+                               // order. Type inference or assignment may fail, which is ok.
+                               // Selecting a defined type, if any, ensures that we don't lose the type name;
+                               // and since we have inexact unification, a value of equally named or matching
+                               // undefined type remains assignable (go.dev/issue/43056).
+                               //
+                               // Similarly, if we have inexact unification and there are no defined types but
+                               // channel types, select a directed channel, if any. This ensures that in a series
+                               // of unnamed types, all matching against the same type parameter, we infer the
+                               // directed channel if there is one, independent of order.
+                               // Selecting a directional channel, if any, ensures that a value of another
+                               // inexactly unifying channel type remains assignable (go.dev/issue/62157).
+                               //
+                               // If we have multiple defined channel types, they are either identical or we
+                               // have assignment conflicts, so we can ignore directionality in this case.
+                               //
+                               // If we have defined and literal channel types, a defined type wins to avoid
+                               // order dependencies.
+                               if mode&exact == 0 {
+                                       switch {
+                                       case xn:
+                                               // x is a defined type: nothing to do.
+                                       case yn:
+                                               // x is not a defined type and y is a defined type: select y.
+                                               u.set(px, y)
+                                       default:
+                                               // Neither x nor y are defined types.
+                                               if yc, _ := under(y).(*Chan); yc != nil && yc.dir != SendRecv {
+                                                       // y is a directed channel type: select y.
+                                                       u.set(px, y)
+                                               }
+                                       }
                                }
                                return true
                        }
@@ -369,32 +451,16 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // x != y if we get here
        assert(x != y)
 
-       // If we get here and x or y is a type parameter, they are unbound
-       // (not recorded with the unifier).
-       // Ensure that if we have at least one type parameter, it is in x
-       // (the earlier swap checks for _recorded_ type parameters only).
-       if isTypeParam(y) {
-               if traceInference {
-                       u.tracef("%s ≡ %s (swap)", y, x)
-               }
-               x, y = y, x
-       }
-
-       // Type elements (array, slice, etc. elements) use emode for unification.
-       // Element types must match exactly if the types are used in an assignment.
-       emode := mode
-       if mode&assign != 0 {
-               emode |= exact
-       }
-
-       // If EnableInterfaceInference is set and both types are interfaces, one
-       // interface must have a subset of the methods of the other and corresponding
-       // method signatures must unify.
+       // If u.EnableInterfaceInference is set and we don't require exact unification,
+       // if both types are interfaces, one interface must have a subset of the
+       // methods of the other and corresponding method signatures must unify.
        // If only one type is an interface, all its methods must be present in the
        // other type and corresponding method signatures must unify.
-       if enableInterfaceInference {
-               xi, _ := x.(*Interface)
-               yi, _ := y.(*Interface)
+       if u.enableInterfaceInference && mode&exact == 0 {
+               // One or both interfaces may be defined types.
+               // Look under the name, but not under type parameters (go.dev/issue/60564).
+               xi := asInterface(x)
+               yi := asInterface(y)
                // If we have two interfaces, check the type terms for equivalence,
                // and unify common methods if possible.
                if xi != nil && yi != nil {
@@ -453,7 +519,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        }
                        // All xmethods must exist in ymethods and corresponding signatures must unify.
                        for _, xm := range xmethods {
-                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym := ymap[xm.Id()]; ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
@@ -474,15 +540,37 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                        xmethods := xi.typeSet().methods
                        for _, xm := range xmethods {
                                obj, _, _ := LookupFieldOrMethod(y, false, xm.pkg, xm.name)
-                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, emode, p) {
+                               if ym, _ := obj.(*Func); ym == nil || !u.nify(xm.typ, ym.typ, exact, p) {
                                        return false
                                }
                        }
                        return true
                }
+       }
+
+       // Unless we have exact unification, neither x nor y are interfaces now.
+       // Except for unbound type parameters (see below), x and y must be structurally
+       // equivalent to unify.
+
+       // If we get here and x or y is a type parameter, they are unbound
+       // (not recorded with the unifier).
+       // Ensure that if we have at least one type parameter, it is in x
+       // (the earlier swap checks for _recorded_ type parameters only).
+       // This ensures that the switch switches on the type parameter.
+       //
+       // TODO(gri) Factor out type parameter handling from the switch.
+       if isTypeParam(y) {
+               if traceInference {
+                       u.tracef("%s ≡ %s\t// swap", y, x)
+               }
+               x, y = y, x
+       }
 
-               // Neither x nor y are interface types.
-               // They must be structurally equivalent to unify.
+       // Type elements (array, slice, etc. elements) use emode for unification.
+       // Element types must match exactly if the types are used in an assignment.
+       emode := mode
+       if mode&assign != 0 {
+               emode |= exact
        }
 
        switch x := x.(type) {
@@ -565,7 +653,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                }
 
        case *Interface:
-               assert(!enableInterfaceInference) // handled before this switch
+               assert(!u.enableInterfaceInference || mode&exact != 0) // handled before this switch
 
                // Two interface types unify if they have the same set of methods with
                // the same names, and corresponding function types unify.
@@ -618,7 +706,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                }
                                for i, f := range a {
                                        g := b[i]
-                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, emode, q) {
+                                       if f.Id() != g.Id() || !u.nify(f.typ, g.typ, exact, q) {
                                                return false
                                        }
                                }
@@ -641,27 +729,9 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                }
 
        case *Named:
-               // Two named non-interface types unify if their type names originate
-               // in the same type declaration. If they are instantiated, their type
-               // argument lists must unify.
-               // If one or both named types are interfaces, the types unify if the
-               // respective methods unify (per the rules for interface unification).
-               if y, ok := y.(*Named); ok {
-                       if enableInterfaceInference && mode&exact == 0 {
-                               xi, _ := x.under().(*Interface)
-                               yi, _ := y.under().(*Interface)
-                               // If one or both of x and y are interfaces, use interface unification.
-                               switch {
-                               case xi != nil && yi != nil:
-                                       return u.nify(xi, yi, mode, p)
-                               case xi != nil:
-                                       return u.nify(xi, y, mode, p)
-                               case yi != nil:
-                                       return u.nify(x, yi, mode, p)
-                               }
-                               // In all other cases, the type arguments and origins must match.
-                       }
-
+               // Two named types unify if their type names originate in the same type declaration.
+               // If they are instantiated, their type argument lists must unify.
+               if y := asNamed(y); y != nil {
                        // Check type arguments before origins so they unify
                        // even if the origins don't match; for better error
                        // messages (see go.dev/issue/53692).
@@ -675,7 +745,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                        return false
                                }
                        }
-                       return indenticalOrigin(x, y)
+                       return identicalOrigin(x, y)
                }
 
        case *TypeParam:
@@ -706,7 +776,11 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                                if traceInference {
                                        u.tracef("core %s ≡ %s", x, y)
                                }
-                               return u.nify(cx, y, mode, p)
+                               // If y is a defined type, it may not match against cx which
+                               // is an underlying type (incl. int, string, etc.). Use assign
+                               // mode here so that the unifier automatically takes under(y)
+                               // if necessary.
+                               return u.nify(cx, y, assign, p)
                        }
                }
                // x != y and there's nothing to do