From 7703db647c0f36566d3ef28ea15d196db5ad3ff2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 3 Oct 2024 12:29:40 -0700 Subject: [PATCH] go/types, types2: add additional tracing output in type inference To support analyzing type inference failures. Change-Id: I47882f6b634d5fcf1834923ec372313323d31a9d Reviewed-on: https://go-review.googlesource.com/c/go/+/617895 LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Griesemer Reviewed-by: Tim King Auto-Submit: Robert Griesemer --- src/cmd/compile/internal/types2/infer.go | 9 +++++++++ src/go/types/infer.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/cmd/compile/internal/types2/infer.go b/src/cmd/compile/internal/types2/infer.go index 45ff6233ca..025b64d9fc 100644 --- a/src/cmd/compile/internal/types2/infer.go +++ b/src/cmd/compile/internal/types2/infer.go @@ -242,6 +242,9 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, // A type parameter can be unified with its core type in two cases. switch { case tx != nil: + if traceInference { + u.tracef("-> unify type parameter %s (type %s) with constraint core type %s", tpar, tx, core.typ) + } // The corresponding type argument tx is known. There are 2 cases: // 1) If the core type has a tilde, per spec requirement for tilde // elements, the core type is an underlying (literal) type. @@ -260,6 +263,9 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, return nil } case single && !core.tilde: + if traceInference { + u.tracef("-> set type parameter %s to constraint core type %s", tpar, core.typ) + } // The corresponding type argument tx is unknown and there's a single // specific type and no tilde. // In this case the type argument must be that single type; set it. @@ -267,6 +273,9 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, } } else { if tx != nil { + if traceInference { + u.tracef("-> unify type parameter %s (type %s) methods with constraint methods", tpar, tx) + } // We don't have a core type, but the type argument tx is known. // It must have (at least) all the methods of the type constraint, // and the method signatures must unify; otherwise tx cannot satisfy diff --git a/src/go/types/infer.go b/src/go/types/infer.go index 9a64bd0a71..b0094a6991 100644 --- a/src/go/types/infer.go +++ b/src/go/types/infer.go @@ -245,6 +245,9 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, // A type parameter can be unified with its core type in two cases. switch { case tx != nil: + if traceInference { + u.tracef("-> unify type parameter %s (type %s) with constraint core type %s", tpar, tx, core.typ) + } // The corresponding type argument tx is known. There are 2 cases: // 1) If the core type has a tilde, per spec requirement for tilde // elements, the core type is an underlying (literal) type. @@ -263,6 +266,9 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, return nil } case single && !core.tilde: + if traceInference { + u.tracef("-> set type parameter %s to constraint core type %s", tpar, core.typ) + } // The corresponding type argument tx is unknown and there's a single // specific type and no tilde. // In this case the type argument must be that single type; set it. @@ -270,6 +276,9 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, } } else { if tx != nil { + if traceInference { + u.tracef("-> unify type parameter %s (type %s) methods with constraint methods", tpar, tx) + } // We don't have a core type, but the type argument tx is known. // It must have (at least) all the methods of the type constraint, // and the method signatures must unify; otherwise tx cannot satisfy -- 2.48.1