Skip to content

Commit d5f6ba9

Browse files
committed
[dev.typeparams] test: add regression test for go/defer wrapper
CL 330330 moved logic for wrapping go/defer from order to esacpe analysis. It introduced a bug involves go/defer statement with ABI0 functions. Consider this following code: package p //go:cgo_unsafe_args func g(*int) (r1 struct{}) { return } func f() { defer g(new(int)) } g is a cgo-like generated function with ABI0. While compiling g, we set the offset per ABI0. The function f is rewritten into: func f() { _0, _1 := g, new(int) defer func() { _0(_1) }() } The temporary _0 hold function value with the same type as g, but with class PAUTO. Thus ssagen/ssa.go:state.call cannot handle it and use ABIDefault to set the offset, causes the offset of r1 changed CL 330332 intended to optimize code generated for wrapping function, by rewriting the wrapper function into: func f() { _0 := new(int) defer func() { g(_0) }() } So it fixed the bug unintentionally. This CL add regression test for this bug, and also add a comment to explain while not wrapping declared function is important. Updates #47227 Change-Id: I75c83d1d9cc7fd4699e6b218a295d0c0a10ef471 Reviewed-on: https://go-review.googlesource.com/c/go/+/334882 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 6a93167 commit d5f6ba9

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/cmd/compile/internal/escape/call.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ func (e *escape) rewriteArgument(argp *ir.Node, init *ir.Nodes, call ir.Node, fn
320320
return
321321
case ir.ONAME:
322322
if arg.(*ir.Name).Class == ir.PFUNC {
323+
// TODO(cuonglm): figure it why this is necessary, we should not depend on this to make
324+
// ABI analyze works correctly (see #47227 and discussion in CL 334882).
323325
return
324326
}
325327
}

test/fixedbugs/issue47227.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run fake-arg-to-force-use-of-go-run
2+
3+
// Copyright 2021 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+
//go:build cgo
8+
// +build cgo
9+
10+
package main
11+
12+
// void f(int *p) { *p = 0x12345678; }
13+
import "C"
14+
15+
func main() {
16+
var x C.int
17+
func() {
18+
defer C.f(&x)
19+
}()
20+
if x != 0x12345678 {
21+
panic("FAIL")
22+
}
23+
}

0 commit comments

Comments
 (0)