Russ Cox | 2b1c9b4 | 2012-02-16 23:49:30 -0500 | [diff] [blame] | 1 | // run |
Robert Griesemer | cc35ca5 | 2009-05-05 17:05:58 -0700 | [diff] [blame] | 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 | |
Russ Cox | 4bdf1fc | 2011-09-26 19:35:21 -0400 | [diff] [blame] | 11 | func println(x, y int) { } |
| 12 | |
Robert Griesemer | cc35ca5 | 2009-05-05 17:05:58 -0700 | [diff] [blame] | 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 | |
Russ Cox | 4bdf1fc | 2011-09-26 19:35:21 -0400 | [diff] [blame] | 29 | shouldPanic(p1) |
| 30 | } |
| 31 | |
| 32 | func p1() { |
Robert Griesemer | cc35ca5 | 2009-05-05 17:05:58 -0700 | [diff] [blame] | 33 | outer_T := T{5, 7}; |
| 34 | f(outer_T); |
| 35 | } |
| 36 | |
Russ Cox | 4bdf1fc | 2011-09-26 19:35:21 -0400 | [diff] [blame] | 37 | func shouldPanic(f func()) { |
| 38 | defer func() { |
| 39 | if recover() == nil { |
| 40 | panic("function should panic") |
| 41 | } |
| 42 | }() |
| 43 | f() |
| 44 | } |
| 45 | |
Robert Griesemer | cc35ca5 | 2009-05-05 17:05:58 -0700 | [diff] [blame] | 46 | /* |
| 47 | This prints: |
| 48 | |
| 49 | 2 3 |
| 50 | 5 7 |
| 51 | |
Russ Cox | 4bdf1fc | 2011-09-26 19:35:21 -0400 | [diff] [blame] | 52 | but it should crash: The type assertion on line 18 should fail |
Robert Griesemer | cc35ca5 | 2009-05-05 17:05:58 -0700 | [diff] [blame] | 53 | for the 2nd call to f with outer_T. |
| 54 | */ |