| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import "strings" |
| 10 |
|
| 11 |
var X interface{} |
| 12 |
|
| 13 |
type T struct{} |
| 14 |
|
| 15 |
func scopes() { |
| 16 |
p, ok := recover().(error) |
| 17 |
if ok && strings.Contains(p.Error(), "different scopes") { |
| 18 |
return |
| 19 |
} |
| 20 |
panic(p) |
| 21 |
} |
| 22 |
|
| 23 |
func F1() { |
| 24 |
type T struct{} |
| 25 |
X = T{} |
| 26 |
} |
| 27 |
|
| 28 |
func F2() { |
| 29 |
type T struct{} |
| 30 |
defer scopes() |
| 31 |
_ = X.(T) |
| 32 |
} |
| 33 |
|
| 34 |
func F3() { |
| 35 |
defer scopes() |
| 36 |
_ = X.(T) |
| 37 |
} |
| 38 |
|
| 39 |
func F4() { |
| 40 |
X = T{} |
| 41 |
} |
| 42 |
|
| 43 |
func main() { |
| 44 |
F1() // set X to F1's T |
| 45 |
F2() // check that X is not F2's T |
| 46 |
F3() // check that X is not package T |
| 47 |
F4() // set X to package T |
| 48 |
F2() // check that X is not F2's T |
| 49 |
} |