Skip to content

Commit

Permalink
cmd/compile: workaround inlining of closures with type switches
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Dan Scales <[email protected]>
Trust: Dan Scales <[email protected]>
Trust: Cuong Manh Le <[email protected]>
  • Loading branch information
mdempsky committed Apr 26, 2021
1 parent a53dc4c commit 9f60169
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cmd/compile/internal/inline/inl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions test/fixedbugs/issue45743.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 9f60169

Please sign in to comment.