OSDN Git Service

compiler: Prohibit comparisons of funcs, maps, and slices to non-nil.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / typeswitch1.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out
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 import "fmt"
10
11 const (
12         a = iota
13         b
14         c
15         d
16         e
17 )
18
19 var x = []int{1, 2, 3}
20
21 func f(x int, len *byte) {
22         *len = byte(x)
23 }
24
25 func whatis(x interface{}) string {
26         switch xx := x.(type) {
27         default:
28                 return fmt.Sprint("default ", xx)
29         case int, int8, int16, int32:
30                 return fmt.Sprint("signed ", xx)
31         case int64:
32                 return fmt.Sprint("signed64 ", int64(xx))
33         case uint, uint8, uint16, uint32:
34                 return fmt.Sprint("unsigned ", xx)
35         case uint64:
36                 return fmt.Sprint("unsigned64 ", uint64(xx))
37         case nil:
38                 return fmt.Sprint("nil ", xx)
39         }
40         panic("not reached")
41 }
42
43 func whatis1(x interface{}) string {
44         xx := x
45         switch xx.(type) {
46         default:
47                 return fmt.Sprint("default ", xx)
48         case int, int8, int16, int32:
49                 return fmt.Sprint("signed ", xx)
50         case int64:
51                 return fmt.Sprint("signed64 ", xx.(int64))
52         case uint, uint8, uint16, uint32:
53                 return fmt.Sprint("unsigned ", xx)
54         case uint64:
55                 return fmt.Sprint("unsigned64 ", xx.(uint64))
56         case nil:
57                 return fmt.Sprint("nil ", xx)
58         }
59         panic("not reached")
60 }
61
62 func check(x interface{}, s string) {
63         w := whatis(x)
64         if w != s {
65                 fmt.Println("whatis", x, "=>", w, "!=", s)
66                 panic("fail")
67         }
68
69         w = whatis1(x)
70         if w != s {
71                 fmt.Println("whatis1", x, "=>", w, "!=", s)
72                 panic("fail")
73         }
74 }
75
76 func main() {
77         check(1, "signed 1")
78         check(uint(1), "unsigned 1")
79         check(int64(1), "signed64 1")
80         check(uint64(1), "unsigned64 1")
81         check(1.5, "default 1.5")
82         check(nil, "nil <nil>")
83 }