From 4f97a7e6eae588784c6bd7f87251b29c1d6c859a Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 28 Aug 2023 02:33:06 -0700 Subject: [PATCH] cmd/compile/internal/ir: set Addrtaken on Canonical ONAME too In CL 522879, I moved the logic for setting Addrtaken from typecheck's markAddrOf and ComputeAddrtaken directly into ir.NewAddrExpr. However, I took the logic from markAddrOf, and failed to notice that ComputeAddrtaken also set Addrtaken on the canonical ONAME. The result is that if the only address-of expressions were within a function literal, the canonical variable never got marked Addrtaken. In turn, this could cause the consistency check in ir.Reassigned to fail. (Yay for consistency checks turning mistakes into ICEs, rather than miscompilation.) Fixes #62313. Change-Id: Ieab2854cd7fcc1b6c5d1e61de66453add9890a4f Reviewed-on: https://go-review.googlesource.com/c/go/+/523375 Reviewed-by: Cuong Manh Le Reviewed-by: Bryan Mills Run-TryBot: Matthew Dempsky Auto-Submit: Matthew Dempsky TryBot-Result: Gopher Robot --- src/cmd/compile/internal/ir/expr.go | 13 +++++++++++++ test/fixedbugs/issue62313.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 test/fixedbugs/issue62313.go diff --git a/src/cmd/compile/internal/ir/expr.go b/src/cmd/compile/internal/ir/expr.go index 9ade6f6a6e..37a30edca2 100644 --- a/src/cmd/compile/internal/ir/expr.go +++ b/src/cmd/compile/internal/ir/expr.go @@ -92,6 +92,19 @@ func NewAddrExpr(pos src.XPos, x Node) *AddrExpr { n.op = OADDR if r, ok := OuterValue(x).(*Name); ok && r.Op() == ONAME { r.SetAddrtaken(true) + + // If r is a closure variable, we need to mark its canonical + // variable as addrtaken too, so that closure conversion + // captures it by reference. + // + // Exception: if we've already marked the variable as + // capture-by-value, then that means this variable isn't + // logically modified, and we must be taking its address to pass + // to a runtime function that won't mutate it. In that case, we + // only need to make sure our own copy is addressable. + if r.IsClosureVar() && !r.Byval() { + r.Canonical().SetAddrtaken(true) + } } } diff --git a/test/fixedbugs/issue62313.go b/test/fixedbugs/issue62313.go new file mode 100644 index 0000000000..139f1ebdf3 --- /dev/null +++ b/test/fixedbugs/issue62313.go @@ -0,0 +1,13 @@ +// 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 p + +func f() { + var err error = nil + defer func() { _ = &err }() + err.Error() +} -- 2.50.0