From: Robert Griesemer Date: Thu, 24 Feb 2022 21:10:34 +0000 (-0800) Subject: go/types, types2: improved tracing output throughout (debugging support) X-Git-Tag: go1.19beta1~978 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f2cdc6d1672fb335ac56f9c7b824071f1e5ba545;p=gostls13.git go/types, types2: improved tracing output throughout (debugging support) This change fine-tunes tracing output and adds additional descriptions for delayed actions that were missing tracing. Change-Id: Ib5e70e8f40ef564194cdb0e8d12c38e15388b987 Reviewed-on: https://go-review.googlesource.com/c/go/+/387919 Trust: Robert Griesemer Reviewed-by: Robert Findley --- diff --git a/src/cmd/compile/internal/types2/call.go b/src/cmd/compile/internal/types2/call.go index 6cc30a7015..3ade147dfe 100644 --- a/src/cmd/compile/internal/types2/call.go +++ b/src/cmd/compile/internal/types2/call.go @@ -64,7 +64,7 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs assert(len(targs) == typ.TypeParams().Len()) if check.conf.Trace { - check.trace(pos, "-- instantiating %s with %s", typ, targs) + check.trace(pos, "-- instantiating signature %s with %s", typ, targs) check.indent++ defer func() { check.indent-- @@ -88,7 +88,7 @@ func (check *Checker) instantiateSignature(pos syntax.Pos, typ *Signature, targs } else { check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) } - }) + }).describef(pos, "verify instantiation") return inst } diff --git a/src/cmd/compile/internal/types2/check.go b/src/cmd/compile/internal/types2/check.go index 4ec6a7b4fd..5cf8454aa4 100644 --- a/src/cmd/compile/internal/types2/check.go +++ b/src/cmd/compile/internal/types2/check.go @@ -373,11 +373,17 @@ func (check *Checker) processDelayed(top int) { // this is a sufficiently bounded process. for i := top; i < len(check.delayed); i++ { a := &check.delayed[i] - if check.conf.Trace && a.desc != nil { - fmt.Println() - check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...) + if check.conf.Trace { + if a.desc != nil { + check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...) + } else { + check.trace(nopos, "-- delayed %p", a.f) + } } a.f() // may append to check.delayed + if check.conf.Trace { + fmt.Println() + } } assert(top <= len(check.delayed)) // stack must not have shrunk check.delayed = check.delayed[:top] diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 01c47ee7c1..95143cbed5 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -716,7 +716,7 @@ func (check *Checker) funcDecl(obj *Func, decl *declInfo) { if !check.conf.IgnoreFuncBodies && fdecl.Body != nil { check.later(func() { check.funcBody(decl, obj.name, sig, fdecl.Body, nil) - }) + }).describef(obj, "func %s", obj.name) } } diff --git a/src/cmd/compile/internal/types2/expr.go b/src/cmd/compile/internal/types2/expr.go index e59f0b74ac..7d2a7ba46b 100644 --- a/src/cmd/compile/internal/types2/expr.go +++ b/src/cmd/compile/internal/types2/expr.go @@ -1214,7 +1214,7 @@ const ( // func (check *Checker) rawExpr(x *operand, e syntax.Expr, hint Type, allowGeneric bool) exprKind { if check.conf.Trace { - check.trace(e.Pos(), "expr %s", e) + check.trace(e.Pos(), "-- expr %s", e) check.indent++ defer func() { check.indent-- @@ -1328,7 +1328,7 @@ func (check *Checker) exprInternal(x *operand, e syntax.Expr, hint Type) exprKin // but before the enclosing scope contents changes (#22992). check.later(func() { check.funcBody(decl, "", sig, e.Body, iota) - }) + }).describef(e, "func literal") } x.mode = value x.typ = sig diff --git a/src/cmd/compile/internal/types2/instantiate.go b/src/cmd/compile/internal/types2/instantiate.go index 9eced489dc..a511538ccc 100644 --- a/src/cmd/compile/internal/types2/instantiate.go +++ b/src/cmd/compile/internal/types2/instantiate.go @@ -61,7 +61,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e // instance creates a type or function instance using the given original type // typ and arguments targs. For Named types the resulting instance will be -// unexpanded. +// unexpanded. check may be nil. func (check *Checker) instance(pos syntax.Pos, orig Type, targs []Type, ctxt *Context) (res Type) { var h string if ctxt != nil { @@ -103,6 +103,7 @@ func (check *Checker) instance(pos syntax.Pos, orig Type, targs []Type, ctxt *Co // anymore; we need to set tparams to nil. sig.tparams = nil res = sig + default: // only types and functions can be generic panic(fmt.Sprintf("%v: cannot instantiate %v", pos, orig)) diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go index daf8fdc986..8dd9fb6bc4 100644 --- a/src/cmd/compile/internal/types2/named.go +++ b/src/cmd/compile/internal/types2/named.go @@ -354,11 +354,19 @@ func (check *Checker) bestContext(ctxt *Context) *Context { // expandNamed ensures that the underlying type of n is instantiated. // The underlying type will be Typ[Invalid] if there was an error. func expandNamed(ctxt *Context, n *Named, instPos syntax.Pos) (tparams *TypeParamList, underlying Type, methods *methodList) { + check := n.check + if check != nil && check.conf.Trace { + check.trace(instPos, "-- expandNamed %s", n) + check.indent++ + defer func() { + check.indent-- + check.trace(instPos, "=> %s (tparams = %s, under = %s)", n, tparams.list(), underlying) + }() + } + n.orig.resolve(ctxt) assert(n.orig.underlying != nil) - check := n.check - if _, unexpanded := n.orig.underlying.(*Named); unexpanded { // We should only get an unexpanded underlying here during type checking // (for example, in recursive type declarations). diff --git a/src/cmd/compile/internal/types2/stmt.go b/src/cmd/compile/internal/types2/stmt.go index 819b7c2463..2b6abbde7e 100644 --- a/src/cmd/compile/internal/types2/stmt.go +++ b/src/cmd/compile/internal/types2/stmt.go @@ -18,10 +18,7 @@ func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body } if check.conf.Trace { - check.trace(body.Pos(), "--- %s: %s", name, sig) - defer func() { - check.trace(syntax.EndPos(body), "--- ") - }() + check.trace(body.Pos(), "-- %s: %s", name, sig) } // set function scope extent diff --git a/src/cmd/compile/internal/types2/typeset.go b/src/cmd/compile/internal/types2/typeset.go index 646b436685..328c5029e7 100644 --- a/src/cmd/compile/internal/types2/typeset.go +++ b/src/cmd/compile/internal/types2/typeset.go @@ -173,7 +173,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ pos = ityp.methods[0].pos } - check.trace(pos, "type set for %s", ityp) + check.trace(pos, "-- type set for %s", ityp) check.indent++ defer func() { check.indent-- @@ -248,7 +248,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_ err.errorf(mpos[other.(*Func)], "other declaration of %s", m.name) check.report(&err) } - }) + }).describef(pos, "duplicate method check for %s", m.name) } } diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index 40333fd77f..afbea06032 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -174,7 +174,7 @@ func (check *Checker) validVarType(e syntax.Expr, typ Type) { } } } - }) + }).describef(e, "check var type %s", typ) } // definedType is like typ but also accepts a type name def. @@ -372,7 +372,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) { } check.errorf(e.Key, "invalid map key type %s%s", typ.key, why) } - }) + }).describef(e.Key, "check map key %s", typ.key) return typ @@ -409,7 +409,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) { func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def *Named) (res Type) { if check.conf.Trace { - check.trace(x.Pos(), "-- instantiating %s with %s", x, xlist) + check.trace(x.Pos(), "-- instantiating type %s with %s", x, xlist) check.indent++ defer func() { check.indent-- @@ -498,7 +498,7 @@ func (check *Checker) instantiatedType(x syntax.Expr, xlist []syntax.Expr, def * } check.validType(inst) - }) + }).describef(x, "resolve instance %s", inst) return inst } diff --git a/src/cmd/compile/internal/types2/union.go b/src/cmd/compile/internal/types2/union.go index e317b9cced..132e73098a 100644 --- a/src/cmd/compile/internal/types2/union.go +++ b/src/cmd/compile/internal/types2/union.go @@ -129,7 +129,7 @@ func parseUnion(check *Checker, uexpr syntax.Expr) Type { check.softErrorf(tlist[i], "overlapping terms %s and %s", t, terms[j]) } } - }) + }).describef(uexpr, "check term validity %s", uexpr) return u } diff --git a/src/go/types/call.go b/src/go/types/call.go index 5d1f60d432..51603170a6 100644 --- a/src/go/types/call.go +++ b/src/go/types/call.go @@ -65,7 +65,7 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs assert(len(targs) == typ.TypeParams().Len()) if trace { - check.trace(pos, "-- instantiating %s with %s", typ, targs) + check.trace(pos, "-- instantiating signature %s with %s", typ, targs) check.indent++ defer func() { check.indent-- @@ -89,7 +89,7 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs } else { check.mono.recordInstance(check.pkg, pos, tparams, targs, xlist) } - }) + }).describef(atPos(pos), "verify instantiation") return inst } diff --git a/src/go/types/check.go b/src/go/types/check.go index 23136377c8..d920d9c080 100644 --- a/src/go/types/check.go +++ b/src/go/types/check.go @@ -381,11 +381,17 @@ func (check *Checker) processDelayed(top int) { // this is a sufficiently bounded process. for i := top; i < len(check.delayed); i++ { a := &check.delayed[i] - if trace && a.desc != nil { - fmt.Println() - check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...) + if trace { + if a.desc != nil { + check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...) + } else { + check.trace(token.NoPos, "-- delayed %p", a.f) + } } a.f() // may append to check.delayed + if trace { + fmt.Println() + } } assert(top <= len(check.delayed)) // stack must not have shrunk check.delayed = check.delayed[:top] diff --git a/src/go/types/decl.go b/src/go/types/decl.go index c3d43d93f6..a20b56c950 100644 --- a/src/go/types/decl.go +++ b/src/go/types/decl.go @@ -786,7 +786,7 @@ func (check *Checker) funcDecl(obj *Func, decl *declInfo) { if !check.conf.IgnoreFuncBodies && fdecl.Body != nil { check.later(func() { check.funcBody(decl, obj.name, sig, fdecl.Body, nil) - }) + }).describef(obj, "func %s", obj.name) } } diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 596bcef9c1..160dcc35d0 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -1197,7 +1197,7 @@ const ( // func (check *Checker) rawExpr(x *operand, e ast.Expr, hint Type, allowGeneric bool) exprKind { if trace { - check.trace(e.Pos(), "expr %s", e) + check.trace(e.Pos(), "-- expr %s", e) check.indent++ defer func() { check.indent-- @@ -1305,7 +1305,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { // but before the enclosing scope contents changes (#22992). check.later(func() { check.funcBody(decl, "", sig, e.Body, iota) - }) + }).describef(e, "func literal") } x.mode = value x.typ = sig diff --git a/src/go/types/instantiate.go b/src/go/types/instantiate.go index a481746657..d7045ff23c 100644 --- a/src/go/types/instantiate.go +++ b/src/go/types/instantiate.go @@ -61,7 +61,7 @@ func Instantiate(ctxt *Context, orig Type, targs []Type, validate bool) (Type, e // instance creates a type or function instance using the given original type // typ and arguments targs. For Named types the resulting instance will be -// unexpanded. +// unexpanded. check may be nil. func (check *Checker) instance(pos token.Pos, orig Type, targs []Type, ctxt *Context) (res Type) { var h string if ctxt != nil { @@ -103,6 +103,7 @@ func (check *Checker) instance(pos token.Pos, orig Type, targs []Type, ctxt *Con // anymore; we need to set tparams to nil. sig.tparams = nil res = sig + default: // only types and functions can be generic panic(fmt.Sprintf("%v: cannot instantiate %v", pos, orig)) diff --git a/src/go/types/named.go b/src/go/types/named.go index 876f7e8551..b8760efc5e 100644 --- a/src/go/types/named.go +++ b/src/go/types/named.go @@ -356,11 +356,19 @@ func (check *Checker) bestContext(ctxt *Context) *Context { // expandNamed ensures that the underlying type of n is instantiated. // The underlying type will be Typ[Invalid] if there was an error. func expandNamed(ctxt *Context, n *Named, instPos token.Pos) (tparams *TypeParamList, underlying Type, methods *methodList) { + check := n.check + if check != nil && trace { + check.trace(instPos, "-- expandNamed %s", n) + check.indent++ + defer func() { + check.indent-- + check.trace(instPos, "=> %s (tparams = %s, under = %s)", n, tparams.list(), underlying) + }() + } + n.orig.resolve(ctxt) assert(n.orig.underlying != nil) - check := n.check - if _, unexpanded := n.orig.underlying.(*Named); unexpanded { // We should only get an unexpanded underlying here during type checking // (for example, in recursive type declarations). diff --git a/src/go/types/stmt.go b/src/go/types/stmt.go index 2aa65a6e36..d7f6a486ca 100644 --- a/src/go/types/stmt.go +++ b/src/go/types/stmt.go @@ -19,10 +19,7 @@ func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body } if trace { - check.trace(body.Pos(), "--- %s: %s", name, sig) - defer func() { - check.trace(body.End(), "--- ") - }() + check.trace(body.Pos(), "-- %s: %s", name, sig) } // set function scope extent diff --git a/src/go/types/typeset.go b/src/go/types/typeset.go index b33141ec32..08ff191f2e 100644 --- a/src/go/types/typeset.go +++ b/src/go/types/typeset.go @@ -177,7 +177,7 @@ func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_T pos = ityp.methods[0].pos } - check.trace(pos, "type set for %s", ityp) + check.trace(pos, "-- type set for %s", ityp) check.indent++ defer func() { check.indent-- @@ -248,7 +248,7 @@ func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_T check.errorf(atPos(pos), _DuplicateDecl, "duplicate method %s", m.name) check.errorf(atPos(mpos[other.(*Func)]), _DuplicateDecl, "\tother declaration of %s", m.name) // secondary error, \t indented } - }) + }).describef(atPos(pos), "duplicate method check for %s", m.name) } } diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index d72b48185a..bae9dc816c 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -170,7 +170,7 @@ func (check *Checker) validVarType(e ast.Expr, typ Type) { } } } - }) + }).describef(e, "check var type %s", typ) } // definedType is like typ but also accepts a type name def. @@ -353,7 +353,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) { } check.errorf(e.Key, _IncomparableMapKey, "incomparable map key type %s%s", typ.key, why) } - }) + }).describef(e.Key, "check map key %s", typ.key) return typ @@ -390,7 +390,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) { func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (res Type) { pos := ix.X.Pos() if trace { - check.trace(pos, "-- instantiating %s with %s", ix.X, ix.Indices) + check.trace(pos, "-- instantiating type %s with %s", ix.X, ix.Indices) check.indent++ defer func() { check.indent-- @@ -483,7 +483,7 @@ func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (re } check.validType(inst) - }) + }).describef(ix, "resolve instance %s", inst) return inst } diff --git a/src/go/types/union.go b/src/go/types/union.go index 8397d65af0..1a8825fcab 100644 --- a/src/go/types/union.go +++ b/src/go/types/union.go @@ -132,7 +132,7 @@ func parseUnion(check *Checker, uexpr ast.Expr) Type { check.softErrorf(tlist[i], _InvalidUnion, "overlapping terms %s and %s", t, terms[j]) } } - }) + }).describef(uexpr, "check term validity %s", uexpr) return u }