From 9f601690da59e601ff68f9868d5eb863bd770eae Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 24 Apr 2021 02:12:55 -0700 Subject: [PATCH] cmd/compile: workaround inlining of closures with type switches Within clovar, n.Defn can also be *ir.TypeSwitchGuard. The proper fix here would be to populate m.Defn and have it filled in too, but we already leave it nil in inlvar. So for consistency, this CL does the same in clovar too. Eventually inl.go should be rewritten to fully respect IR invariants. Fixes #45743. Change-Id: I8b38e5d8b2329ad242de97670f2141f713954d28 Reviewed-on: https://go-review.googlesource.com/c/go/+/313289 Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Dan Scales Trust: Dan Scales Trust: Cuong Manh Le --- src/cmd/compile/internal/inline/inl.go | 2 ++ test/fixedbugs/issue45743.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 test/fixedbugs/issue45743.go diff --git a/src/cmd/compile/internal/inline/inl.go b/src/cmd/compile/internal/inline/inl.go index 54fcb2b830d..339ea77509a 100644 --- a/src/cmd/compile/internal/inline/inl.go +++ b/src/cmd/compile/internal/inline/inl.go @@ -1194,6 +1194,8 @@ func (subst *inlsubst) clovar(n *ir.Name) *ir.Name { case *ir.AssignStmt, *ir.AssignListStmt: // Mark node for reassignment at the end of inlsubst.node. m.Defn = &subst.defnMarker + case *ir.TypeSwitchGuard: + // TODO(mdempsky): Set m.Defn properly. See discussion on #45743. default: base.FatalfAt(n.Pos(), "unexpected Defn: %+v", defn) } diff --git a/test/fixedbugs/issue45743.go b/test/fixedbugs/issue45743.go new file mode 100644 index 00000000000..0b30e0f2a4d --- /dev/null +++ b/test/fixedbugs/issue45743.go @@ -0,0 +1,20 @@ +// compile + +// Copyright 2021 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 fn() func(interface{}) { + return func(o interface{}) { + switch v := o.(type) { + case *int: + *v = 1 + } + } +} + +func main() { + fn() +}