]> Cypherpunks.ru repositories - gostls13.git/commitdiff
Revert "go/types, types2: remove internal constant enableInterfaceInference"
authorRobert Griesemer <gri@golang.org>
Wed, 16 Aug 2023 18:17:01 +0000 (11:17 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 17 Aug 2023 23:32:40 +0000 (23:32 +0000)
This reverts CL 514715.

This will make it easier to make interface inference conditional
based on the current language version.

For #61903.

Change-Id: I07820c861d6ebfd04899e41eb4123f26af2da1ee
Reviewed-on: https://go-review.googlesource.com/c/go/+/520195
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/unify.go
src/go/types/unify.go

index 6a130d55e0883f974e6f1ccef452d9c4e2aae5f6..be5796bc32fb0a6698de996ddc6856c3742758f9 100644 (file)
@@ -53,6 +53,11 @@ 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
@@ -334,7 +339,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) && !IsInterface(x) {
+       if ny, _ := y.(*Named); mode&exact == 0 && ny != nil && isTypeLit(x) && !(enableInterfaceInference && IsInterface(x)) {
                if traceInference {
                        u.tracef("%s ≡ under %s", x, ny)
                }
@@ -425,12 +430,12 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // x != y if we get here
        assert(x != y)
 
-       // If we don't require exact unification and both types are interfaces,
-       // one interface must have a subset of the methods of the other and
-       // corresponding method signatures must unify.
+       // If 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 mode&exact == 0 {
+       if 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)
@@ -627,7 +632,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                }
 
        case *Interface:
-               assert(mode&exact != 0) // inexact unification is handled before this switch
+               assert(!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.
index 20381215c54e7fd8ca2fe968c5d13851f52853cb..6d4a69e522a932592e29d821980f478951c96717 100644 (file)
@@ -55,6 +55,11 @@ 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
@@ -336,7 +341,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) && !IsInterface(x) {
+       if ny, _ := y.(*Named); mode&exact == 0 && ny != nil && isTypeLit(x) && !(enableInterfaceInference && IsInterface(x)) {
                if traceInference {
                        u.tracef("%s ≡ under %s", x, ny)
                }
@@ -427,12 +432,12 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // x != y if we get here
        assert(x != y)
 
-       // If we don't require exact unification and both types are interfaces,
-       // one interface must have a subset of the methods of the other and
-       // corresponding method signatures must unify.
+       // If 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 mode&exact == 0 {
+       if 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)
@@ -629,7 +634,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
                }
 
        case *Interface:
-               assert(mode&exact != 0) // inexact unification is handled before this switch
+               assert(!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.