From: Matthew Dempsky Date: Tue, 13 Mar 2018 22:07:12 +0000 (-0700) Subject: cmd/compile: fix duplicate code generation in swt.go X-Git-Tag: go1.11beta1~1201 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=463fe95bdd07e255a10ad36d5c51c8811cfee49f;p=gostls13.git 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 TryBot-Result: Gobot Gobot Reviewed-by: Josh Bleecher Snyder --- diff --git a/src/cmd/compile/internal/gc/swt.go b/src/cmd/compile/internal/gc/swt.go index 404a88444a..cc69d86870 100644 --- a/src/cmd/compile/internal/gc/swt.go +++ b/src/cmd/compile/internal/gc/swt.go @@ -803,19 +803,19 @@ func (s *typeSwitch) walk(sw *Node) { } // combine adjacent cases with the same hash - ncase := 0 - for i := 0; i < run; i++ { - ncase++ + var batch []caseClause + for i, j := 0, 0; i < run; i = j { hash := []*Node{cc[i].node.Right} - for j := i + 1; j < run && cc[i].hash == cc[j].hash; j++ { + for j = i + 1; j < run && cc[i].hash == cc[j].hash; j++ { hash = append(hash, cc[j].node.Right) } cc[i].node.Right = liststmt(hash) + batch = append(batch, cc[i]) } // binary search among cases to narrow by hash - cas = append(cas, s.walkCases(cc[:ncase])) - cc = cc[ncase:] + cas = append(cas, s.walkCases(batch)) + cc = cc[run:] } // handle default case