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 7package main 8 9import "strings" 10 11var X interface{} 12 13type T struct{} 14 15func scopes() { 16 p, ok := recover().(error) 17 if ok && strings.Contains(p.Error(), "different scopes") { 18 return 19 } 20 panic(p) 21} 22 23func F1() { 24 type T struct{} 25 X = T{} 26} 27 28func F2() { 29 type T struct{} 30 defer scopes() 31 _ = X.(T) 32} 33 34func F3() { 35 defer scopes() 36 _ = X.(T) 37} 38 39func F4() { 40 X = T{} 41} 42 43func 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} 50