1
0
mirror of https://github.com/golang/go synced 2024-11-12 06:40:22 -07:00
go/test/escape_field.go
Emmanuel Odeke 53fd522c0d all: make copyright headers consistent with one space after period
Follows suit with https://go-review.googlesource.com/#/c/20111.

Generated by running
$ grep -R 'Go Authors.  All' * | cut -d":" -f1 | while read F;do perl -pi -e 's/Go
Authors.  All/Go Authors. All/g' $F;done

The code in cmd/internal/unvendor wasn't changed.

Fixes #15213

Change-Id: I4f235cee0a62ec435f9e8540a1ec08ae03b1a75f
Reviewed-on: https://go-review.googlesource.com/21819
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-05-02 13:43:18 +00:00

175 lines
4.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 // ERROR "&i escapes to heap$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
}
func field1() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
func field3() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i // ERROR "&i escapes to heap$"
sink = x // ERROR "x escapes to heap"
}
func field4() {
i := 0 // ERROR "moved to heap: i$"
var y Y
y.x.p1 = &i // ERROR "&i escapes to heap$"
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 // ERROR "&i escapes to heap$"
sink = x.a[1] // ERROR "x\.a\[1\] escapes to heap"
}
// BAD: we are not leaking param x, only x.p2
func field6(x *X) { // ERROR "leaking param content: x$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
func field6a() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
field6(&x) // ERROR "field6a &x does not escape"
}
func field7() {
i := 0
var y Y
y.x.p1 = &i // ERROR "field7 &i does not escape$"
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 // ERROR "&i escapes to heap$"
x := y.x
var y1 Y
y1.x = x
sink = y1.x.p1 // ERROR "y1\.x\.p1 escapes to heap"
}
func field9() {
i := 0 // ERROR "moved to heap: i$"
var y Y
y.x.p1 = &i // ERROR "&i escapes to heap$"
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 // ERROR "&i escapes to heap$"
x := y.x
var y1 Y
y1.x = x
sink = y1.x.p2 // ERROR "y1\.x\.p2 escapes to heap"
}
func field11() {
i := 0 // ERROR "moved to heap: i$"
x := X{p1: &i} // ERROR "&i escapes to heap$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
}
func field12() {
i := 0 // ERROR "moved to heap: i$"
// BAD: &i should not escape
x := X{p1: &i} // ERROR "&i escapes to heap$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
func field13() {
i := 0 // ERROR "moved to heap: i$"
x := &X{p1: &i} // ERROR "&i escapes to heap$" "field13 &X literal does not escape$"
sink = x.p1 // ERROR "x\.p1 escapes to heap"
}
func field14() {
i := 0 // ERROR "moved to heap: i$"
// BAD: &i should not escape
x := &X{p1: &i} // ERROR "&i escapes to heap$" "field14 &X literal does not escape$"
sink = x.p2 // ERROR "x\.p2 escapes to heap"
}
func field15() {
i := 0 // ERROR "moved to heap: i$"
x := &X{p1: &i} // ERROR "&X literal escapes to heap$" "&i escapes to heap$"
sink = x // ERROR "x escapes to heap"
}
func field16() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
var iface interface{} = x // ERROR "x escapes to heap"
x1 := iface.(X)
sink = x1.p2 // ERROR "x1\.p2 escapes to heap"
}
func field17() {
i := 0 // ERROR "moved to heap: i$"
var x X
x.p1 = &i // ERROR "&i escapes to heap$"
var iface interface{} = x // ERROR "x escapes to heap"
x1 := iface.(X)
sink = x1.p1 // ERROR "x1\.p1 escapes to heap"
}
func field18() {
i := 0 // ERROR "moved to heap: i$"
var x X
// BAD: &i should not escape
x.p1 = &i // ERROR "&i escapes to heap$"
var iface interface{} = x // ERROR "x escapes to heap"
y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized.
sink = y // ERROR "y escapes to heap"
}