From 8c5a54f698873244694c8e6f20d3794a5f32ba3f Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 9 Aug 2023 09:04:37 -0700 Subject: [PATCH] cmd/compile: keep all open-coded defer slots as used Open-coded defer slots are assigned indices upfront, so they're logically like elements in an array. Without reassigning the indices, we need to keep all of the elements alive so their relative offsets are correct. Fixes #61895. Change-Id: Ie0191fdb33276f4e8ed0becb69086524fff022b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/517856 Reviewed-by: Keith Randall Reviewed-by: Keith Randall Run-TryBot: Matthew Dempsky TryBot-Result: Gopher Robot --- src/cmd/compile/internal/ssagen/pgen.go | 8 ++++++++ test/fixedbugs/issue61895.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/fixedbugs/issue61895.go diff --git a/src/cmd/compile/internal/ssagen/pgen.go b/src/cmd/compile/internal/ssagen/pgen.go index ca064a16a7..e7a0699641 100644 --- a/src/cmd/compile/internal/ssagen/pgen.go +++ b/src/cmd/compile/internal/ssagen/pgen.go @@ -121,6 +121,14 @@ func (s *ssafn) AllocFrame(f *ssa.Func) { // Mark the PAUTO's unused. for _, ln := range fn.Dcl { + if ln.OpenDeferSlot() { + // Open-coded defer slots have indices that were assigned + // upfront during SSA construction, but the defer statement can + // later get removed during deadcode elimination (#61895). To + // keep their relative offsets correct, treat them all as used. + continue + } + if needAlloc(ln) { ln.SetUsed(false) } diff --git a/test/fixedbugs/issue61895.go b/test/fixedbugs/issue61895.go new file mode 100644 index 0000000000..cda649483d --- /dev/null +++ b/test/fixedbugs/issue61895.go @@ -0,0 +1,15 @@ +// compile + +// Copyright 2023 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. + +package main + +func main() { + for { + } + + defer func() {}() + defer func() {}() +} -- 2.48.1