Skip to content

Commit c15d0a9

Browse files
cuonglmrandall77
authored andcommitted
cmd/compile: fix missing export/import init nodes of builtins that allow multiple arguments
Fixes #52590 Change-Id: Ibd0852ae2a9ad8e4598e93320daff1b3c196929f Reviewed-on: https://go-review.googlesource.com/c/go/+/402854 Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 4289bd3 commit c15d0a9

File tree

5 files changed

+111
-4
lines changed

5 files changed

+111
-4
lines changed

src/cmd/compile/internal/typecheck/iexport.go

+2
Original file line numberDiff line numberDiff line change
@@ -2001,6 +2001,7 @@ func (w *exportWriter) expr(n ir.Node) {
20012001
n := n.(*ir.BinaryExpr)
20022002
w.op(n.Op())
20032003
w.pos(n.Pos())
2004+
w.stmtList(n.Init())
20042005
w.expr(n.X)
20052006
w.expr(n.Y)
20062007
if go117ExportTypes {
@@ -2037,6 +2038,7 @@ func (w *exportWriter) expr(n ir.Node) {
20372038
n := n.(*ir.CallExpr)
20382039
w.op(n.Op())
20392040
w.pos(n.Pos())
2041+
w.stmtList(n.Init())
20402042
w.exprList(n.Args) // emits terminating OEND
20412043
// only append() calls may contain '...' arguments
20422044
if n.Op() == ir.OAPPEND {

src/cmd/compile/internal/typecheck/iimport.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -1552,20 +1552,25 @@ func (r *importReader) node() ir.Node {
15521552
return ir.NewConvExpr(r.pos(), op, r.typ(), r.expr())
15531553

15541554
case ir.OCOPY, ir.OCOMPLEX, ir.OREAL, ir.OIMAG, ir.OAPPEND, ir.OCAP, ir.OCLOSE, ir.ODELETE, ir.OLEN, ir.OMAKE, ir.ONEW, ir.OPANIC, ir.ORECOVER, ir.OPRINT, ir.OPRINTN, ir.OUNSAFEADD, ir.OUNSAFESLICE:
1555+
pos := r.pos()
15551556
if go117ExportTypes {
15561557
switch op {
15571558
case ir.OCOPY, ir.OCOMPLEX, ir.OUNSAFEADD, ir.OUNSAFESLICE:
1558-
n := ir.NewBinaryExpr(r.pos(), op, r.expr(), r.expr())
1559+
init := r.stmtList()
1560+
n := ir.NewBinaryExpr(pos, op, r.expr(), r.expr())
1561+
n.SetInit(init)
15591562
n.SetType(r.typ())
15601563
return n
15611564
case ir.OREAL, ir.OIMAG, ir.OCAP, ir.OCLOSE, ir.OLEN, ir.ONEW, ir.OPANIC:
1562-
n := ir.NewUnaryExpr(r.pos(), op, r.expr())
1565+
n := ir.NewUnaryExpr(pos, op, r.expr())
15631566
if op != ir.OPANIC {
15641567
n.SetType(r.typ())
15651568
}
15661569
return n
15671570
case ir.OAPPEND, ir.ODELETE, ir.ORECOVER, ir.OPRINT, ir.OPRINTN:
1568-
n := ir.NewCallExpr(r.pos(), op, nil, r.exprList())
1571+
init := r.stmtList()
1572+
n := ir.NewCallExpr(pos, op, nil, r.exprList())
1573+
n.SetInit(init)
15691574
if op == ir.OAPPEND {
15701575
n.IsDDD = r.bool()
15711576
}
@@ -1577,7 +1582,14 @@ func (r *importReader) node() ir.Node {
15771582
// ir.OMAKE
15781583
goto error
15791584
}
1580-
n := builtinCall(r.pos(), op)
1585+
n := builtinCall(pos, op)
1586+
switch n.Op() {
1587+
case ir.OCOPY, ir.OCOMPLEX, ir.OUNSAFEADD, ir.OUNSAFESLICE:
1588+
// treated like other builtin calls
1589+
fallthrough
1590+
case ir.OAPPEND, ir.ODELETE, ir.ORECOVER, ir.OPRINT, ir.OPRINTN:
1591+
n.SetInit(r.stmtList())
1592+
}
15811593
n.Args = r.exprList()
15821594
if op == ir.OAPPEND {
15831595
n.IsDDD = r.bool()

test/fixedbugs/issue52590.dir/a.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
import "unsafe"
8+
9+
func Append() {
10+
_ = append(appendArgs())
11+
}
12+
13+
func Delete() {
14+
delete(deleteArgs())
15+
}
16+
17+
func Print() {
18+
print(ints())
19+
}
20+
21+
func Println() {
22+
println(ints())
23+
}
24+
25+
func Complex() {
26+
_ = complex(float64s())
27+
}
28+
29+
func Copy() {
30+
copy(slices())
31+
}
32+
33+
func UnsafeAdd() {
34+
_ = unsafe.Add(unsafeAdd())
35+
}
36+
37+
func UnsafeSlice() {
38+
_ = unsafe.Slice(unsafeSlice())
39+
}
40+
41+
func appendArgs() ([]int, int) {
42+
return []int{}, 0
43+
}
44+
45+
func deleteArgs() (map[int]int, int) {
46+
return map[int]int{}, 0
47+
}
48+
49+
func ints() (int, int) {
50+
return 1, 1
51+
}
52+
53+
func float64s() (float64, float64) {
54+
return 0, 0
55+
}
56+
57+
func slices() ([]int, []int) {
58+
return []int{}, []int{}
59+
}
60+
61+
func unsafeAdd() (unsafe.Pointer, int) {
62+
return nil, 0
63+
}
64+
65+
func unsafeSlice() (*byte, int) {
66+
var p [10]byte
67+
return &p[0], 0
68+
}

test/fixedbugs/issue52590.dir/b.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import "./a"
8+
9+
func f() {
10+
a.Append()
11+
a.Delete()
12+
a.Print()
13+
a.Println()
14+
a.Complex()
15+
a.Copy()
16+
a.UnsafeAdd()
17+
a.UnsafeSlice()
18+
}

test/fixedbugs/issue52590.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compiledir
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 ignored

0 commit comments

Comments
 (0)