aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJunyang Shao <shaojunyang@google.com>2025-10-30 19:14:57 +0000
committerJunyang Shao <shaojunyang@google.com>2025-11-21 12:49:20 -0800
commit22f24f90b52958158a2de5fa68adf0faf7699e4e (patch)
treec71d09d9e3d69eb01d62f52df195f27c2edeb4a8 /test
parentcfb9d2eb73d9ecb0e5ea2ce5ef0d3f23593087be (diff)
downloadgo-22f24f90b52958158a2de5fa68adf0faf7699e4e.tar.xz
cmd/compile: change testing.B.Loop keep alive semantic
This CL implements this initial design of testing.B.Loop's keep variable alive semantic: https://github.com/golang/go/issues/61515#issuecomment-2407963248. Fixes #73137. Change-Id: I8060470dbcb0dda0819334f3615cc391ff0f6501 Reviewed-on: https://go-review.googlesource.com/c/go/+/716660 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'test')
-rw-r--r--test/bloop.go51
-rw-r--r--test/inline_testingbloop.go37
2 files changed, 51 insertions, 37 deletions
diff --git a/test/bloop.go b/test/bloop.go
new file mode 100644
index 0000000000..0d2dcba0ac
--- /dev/null
+++ b/test/bloop.go
@@ -0,0 +1,51 @@
+// errorcheck -0 -m
+
+// Copyright 2025 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Test keeping statements results in testing.B.Loop alive.
+// See issue #61515, #73137.
+
+package foo
+
+import "testing"
+
+func caninline(x int) int { // ERROR "can inline caninline"
+ return x
+}
+
+var something int
+
+func caninlineNoRet(x int) { // ERROR "can inline caninlineNoRet"
+ something = x
+}
+
+func caninlineVariadic(x ...int) { // ERROR "can inline caninlineVariadic" "x does not escape"
+ something = x[0]
+}
+
+func test(b *testing.B, localsink, cond int) { // ERROR "leaking param: b"
+ for i := 0; i < b.N; i++ {
+ caninline(1) // ERROR "inlining call to caninline"
+ }
+ for b.Loop() { // ERROR "inlining call to testing\.\(\*B\)\.Loop"
+ caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
+ caninlineNoRet(1) // ERROR "inlining call to caninlineNoRet" "function arg will be kept alive" ".* does not escape"
+ caninlineVariadic(1) // ERROR "inlining call to caninlineVariadic" "function arg will be kept alive" ".* does not escape"
+ caninlineVariadic(localsink) // ERROR "inlining call to caninlineVariadic" "localsink will be kept alive" ".* does not escape"
+ localsink = caninline(1) // ERROR "inlining call to caninline" "localsink will be kept alive" ".* does not escape"
+ localsink += 5 // ERROR "localsink will be kept alive" ".* does not escape"
+ localsink, cond = 1, 2 // ERROR "localsink will be kept alive" "cond will be kept alive" ".* does not escape"
+ if cond > 0 {
+ caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
+ }
+ switch cond {
+ case 2:
+ caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
+ }
+ {
+ caninline(1) // ERROR "inlining call to caninline" "function result will be kept alive" ".* does not escape"
+ }
+ }
+}
diff --git a/test/inline_testingbloop.go b/test/inline_testingbloop.go
deleted file mode 100644
index 702a652f56..0000000000
--- a/test/inline_testingbloop.go
+++ /dev/null
@@ -1,37 +0,0 @@
-// errorcheck -0 -m
-
-// Copyright 2024 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// Test no inlining of function calls in testing.B.Loop.
-// See issue #61515.
-
-package foo
-
-import "testing"
-
-func caninline(x int) int { // ERROR "can inline caninline"
- return x
-}
-
-func test(b *testing.B) { // ERROR "leaking param: b"
- for i := 0; i < b.N; i++ {
- caninline(1) // ERROR "inlining call to caninline"
- }
- for b.Loop() { // ERROR "skip inlining within testing.B.loop" "inlining call to testing\.\(\*B\)\.Loop"
- caninline(1)
- }
- for i := 0; i < b.N; i++ {
- caninline(1) // ERROR "inlining call to caninline"
- }
- for b.Loop() { // ERROR "skip inlining within testing.B.loop" "inlining call to testing\.\(\*B\)\.Loop"
- caninline(1)
- }
- for i := 0; i < b.N; i++ {
- caninline(1) // ERROR "inlining call to caninline"
- }
- for b.Loop() { // ERROR "skip inlining within testing.B.loop" "inlining call to testing\.\(\*B\)\.Loop"
- caninline(1)
- }
-}