Skip to content

Commit 3db6d46

Browse files
cuonglmmdempsky
authored andcommitted
cmd/compile: add marker for skipping dowidth when tracing typecheck
The root cause of #33658 is that fmt.Printf does have side effects when printing Type. typefmt for TINTER will call Type.Fields to get all embedded fields and methods. The thing is that type.Fields itself will call dowidth, which will expand the embedded interface, make it non-embedded anymore. To fix it, we add a marker while we are tracing, so dowidth can know and return immediately without doing anything. Fixes #33658 Change-Id: Id4b70ff68a3b802675deae96793fdb8f7ef1a4a7 Reviewed-on: https://go-review.googlesource.com/c/go/+/190537 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
1 parent 5eec0a9 commit 3db6d46

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/cmd/compile/internal/gc/align.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ func widstruct(errtype *types.Type, t *types.Type, o int64, flag int) int64 {
178178
// have not already been calculated, it calls Fatal.
179179
// This is used to prevent data races in the back end.
180180
func dowidth(t *types.Type) {
181+
// Calling dowidth when typecheck tracing enabled is not safe.
182+
// See issue #33658.
183+
if enableTrace && skipDowidthForTracing {
184+
return
185+
}
181186
if Widthptr == 0 {
182187
Fatalf("dowidth without betypeinit")
183188
}

src/cmd/compile/internal/gc/typecheck.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const enableTrace = false
1616

1717
var trace bool
1818
var traceIndent []byte
19+
var skipDowidthForTracing bool
1920

2021
func tracePrint(title string, n *Node) func(np **Node) {
2122
indent := traceIndent
@@ -29,6 +30,8 @@ func tracePrint(title string, n *Node) func(np **Node) {
2930
tc = n.Typecheck()
3031
}
3132

33+
skipDowidthForTracing = true
34+
defer func() { skipDowidthForTracing = false }()
3235
fmt.Printf("%s: %s%s %p %s %v tc=%d\n", pos, indent, title, n, op, n, tc)
3336
traceIndent = append(traceIndent, ". "...)
3437

@@ -51,6 +54,8 @@ func tracePrint(title string, n *Node) func(np **Node) {
5154
typ = n.Type
5255
}
5356

57+
skipDowidthForTracing = true
58+
defer func() { skipDowidthForTracing = false }()
5459
fmt.Printf("%s: %s=> %p %s %v tc=%d type=%#L\n", pos, indent, n, op, n, tc, typ)
5560
}
5661
}

0 commit comments

Comments
 (0)