2013-05-17 14:20:39 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
|
|
|
// This file contains tests for the rangeloop checker.
|
|
|
|
|
2013-05-22 11:20:50 -06:00
|
|
|
package testdata
|
2013-05-17 14:20:39 -06:00
|
|
|
|
|
|
|
func RangeLoopTests() {
|
|
|
|
var s []int
|
|
|
|
for i, v := range s {
|
|
|
|
go func() {
|
2014-08-24 18:26:18 -06:00
|
|
|
println(i) // ERROR "range variable i captured by func literal"
|
|
|
|
println(v) // ERROR "range variable v captured by func literal"
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i, v := range s {
|
|
|
|
defer func() {
|
2014-08-24 18:26:18 -06:00
|
|
|
println(i) // ERROR "range variable i captured by func literal"
|
|
|
|
println(v) // ERROR "range variable v captured by func literal"
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i := range s {
|
|
|
|
go func() {
|
2014-08-24 18:26:18 -06:00
|
|
|
println(i) // ERROR "range variable i captured by func literal"
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
for _, v := range s {
|
|
|
|
go func() {
|
2014-08-24 18:26:18 -06:00
|
|
|
println(v) // ERROR "range variable v captured by func literal"
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
for i, v := range s {
|
|
|
|
go func() {
|
|
|
|
println(i, v)
|
|
|
|
}()
|
|
|
|
println("unfortunately, we don't catch the error above because of this statement")
|
|
|
|
}
|
|
|
|
for i, v := range s {
|
|
|
|
go func(i, v int) {
|
|
|
|
println(i, v)
|
|
|
|
}(i, v)
|
|
|
|
}
|
|
|
|
for i, v := range s {
|
|
|
|
i, v := i, v
|
|
|
|
go func() {
|
|
|
|
println(i, v)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// If the key of the range statement is not an identifier
|
|
|
|
// the code should not panic (it used to).
|
|
|
|
var x [2]int
|
|
|
|
var f int
|
|
|
|
for x[0], f = range s {
|
|
|
|
go func() {
|
2014-08-24 18:26:18 -06:00
|
|
|
_ = f // ERROR "range variable f captured by func literal"
|
2013-05-17 14:20:39 -06:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|