Skip to content

Commit 463fe95

Browse files
committed
cmd/compile: fix duplicate code generation in swt.go
When combining adjacent type switch cases with the same type hash, we failed to actually remove the combined cases, so we would generate code for them twice. We use MD5 for type hashes, so collisions are rare, but they do currently appear in test/fixedbugs/bug248.dir/bug2.go, which is how I noticed this failure. Passes toolstash-check. Change-Id: I66729b3366b96cb8ddc8fa6f3ebea11ef6d74012 Reviewed-on: https://go-review.googlesource.com/100461 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]>
1 parent 183fd6f commit 463fe95

File tree

1 file changed

+6
-6
lines changed
  • src/cmd/compile/internal/gc

1 file changed

+6
-6
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,19 +803,19 @@ func (s *typeSwitch) walk(sw *Node) {
803803
}
804804

805805
// combine adjacent cases with the same hash
806-
ncase := 0
807-
for i := 0; i < run; i++ {
808-
ncase++
806+
var batch []caseClause
807+
for i, j := 0, 0; i < run; i = j {
809808
hash := []*Node{cc[i].node.Right}
810-
for j := i + 1; j < run && cc[i].hash == cc[j].hash; j++ {
809+
for j = i + 1; j < run && cc[i].hash == cc[j].hash; j++ {
811810
hash = append(hash, cc[j].node.Right)
812811
}
813812
cc[i].node.Right = liststmt(hash)
813+
batch = append(batch, cc[i])
814814
}
815815

816816
// binary search among cases to narrow by hash
817-
cas = append(cas, s.walkCases(cc[:ncase]))
818-
cc = cc[ncase:]
817+
cas = append(cas, s.walkCases(batch))
818+
cc = cc[run:]
819819
}
820820

821821
// handle default case

0 commit comments

Comments
 (0)