blob: 4ca07dc65a72da31cbf1c206658834f1de489d70 [file] [log] [blame]
Russ Cox4bdf1fc2011-09-26 19:35:21 -04001// $G $D/$F.go && $L $F.$A && ./$A.out
Ian Lance Taylor22a67472008-10-24 22:40:32 -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
Rob Pike325cf8e2010-03-24 16:46:53 -07008
9type I interface{}
10
11func foo1(i int) int { return i }
Ian Lance Taylor22a67472008-10-24 22:40:32 -070012func foo2(i int32) int32 { return i }
13func main() {
Rob Pike325cf8e2010-03-24 16:46:53 -070014 var i I
15 i = 1
16 var v1 = i.(int)
17 if foo1(v1) != 1 {
18 panic(1)
19 }
20 var v2 = int32(i.(int))
21 if foo2(v2) != 1 {
22 panic(2)
23 }
Russ Cox4bdf1fc2011-09-26 19:35:21 -040024
25 shouldPanic(p1)
26}
27
28func p1() {
29 var i I
30 i = 1
Rob Pike325cf8e2010-03-24 16:46:53 -070031 var v3 = i.(int32) // This type conversion should fail at runtime.
32 if foo2(v3) != 1 {
33 panic(3)
34 }
Ian Lance Taylor22a67472008-10-24 22:40:32 -070035}
Russ Cox4bdf1fc2011-09-26 19:35:21 -040036
37func shouldPanic(f func()) {
38 defer func() {
39 if recover() == nil {
40 panic("function should panic")
41 }
42 }()
43 f()
44}