cmd/compile: add ir.ContainsClosure

And use it to unify all codes that need parent/closure checking.

Change-Id: I0b0aa1b007598668dff2c4bee31e21f0fb3830ce
Reviewed-on: https://go-review.googlesource.com/c/go/+/650315
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
Cuong Manh Le 2025-02-18 23:03:54 +07:00 committed by Gopher Robot
parent 34073a736a
commit a08984bc8f
3 changed files with 21 additions and 28 deletions

View file

@ -627,3 +627,18 @@ func (fn *Func) DeclareParams(setNname bool) {
declareParams(params, PPARAM, "~p", 0)
declareParams(results, PPARAMOUT, "~r", len(params))
}
// ContainsClosure reports whether c is a closure contained within f.
func ContainsClosure(f, c *Func) bool {
// Common cases.
if f == c || c.OClosure == nil {
return false
}
for p := c.ClosureParent; p != nil; p = p.ClosureParent {
if p == f {
return true
}
}
return false
}