blob: b67870b12a61d0bd3bfe4fdb9932ca59ea7fb4ba [file] [log] [blame]
Russ Cox2b1c9b42012-02-16 23:49:30 -05001// run
Robert Griesemercc35ca52009-05-05 17:05:58 -07002
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
7package main
8
9type T struct {a, b int};
10
Russ Cox4bdf1fc2011-09-26 19:35:21 -040011func println(x, y int) { }
12
Robert Griesemercc35ca52009-05-05 17:05:58 -070013func 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
25func main() {
26 inner_T := f(nil);
27 f(inner_T);
28
Russ Cox4bdf1fc2011-09-26 19:35:21 -040029 shouldPanic(p1)
30}
31
32func p1() {
Robert Griesemercc35ca52009-05-05 17:05:58 -070033 outer_T := T{5, 7};
34 f(outer_T);
35}
36
Russ Cox4bdf1fc2011-09-26 19:35:21 -040037func shouldPanic(f func()) {
38 defer func() {
39 if recover() == nil {
40 panic("function should panic")
41 }
42 }()
43 f()
44}
45
Robert Griesemercc35ca52009-05-05 17:05:58 -070046/*
47This prints:
48
492 3
505 7
51
Russ Cox4bdf1fc2011-09-26 19:35:21 -040052but it should crash: The type assertion on line 18 should fail
Robert Griesemercc35ca52009-05-05 17:05:58 -070053for the 2nd call to f with outer_T.
54*/