Skip to content

Commit 6139019

Browse files
committed
cmd/compile: pick position of implicit break statements more carefully
The previous version used the position of the switch statement, which makes for potentially jumpy stepping and introduces a large number of statements repeating the line (tricky for inserting breaks). It also shared a single OBREAK node and this was not really a syntax "tree". This improves both the nostmt test (by 6 lines) and reduces the total badness score from dwarf-goodness (by about 200). Change-Id: I1f71b231a26f152bdb6ce9bc8f95828bb222f665 Reviewed-on: https://go-review.googlesource.com/c/go/+/188218 Run-TryBot: David Chase <[email protected]> Reviewed-by: Jeremy Faller <[email protected]>
1 parent f7f85bd commit 6139019

File tree

1 file changed

+10
-4
lines changed
  • src/cmd/compile/internal/gc

1 file changed

+10
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ func walkExprSwitch(sw *Node) {
268268
exprname: cond,
269269
}
270270

271-
br := nod(OBREAK, nil, nil)
272271
var defaultGoto *Node
273272
var body Nodes
274273
for _, ncase := range sw.List.Slice() {
@@ -290,13 +289,17 @@ func walkExprSwitch(sw *Node) {
290289
// Process body.
291290
body.Append(npos(ncase.Pos, nodSym(OLABEL, nil, label)))
292291
body.Append(ncase.Nbody.Slice()...)
293-
if !hasFall(ncase.Nbody.Slice()) {
292+
if fall, pos := hasFall(ncase.Nbody.Slice()); !fall {
293+
br := nod(OBREAK, nil, nil)
294+
br.Pos = pos
294295
body.Append(br)
295296
}
296297
}
297298
sw.List.Set(nil)
298299

299300
if defaultGoto == nil {
301+
br := nod(OBREAK, nil, nil)
302+
br.Pos = br.Pos.WithNotStmt()
300303
defaultGoto = br
301304
}
302305

@@ -469,7 +472,7 @@ func allCaseExprsAreSideEffectFree(sw *Node) bool {
469472
}
470473

471474
// hasFall reports whether stmts ends with a "fallthrough" statement.
472-
func hasFall(stmts []*Node) bool {
475+
func hasFall(stmts []*Node) (bool, src.XPos) {
473476
// Search backwards for the index of the fallthrough
474477
// statement. Do not assume it'll be in the last
475478
// position, since in some cases (e.g. when the statement
@@ -480,7 +483,10 @@ func hasFall(stmts []*Node) bool {
480483
for i >= 0 && stmts[i].Op == OVARKILL {
481484
i--
482485
}
483-
return i >= 0 && stmts[i].Op == OFALL
486+
if i < 0 {
487+
return false, src.NoXPos
488+
}
489+
return stmts[i].Op == OFALL, stmts[i].Pos
484490
}
485491

486492
// walkTypeSwitch generates an AST that implements sw, where sw is a

0 commit comments

Comments
 (0)