Skip to content

Commit f6f2b8e

Browse files
mdempskybradfitz
authored andcommitted
[release-branch.go1.19] cmd/compile/internal/inline: fix latent CalleeEffects issue
ir.ClosureExpr implements ir.InitNode, so ir.InitExpr can prepend init statements to it. However, CalleeEffects wasn't aware of this and could cause the init statements to get dropped when inlining a call to a closure. This isn't an issue today, because we don't create closures with init statements. But I ran into this within unified IR. Easy and robust solution: just take advantage that ir.TakeInit can handle any node. Fixes golang#54917. Change-Id: Ica05fbf6a8c5be4b11927daf84491a1140da5431 Reviewed-on: https://go-review.googlesource.com/c/go/+/422196 Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/429896 Reviewed-by: Michael Knyszek <[email protected]>
1 parent 79b7def commit f6f2b8e

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Diff for: src/cmd/compile/internal/inline/inl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -798,18 +798,18 @@ func mkinlcall(n *ir.CallExpr, fn *ir.Func, maxCost int32, inlMap map[*ir.Func]b
798798
// CalleeEffects appends any side effects from evaluating callee to init.
799799
func CalleeEffects(init *ir.Nodes, callee ir.Node) {
800800
for {
801+
init.Append(ir.TakeInit(callee)...)
802+
801803
switch callee.Op() {
802804
case ir.ONAME, ir.OCLOSURE, ir.OMETHEXPR:
803805
return // done
804806

805807
case ir.OCONVNOP:
806808
conv := callee.(*ir.ConvExpr)
807-
init.Append(ir.TakeInit(conv)...)
808809
callee = conv.X
809810

810811
case ir.OINLCALL:
811812
ic := callee.(*ir.InlinedCallExpr)
812-
init.Append(ir.TakeInit(ic)...)
813813
init.Append(ic.Body.Take()...)
814814
callee = ic.SingleResult()
815815

Diff for: test/fixedbugs/issue54911.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
type Set[T comparable] map[T]struct{}
10+
11+
func (s Set[T]) Add() Set[T] {
12+
return s
13+
}
14+
15+
func (s Set[T]) Copy() Set[T] {
16+
return Set[T].Add(s)
17+
}
18+
19+
func main() {
20+
_ = Set[int]{42: {}}
21+
}

0 commit comments

Comments
 (0)