1
0
mirror of https://github.com/golang/go synced 2024-09-29 12:04:28 -06:00
go/test/escape_field.go
Matthew Dempsky 9f89edcd96 cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.

However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).

For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.

Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-09-03 17:52:06 +00:00

175 lines
3.0 KiB
Go

// errorcheck -0 -m -l
// Copyright 2015 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 escape analysis with respect to field assignments.
package escape
var sink interface{}
type X struct {
p1 *int
p2 *int
a [2]*int
}
type Y struct {
x X
}
func field0() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i
sink = x.p1
}
func field1() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i
sink = x.p2
}
func field3() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i
sink = x // ERROR "x escapes to heap"
}
func field4() {
i := 0 // ERROR "moved to heap: i$"
var y Y
y.x.p1 = &i
x := y.x
sink = x // ERROR "x escapes to heap"
}
func field5() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape here
x.a[0] = &i
sink = x.a[1]
}
// BAD: we are not leaking param x, only x.p2
func field6(x *X) { // ERROR "leaking param content: x$"
sink = x.p2
}
func field6a() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i
field6(&x)
}
func field7() {
i := 0
var y Y
y.x.p1 = &i
x := y.x
var y1 Y
y1.x = x
_ = y1.x.p1
}
func field8() {
i := 0 // ERROR "moved to heap: i$"
var y Y
y.x.p1 = &i
x := y.x
var y1 Y
y1.x = x
sink = y1.x.p1
}
func field9() {
i := 0 // ERROR "moved to heap: i$"
var y Y
y.x.p1 = &i
x := y.x
var y1 Y
y1.x = x
sink = y1.x // ERROR "y1\.x escapes to heap"
}
func field10() {
i := 0 // ERROR "moved to heap: i$"
var y Y
// BAD: &i should not escape
y.x.p1 = &i
x := y.x
var y1 Y
y1.x = x
sink = y1.x.p2
}
func field11() {
i := 0 // ERROR "moved to heap: i$"
x := X{p1: &i}
sink = x.p1
}
func field12() {
i := 0 // ERROR "moved to heap: i$"
// BAD: &i should not escape
x := X{p1: &i}
sink = x.p2
}
func field13() {
i := 0 // ERROR "moved to heap: i$"
x := &X{p1: &i} // ERROR "field13 &X literal does not escape$"
sink = x.p1
}
func field14() {
i := 0 // ERROR "moved to heap: i$"
// BAD: &i should not escape
x := &X{p1: &i} // ERROR "field14 &X literal does not escape$"
sink = x.p2
}
func field15() {
i := 0 // ERROR "moved to heap: i$"
x := &X{p1: &i} // ERROR "&X literal escapes to heap$"
sink = x
}
func field16() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i
var iface interface{} = x // ERROR "field16 x does not escape"
x1 := iface.(X)
sink = x1.p2
}
func field17() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i
var iface interface{} = x // ERROR "field17 x does not escape"
x1 := iface.(X)
sink = x1.p1
}
func field18() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i
var iface interface{} = x // ERROR "field18 x does not escape"
y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized.
sink = y // ERROR "y escapes to heap"
}