]> Cypherpunks.ru repositories - gostls13.git/commitdiff
go/types, types2: more readable inference trace
authorRobert Griesemer <gri@golang.org>
Thu, 22 Jun 2023 23:07:29 +0000 (16:07 -0700)
committerGopher Robot <gobot@golang.org>
Mon, 26 Jun 2023 15:43:55 +0000 (15:43 +0000)
Print the unification mode in human-readable form.
Use a tab and // instead of ()'s to show unification mode
and whether operands where swapped.

These changes only affect inference trace output, which is
disabled by default. For easier debugging.

For #60933.

Change-Id: I95299c6e09b90670fc45addc4f9196b6cdd3b59f
Reviewed-on: https://go-review.googlesource.com/c/go/+/505395
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/unify.go
src/go/types/unify.go

index 48adc185c379232b979bc6e4775346c64af6a6d3..3e2b299e49266f6a2f705c01a4432e8589c8a15d 100644 (file)
@@ -124,6 +124,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.
@@ -263,7 +277,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 {
@@ -294,7 +308,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // - type parameter recorded with u, make sure one is in x
        if _, ok := x.(*Named); ok || 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
        }
@@ -492,7 +506,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // TODO(gri) Factor out type parameter handling from the switch.
        if isTypeParam(y) {
                if traceInference {
-                       u.tracef("%s ≡ %s (swap)", y, x)
+                       u.tracef("%s ≡ %s\t// swap", y, x)
                }
                x, y = y, x
        }
index 3ecc80f1616281b7e11dca24fe58b67237e43a97..9c40394c59fc719c14016f8959619bc916d6e91d 100644 (file)
@@ -126,6 +126,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.
@@ -265,7 +279,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 {
@@ -296,7 +310,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // - type parameter recorded with u, make sure one is in x
        if _, ok := x.(*Named); ok || 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
        }
@@ -494,7 +508,7 @@ func (u *unifier) nify(x, y Type, mode unifyMode, p *ifacePair) (result bool) {
        // TODO(gri) Factor out type parameter handling from the switch.
        if isTypeParam(y) {
                if traceInference {
-                       u.tracef("%s ≡ %s (swap)", y, x)
+                       u.tracef("%s ≡ %s\t// swap", y, x)
                }
                x, y = y, x
        }