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 <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2022-02-24 13:10:34 -08:00
parent 7751883379
commit f2cdc6d167
20 changed files with 68 additions and 44 deletions

View File

@ -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
}

View File

@ -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]

View File

@ -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)
}
}

View File

@ -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, "<function literal>", sig, e.Body, iota)
})
}).describef(e, "func literal")
}
x.mode = value
x.typ = sig

View File

@ -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))

View File

@ -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).

View File

@ -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), "--- <end>")
}()
check.trace(body.Pos(), "-- %s: %s", name, sig)
}
// set function scope extent

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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]

View File

@ -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)
}
}

View File

@ -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, "<function literal>", sig, e.Body, iota)
})
}).describef(e, "func literal")
}
x.mode = value
x.typ = sig

View File

@ -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))

View File

@ -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).

View File

@ -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(), "--- <end>")
}()
check.trace(body.Pos(), "-- %s: %s", name, sig)
}
// set function scope extent

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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
}