Skip to content

Commit 75974fa

Browse files
griesemereric
authored andcommitted
go/types: make tracing configurable (matching types2)
This CL replaces the internal trace flag with Config.trace. While unexported, it can still be set for testing via reflection. The typical use is for manual tests, where -v (verbose) turns on tracing output. Typical use: go test -run Manual -v This change makes go/types match types2 behavior. Change-Id: I22842f4bba8fd632efe5929c950f4b1cab0a8569 Reviewed-on: https://go-review.googlesource.com/c/go/+/461081 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 40886a0 commit 75974fa

File tree

12 files changed

+25
-23
lines changed

12 files changed

+25
-23
lines changed

src/go/types/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ type Config struct {
143143
// It is an error to set both FakeImportC and go115UsesCgo.
144144
go115UsesCgo bool
145145

146+
// If trace is set, a debug trace is printed to stdout.
147+
trace bool
148+
146149
// If Error != nil, it is called with each error found
147150
// during type checking; err has dynamic type Error.
148151
// Secondary errors (for instance, to enumerate all types

src/go/types/call.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (check *Checker) instantiateSignature(pos token.Pos, typ *Signature, targs
6565
assert(check != nil)
6666
assert(len(targs) == typ.TypeParams().Len())
6767

68-
if trace {
68+
if check.conf.trace {
6969
check.trace(pos, "-- instantiating signature %s with %s", typ, targs)
7070
check.indent++
7171
defer func() {

src/go/types/check.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ import (
1616
)
1717

1818
// debugging/development support
19-
const (
20-
debug = false // leave on during development
21-
trace = false // turn on for detailed type resolution traces
22-
)
19+
const debug = false // leave on during development
2320

2421
// exprInfo stores information about an untyped expression.
2522
type exprInfo struct {
@@ -313,7 +310,7 @@ func (check *Checker) checkFiles(files []*ast.File) (err error) {
313310
defer check.handleBailout(&err)
314311

315312
print := func(msg string) {
316-
if trace {
313+
if check.conf.trace {
317314
fmt.Println()
318315
fmt.Println(msg)
319316
}
@@ -377,15 +374,15 @@ func (check *Checker) processDelayed(top int) {
377374
// this is a sufficiently bounded process.
378375
for i := top; i < len(check.delayed); i++ {
379376
a := &check.delayed[i]
380-
if trace {
377+
if check.conf.trace {
381378
if a.desc != nil {
382379
check.trace(a.desc.pos.Pos(), "-- "+a.desc.format, a.desc.args...)
383380
} else {
384381
check.trace(token.NoPos, "-- delayed %p", a.f)
385382
}
386383
}
387384
a.f() // may append to check.delayed
388-
if trace {
385+
if check.conf.trace {
389386
fmt.Println()
390387
}
391388
}

src/go/types/check_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
138138
flags := flag.NewFlagSet("", flag.PanicOnError)
139139
flags.StringVar(&conf.GoVersion, "lang", "", "")
140140
flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
141-
flags.BoolVar(addrOldComparableSemantics(&conf), "oldComparableSemantics", false, "")
141+
flags.BoolVar(boolFieldAddr(&conf, "oldComparableSemantics"), "oldComparableSemantics", false, "")
142142
if err := parseFlags(filenames[0], srcs[0], flags); err != nil {
143143
t.Fatal(err)
144144
}
@@ -159,6 +159,7 @@ func testFiles(t *testing.T, sizes Sizes, filenames []string, srcs [][]byte, man
159159
}
160160

161161
// typecheck and collect typechecker errors
162+
*boolFieldAddr(&conf, "trace") = manual && testing.Verbose()
162163
if imp == nil {
163164
imp = importer.Default()
164165
}
@@ -287,10 +288,11 @@ func readCode(err Error) int {
287288
return int(v.FieldByName("go116code").Int())
288289
}
289290

290-
// addrOldComparableSemantics(conf) returns &conf.oldComparableSemantics (unexported field).
291-
func addrOldComparableSemantics(conf *Config) *bool {
291+
// boolFieldAddr(conf, name) returns the address of the boolean field conf.<name>.
292+
// For accessing unexported fields.
293+
func boolFieldAddr(conf *Config, name string) *bool {
292294
v := reflect.Indirect(reflect.ValueOf(conf))
293-
return (*bool)(v.FieldByName("oldComparableSemantics").Addr().UnsafePointer())
295+
return (*bool)(v.FieldByName(name).Addr().UnsafePointer())
294296
}
295297

296298
// TestManual is for manual testing of a package - either provided

src/go/types/decl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func pathString(path []Object) string {
5454
// objDecl type-checks the declaration of obj in its respective (file) environment.
5555
// For the meaning of def, see Checker.definedType, in typexpr.go.
5656
func (check *Checker) objDecl(obj Object, def *Named) {
57-
if trace && obj.Type() == nil {
57+
if check.conf.trace && obj.Type() == nil {
5858
if check.indent == 0 {
5959
fmt.Println() // empty line between top-level objects for readability
6060
}
@@ -264,7 +264,7 @@ loop:
264264
}
265265
}
266266

267-
if trace {
267+
if check.conf.trace {
268268
check.trace(obj.Pos(), "## cycle detected: objPath = %s->%s (len = %d)", pathString(cycle), obj.Name(), len(cycle))
269269
if tparCycle {
270270
check.trace(obj.Pos(), "## cycle contains: generic type in a type parameter list")
@@ -707,7 +707,7 @@ func (check *Checker) declareTypeParams(tparams []*TypeParam, names []*ast.Ident
707707
tparams = append(tparams, tpar)
708708
}
709709

710-
if trace && len(names) > 0 {
710+
if check.conf.trace && len(names) > 0 {
711711
check.trace(names[0].Pos(), "type params = %v", tparams[len(tparams)-len(names):])
712712
}
713713

src/go/types/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (check *Checker) report(errp *error_) {
266266
check.firstErr = err
267267
}
268268

269-
if trace {
269+
if check.conf.trace {
270270
pos := e.Pos
271271
msg := e.Msg
272272
check.trace(pos, "ERROR: %s", msg)

src/go/types/expr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ const (
12241224
// If allowGeneric is set, the operand type may be an uninstantiated
12251225
// parameterized type or function value.
12261226
func (check *Checker) rawExpr(x *operand, e ast.Expr, hint Type, allowGeneric bool) exprKind {
1227-
if trace {
1227+
if check.conf.trace {
12281228
check.trace(e.Pos(), "-- expr %s", e)
12291229
check.indent++
12301230
defer func() {

src/go/types/named.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ func (check *Checker) context() *Context {
581581
// returning the result. Returns Typ[Invalid] if there was an error.
582582
func (n *Named) expandUnderlying() Type {
583583
check := n.check
584-
if check != nil && trace {
584+
if check != nil && check.conf.trace {
585585
check.trace(n.obj.pos, "-- Named.expandUnderlying %s", n)
586586
check.indent++
587587
defer func() {

src/go/types/stmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (check *Checker) funcBody(decl *declInfo, name string, sig *Signature, body
1919
panic("function body not ignored")
2020
}
2121

22-
if trace {
22+
if check.conf.trace {
2323
check.trace(body.Pos(), "-- %s: %s", name, sig)
2424
}
2525

src/go/types/subst.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func (subst *subster) typ(typ Type) Type {
204204
case *Named:
205205
// dump is for debugging
206206
dump := func(string, ...any) {}
207-
if subst.check != nil && trace {
207+
if subst.check != nil && subst.check.conf.trace {
208208
subst.check.indent++
209209
defer func() {
210210
subst.check.indent--

src/go/types/typeset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_T
170170
return &topTypeSet
171171
}
172172

173-
if check != nil && trace {
173+
if check != nil && check.conf.trace {
174174
// Types don't generally have position information.
175175
// If we don't have a valid pos provided, try to use
176176
// one close enough.

src/go/types/typexpr.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func goTypeName(typ Type) string {
214214
// typInternal drives type checking of types.
215215
// Must only be called by definedType or genericType.
216216
func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
217-
if trace {
217+
if check.conf.trace {
218218
check.trace(e0.Pos(), "-- type %s", e0)
219219
check.indent++
220220
defer func() {
@@ -395,7 +395,7 @@ func (check *Checker) typInternal(e0 ast.Expr, def *Named) (T Type) {
395395
}
396396

397397
func (check *Checker) instantiatedType(ix *typeparams.IndexExpr, def *Named) (res Type) {
398-
if trace {
398+
if check.conf.trace {
399399
check.trace(ix.Pos(), "-- instantiating type %s with %s", ix.X, ix.Indices)
400400
check.indent++
401401
defer func() {

0 commit comments

Comments
 (0)