github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/test/fixedbugs/bug148.go (about)

     1  // run
     2  
     3  // Copyright 2009 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  type T struct {a, b int};
    10  
    11  func println(x, y int) { }
    12  
    13  func f(x interface{}) interface{} {
    14  	type T struct {a, b int};
    15  
    16  	if x == nil {
    17  		return T{2, 3};
    18  	}
    19  
    20  	t := x.(T);
    21  	println(t.a, t.b);
    22  	return x;
    23  }
    24  
    25  func main() {
    26  	inner_T := f(nil);
    27  	f(inner_T);
    28  
    29  	shouldPanic(p1)
    30  }
    31  
    32  func p1() {
    33  	outer_T := T{5, 7};
    34  	f(outer_T);
    35  }
    36  
    37  func shouldPanic(f func()) {
    38  	defer func() {
    39  		if recover() == nil {
    40  			panic("function should panic")
    41  		}
    42  	}()
    43  	f()
    44  }
    45  
    46  /*
    47  This prints:
    48  
    49  2 3
    50  5 7
    51  
    52  but it should crash: The type assertion on line 18 should fail
    53  for the 2nd call to f with outer_T.
    54  */