1
0
mirror of https://github.com/golang/go synced 2024-09-24 21:10:12 -06:00
go/test/closure2.go
Robert Griesemer 4a1756824f test/closure2.go: correctly "use" tmp
cmd/go doesn't complain (this is an open issue), but go/types does

Change-Id: I2caec1f7aec991a9500d2c3504c29e4ab718c138
Reviewed-on: https://go-review.googlesource.com/3541
Reviewed-by: Alan Donovan <adonovan@google.com>
2015-01-29 22:22:14 +00:00

66 lines
891 B
Go

// run
// 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.
// Check that these do not use "by value" capturing,
// because changes are made to the value during the closure.
package main
func main() {
type X struct {
v int
}
var x X
func() {
x.v++
}()
if x.v != 1 {
panic("x.v != 1")
}
type Y struct {
X
}
var y Y
func() {
y.v = 1
}()
if y.v != 1 {
panic("y.v != 1")
}
type Z struct {
a [3]byte
}
var z Z
func() {
i := 0
for z.a[1] = 1; i < 10; i++ {
}
}()
if z.a[1] != 1 {
panic("z.a[1] != 1")
}
w := 0
tmp := 0
f := func() {
if w != 1 {
panic("w != 1")
}
}
func() {
tmp = w // force capture of w, but do not write to it yet
_ = tmp
func() {
func() {
w++ // write in a nested closure
}()
}()
}()
f()
}