aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/cover
diff options
context:
space:
mode:
authorAndrew Gerrand <adg@golang.org>2019-05-31 16:58:44 +1000
committerAndrew Gerrand <adg@golang.org>2019-06-03 01:37:58 +0000
commit98100c56da0da1503e7612921eff821409aa6cce (patch)
tree391e06eb2476bfc0b59e22183c0a6d84f9744716 /src/cmd/cover
parentce656af9b5d9acb817b4de5170a11a2c42ad2047 (diff)
downloadgo-98100c56da0da1503e7612921eff821409aa6cce.tar.gz
go-98100c56da0da1503e7612921eff821409aa6cce.zip
cmd/cover: fix counting of blocks split by goto statements
When adding coverage counters to a block, the block's statement list is mutated. CL 77150 removed the part where the mutated list is assigned back to its parent node; this was confusing ast.Walk, which would then lose its place and stop walking the current block, dropping counters in the process. This change has addCounters make a copy of the list before mutating it, so that the original list doesn't change under Walk's feet. Fix #32200 Change-Id: Ia3b67d8cee860ceb7caf8748cb7a80ff9c6276e0 Reviewed-on: https://go-review.googlesource.com/c/go/+/179581 Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src/cmd/cover')
-rw-r--r--src/cmd/cover/cover.go3
-rw-r--r--src/cmd/cover/testdata/test.go4
2 files changed, 7 insertions, 0 deletions
diff --git a/src/cmd/cover/cover.go b/src/cmd/cover/cover.go
index 1748606c5e..e04c8834bd 100644
--- a/src/cmd/cover/cover.go
+++ b/src/cmd/cover/cover.go
@@ -386,6 +386,9 @@ func (f *File) addCounters(pos, insertPos, blockEnd token.Pos, list []ast.Stmt,
f.edit.Insert(f.offset(insertPos), f.newCounter(insertPos, blockEnd, 0)+";")
return
}
+ // Make a copy of the list, as we may mutate it and should leave the
+ // existing list intact.
+ list = append([]ast.Stmt(nil), list...)
// We have a block (statement list), but it may have several basic blocks due to the
// appearance of statements that affect the flow of control.
for {
diff --git a/src/cmd/cover/testdata/test.go b/src/cmd/cover/testdata/test.go
index 0b03ef91ab..b794962205 100644
--- a/src/cmd/cover/testdata/test.go
+++ b/src/cmd/cover/testdata/test.go
@@ -132,6 +132,10 @@ func testBlockRun() {
func testSwitch() {
for i := 0; i < 5; func() { i++; check(LINE, 5) }() {
+ goto label2
+ label1:
+ goto label1
+ label2:
switch i {
case 0:
check(LINE, 1)