diff options
| author | Ian Lance Taylor <iant@golang.org> | 2025-03-11 10:10:57 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-11 16:42:19 -0700 |
| commit | 6b7763407c83e9014c94997f1d454b175a6ea601 (patch) | |
| tree | 9420ce5de3d242cdbae50f261d5129f1eb153399 /src/testing/testing.go | |
| parent | 3fc4af70d04682ab42744e4ce78f95025688996d (diff) | |
| download | go-6b7763407c83e9014c94997f1d454b175a6ea601.tar.xz | |
testing: recognize helper functions that use range-over-function
This assumes the current behavior of the gc compiler:
range functions use a suffix of -rangeNNNN.
Fixes #72794
Change-Id: I3c10c60829853cf2cb4c17a75f6243def0313ae9
Reviewed-on: https://go-review.googlesource.com/c/go/+/656775
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: David Chase <drchase@google.com>
Diffstat (limited to 'src/testing/testing.go')
| -rw-r--r-- | src/testing/testing.go | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/testing/testing.go b/src/testing/testing.go index 626772e57a..f6ecd5b901 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -816,8 +816,15 @@ func (c *common) frameSkip(skip int) runtime.Frame { } frames := runtime.CallersFrames(pc[:n]) var firstFrame, prevFrame, frame runtime.Frame + skipRange := false for more := true; more; prevFrame = frame { frame, more = frames.Next() + if skipRange { + // Skip the iterator function when a helper + // functions does a range over function. + skipRange = false + continue + } if frame.Function == "runtime.gopanic" { continue } @@ -861,7 +868,25 @@ func (c *common) frameSkip(skip int) runtime.Frame { c.helperNames[pcToName(pc)] = struct{}{} } } - if _, ok := c.helperNames[frame.Function]; !ok { + + fnName := frame.Function + // Ignore trailing -rangeN used for iterator functions. + const rangeSuffix = "-range" + if suffixIdx := strings.LastIndex(fnName, rangeSuffix); suffixIdx > 0 { + ok := true + for i := suffixIdx + len(rangeSuffix); i < len(fnName); i++ { + if fnName[i] < '0' || fnName[i] > '9' { + ok = false + break + } + } + if ok { + fnName = fnName[:suffixIdx] + skipRange = true + } + } + + if _, ok := c.helperNames[fnName]; !ok { // Found a frame that wasn't inside a helper function. return frame } |
