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 <cuong.manhle.vn@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Matthew Dempsky 2023-08-28 02:33:06 -07:00 committed by Gopher Robot
parent 106afda184
commit 4f97a7e6ea
2 changed files with 26 additions and 0 deletions

View file

@ -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)
}
}
}

View file

@ -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()
}