aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/rangefunc
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2023-11-07 16:37:17 -0500
committerDavid Chase <drchase@google.com>2023-11-15 20:06:34 +0000
commit6f5aba995fdea76433337d1a8030518bdadfae28 (patch)
treec55ec64ee6b24e95a21b9494d2c203d1b6cd92de /src/cmd/compile/internal/rangefunc
parentfdc21f3eafe94490e55e0bf018490b3aa9ba2383 (diff)
downloadgo-6f5aba995fdea76433337d1a8030518bdadfae28.tar.xz
cmd/compile: replace magic numbers "2" and "1" with named constant
This was originally done for a #next-encoding-based check for misbehaving loops, but it's a good idea anyhow because it makes the code slightly easier to follow or change (we may decide to check for errors the "other way" anyhow, later). Change-Id: I2ba8f6e0f9146f0ff148a900eabdefd0fffebf8b Reviewed-on: https://go-review.googlesource.com/c/go/+/540261 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/rangefunc')
-rw-r--r--src/cmd/compile/internal/rangefunc/rewrite.go13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/cmd/compile/internal/rangefunc/rewrite.go b/src/cmd/compile/internal/rangefunc/rewrite.go
index ac12c53c2b..c28c9a1207 100644
--- a/src/cmd/compile/internal/rangefunc/rewrite.go
+++ b/src/cmd/compile/internal/rangefunc/rewrite.go
@@ -640,6 +640,13 @@ func (r *rewriter) editReturn(x *syntax.ReturnStmt) syntax.Stmt {
return bl
}
+// perLoopStep is part of the encoding of loop-spanning control flow
+// for function range iterators. Each multiple of two encodes a "return false"
+// passing control to an enclosing iterator; a terminal value of 1 encodes
+// "return true" (i.e., local continue) from the body function, and a terminal
+// value of 0 encodes executing the remainder of the body function.
+const perLoopStep = 2
+
// editBranch returns the replacement for the branch statement x,
// or x itself if it should be left alone.
// See the package doc comment above for more context.
@@ -734,7 +741,7 @@ func (r *rewriter) editBranch(x *syntax.BranchStmt) syntax.Stmt {
// Set next to break the appropriate number of times;
// the final time may be a continue, not a break.
- next = 2 * depth
+ next = perLoopStep * depth
if x.Tok == syntax.Continue {
next--
}
@@ -948,10 +955,10 @@ func (r *rewriter) checks(loop *forLoop, pos syntax.Pos) []syntax.Stmt {
list = append(list, r.ifNext(syntax.Lss, 0, retStmt(r.useVar(r.false))))
}
if loop.checkBreak {
- list = append(list, r.ifNext(syntax.Geq, 2, retStmt(r.useVar(r.false))))
+ list = append(list, r.ifNext(syntax.Geq, perLoopStep, retStmt(r.useVar(r.false))))
}
if loop.checkContinue {
- list = append(list, r.ifNext(syntax.Eql, 1, retStmt(r.useVar(r.true))))
+ list = append(list, r.ifNext(syntax.Eql, perLoopStep-1, retStmt(r.useVar(r.true))))
}
}