]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: generate code that type checks when inlining variadic functions
authorDavid Lazar <lazard@golang.org>
Mon, 28 Nov 2016 22:39:31 +0000 (17:39 -0500)
committerDavid Lazar <lazard@golang.org>
Wed, 30 Nov 2016 19:46:00 +0000 (19:46 +0000)
This fixes a bug in -l=3 or higher.

To inline a variadic function, the compiler generates code that constructs
a slice of arguments for the variadic parameter. Consider the function

  func Foo(xs ...string)

and the call Foo("hello", "world"). To inline the call to Foo, the
compiler used to generate

  xs := [2]string{"hello", "world"}[:]

which doesn't type check:

  invalid operation [2]string literal[:] (slice of unaddressable value).

Now, the compiler generates

  xs := []string{"hello", "world"}

which does type check.

Fixes #18116.

Change-Id: I0ee531ef2e6cc276db6fb12602b25a46d6d5db21
Reviewed-on: https://go-review.googlesource.com/33671
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/gc/inl.go
test/inline_variadic.go [new file with mode: 0644]

index 59a047fdf250b8f72510b24187bf14cc1358c863..d8f1f2453645c8e69be2cd9d6def382d127359ae 100644 (file)
@@ -781,10 +781,9 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
                        as.Right = nodnil()
                        as.Right.Type = varargtype
                } else {
-                       vararrtype := typArray(varargtype.Elem(), int64(varargcount))
-                       as.Right = nod(OCOMPLIT, nil, typenod(vararrtype))
+                       varslicetype := typSlice(varargtype.Elem())
+                       as.Right = nod(OCOMPLIT, nil, typenod(varslicetype))
                        as.Right.List.Set(varargs)
-                       as.Right = nod(OSLICE, as.Right, nil)
                }
 
                as = typecheck(as, Etop)
diff --git a/test/inline_variadic.go b/test/inline_variadic.go
new file mode 100644 (file)
index 0000000..6466c2b
--- /dev/null
@@ -0,0 +1,19 @@
+// errorcheck -0 -m -l=3
+
+// Copyright 2016 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.
+
+// Test more aggressive inlining (-l=3 allows variadic functions)
+// See issue #18116.
+
+package foo
+
+func head(xs ...string) string { // ERROR "can inline head" "leaking param: xs to result"
+       return xs[0]
+}
+
+func f() string { // ERROR "can inline f"
+       x := head("hello", "world") // ERROR "inlining call to head" "\[\]string literal does not escape"
+       return x
+}