Skip to content

Commit 1e12c63

Browse files
committed
cmd/compile: fix -m=2 output for recursive function with closures
ir.VisitFuncsBottomUp returns recursive==true for functions which call themselves. It also returns any closures inside that function. We don't want to report the closures as recursive, as they really aren't. Only the containing function is recursive. Fixes #54159 Change-Id: I3b4d6710a389ec1d6b250ba8a7065f2e985bdbe1 Reviewed-on: https://go-review.googlesource.com/c/go/+/463233 Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Keith Randall <[email protected]>
1 parent b15297f commit 1e12c63

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/cmd/compile/internal/inline/inl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func InlineDecls(p *pgo.Profile, decls []ir.Node, doInline bool) {
195195
// across more than one function.
196196
CanInline(n, p)
197197
} else {
198-
if base.Flag.LowerM > 1 {
198+
if base.Flag.LowerM > 1 && n.OClosure == nil {
199199
fmt.Printf("%v: cannot inline %v: recursive\n", ir.Line(n), n.Nname)
200200
}
201201
}

test/fixedbugs/issue54159.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// errorcheck -0 -m=2
2+
3+
// Copyright 2023 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func run() { // ERROR "cannot inline run: recursive"
10+
f := func() { // ERROR "can inline run.func1 with cost .* as:.*" "func literal does not escape"
11+
g() // ERROR "inlining call to g"
12+
}
13+
f() // ERROR "inlining call to run.func1" "inlining call to g"
14+
run()
15+
}
16+
17+
func g() { // ERROR "can inline g with cost .* as:.*"
18+
}
19+
20+
func main() { // ERROR "can inline main with cost .* as:.*"
21+
run()
22+
}

0 commit comments

Comments
 (0)