]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix reassignVisitor
authorMatthew Dempsky <mdempsky@google.com>
Fri, 30 Oct 2020 17:36:31 +0000 (10:36 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 30 Oct 2020 19:30:44 +0000 (19:30 +0000)
reassignVisitor was short-circuiting on assignment statements after
checking the LHS, but there might be further assignment statements
nested within the RHS expressions.

Fixes #42284.

Change-Id: I175eef87513b973ed5ebe6a6527adb9766dde6cf
Reviewed-on: https://go-review.googlesource.com/c/go/+/266618
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/gc/inl.go
test/fixedbugs/issue42284.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue42284.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue42284.go [new file with mode: 0644]

index 5b589082994231b756af31bef9b8792310acdb9b..8a5c6d86666f6f5e6e85dc0c91def641e0b82a5f 100644 (file)
@@ -839,14 +839,12 @@ func (v *reassignVisitor) visit(n *Node) *Node {
                if n.Left == v.name && n != v.name.Name.Defn {
                        return n
                }
-               return nil
        case OAS2, OAS2FUNC, OAS2MAPR, OAS2DOTTYPE:
                for _, p := range n.List.Slice() {
                        if p == v.name && n != v.name.Name.Defn {
                                return n
                        }
                }
-               return nil
        }
        if a := v.visit(n.Left); a != nil {
                return a
diff --git a/test/fixedbugs/issue42284.dir/a.go b/test/fixedbugs/issue42284.dir/a.go
new file mode 100644 (file)
index 0000000..e1271af
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2020 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 a
+
+type I interface{ M() }
+type T int
+
+func (T) M() {} // ERROR "can inline T.M"
+
+func F(i I) I { // ERROR "can inline F" "leaking param: i to result ~r1 level=0"
+       i = nil
+       return i
+}
+
+func g() { // ERROR "can inline g"
+       // BAD: T(0) could be stack allocated.
+       i := F(T(0)) // ERROR "inlining call to F" "T\(0\) escapes to heap"
+
+       // Testing that we do NOT devirtualize here:
+       i.M()
+}
diff --git a/test/fixedbugs/issue42284.dir/b.go b/test/fixedbugs/issue42284.dir/b.go
new file mode 100644 (file)
index 0000000..3305166
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright 2020 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 b
+
+import "./a"
+
+func g() { // ERROR "can inline g"
+       // BAD: T(0) could be stack allocated.
+       i := a.F(a.T(0)) // ERROR "inlining call to a.F" "a.T\(0\) escapes to heap"
+
+       // Testing that we do NOT devirtualize here:
+       i.M()
+}
diff --git a/test/fixedbugs/issue42284.go b/test/fixedbugs/issue42284.go
new file mode 100644 (file)
index 0000000..e5d6173
--- /dev/null
@@ -0,0 +1,7 @@
+// errorcheckdir -0 -m
+
+// Copyright 2020 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 ignored