OSDN Git Service

Add Go frontend, libgo library, and Go testsuite.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / go.test / test / fixedbugs / bug285.go
1 // $G $D/$F.go && $L $F.$A && ./$A.out || echo BUG: bug285
2
3 // Copyright 2010 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 // Test for issue 778: Map key values that are assignment
8 // compatible with the map key type must be accepted according
9 // to the spec: http://golang.org/doc/go_spec.html#Indexes .
10
11 package main
12
13 type T2 struct {
14         x int
15 }
16
17 func (t *T2) f() int { return t.x }
18
19 func main() {
20         type B bool
21         b := B(false)
22         mb := make(map[B]int)
23         mb[false] = 42 // this should work: false is assignment compatible with B
24         mb[b] = 42
25
26         type Z int
27         z := Z(0)
28         mz := make(map[Z]int)
29         mz[0] = 42
30         mz[z] = 42
31
32         type S string
33         s := S("foo")
34         ms := make(map[S]int)
35         ms["foo"] = 42
36         ms[s] = 42
37
38         type T struct {
39                 x int
40         }
41         type P *T
42         p := P(nil)
43         mp := make(map[P]int)
44         mp[nil] = 42
45         mp[p] = 42
46         mp[&T{7}] = 42
47
48         type F func(x int)
49         f := func(x int) {}
50         mf := make(map[F]int)
51         mf[nil] = 42
52         mf[f] = 42
53         mf[func(x int) {}] = 42
54
55         type M map[int]int
56         m := make(M)
57         mm := make(map[M]int)
58         mm[nil] = 42
59         mm[m] = 42
60         mm[make(M)] = 42
61
62         type C chan int
63         c := make(C)
64         mc := make(map[C]int)
65         mc[nil] = 42
66         mc[c] = 42
67         mc[make(C)] = 42
68
69         type I1 interface{}
70         type I2 interface {
71                 f() int
72         }
73         var i0 interface{} = z
74         var i1 I1 = p
75         m0 := make(map[interface{}]int)
76         m1 := make(map[I1]int)
77         m2 := make(map[I2]int)
78         m0[i0] = 42
79         m0[i1] = 42
80         m0[z] = 42 // this should work: z is assignment-compatible with interface{}
81         m0[new(struct {
82                 x int
83         })] = 42       // this should work: *struct{x int} is assignment-compatible with interface{}
84         m0[p] = 42     // this should work: p is assignment-compatible with interface{}
85         m0[false] = 42 // this should work: false is assignment-compatible with interface{}
86         m0[17] = 42    // this should work: 17 is assignment-compatible with interface{}
87         m0["foo"] = 42 // this should work: "foo" is assignment-compatible with interface{}
88
89         m1[i0] = 42
90         m1[i1] = 42
91         m1[new(struct {
92                 x int
93         })] = 42       // this should work: *struct{x int} is assignment-compatible with I1
94         m1[false] = 42 // this should work: false is assignment-compatible with I1
95         m1[17] = 42    // this should work: 17 is assignment-compatible with I1
96         m1["foo"] = 42 // this should work: "foo" is assignment-compatible with I1
97
98         m2[new(T2)] = 42 // this should work: *T2 is assignment-compatible with I2
99 }
100
101 /*
102 6g -e bug286.go
103 bug286.go:23: invalid map index false - need type B
104 bug286.go:80: invalid map index z - need type interface { }
105 bug286.go:83: invalid map index new(struct { x int }) - need type interface { }
106 bug286.go:84: invalid map index p - need type interface { }
107 bug286.go:85: invalid map index false - need type interface { }
108 bug286.go:86: invalid map index 17 - need type interface { }
109 bug286.go:87: invalid map index "foo" - need type interface { }
110 bug286.go:93: invalid map index new(struct { x int }) - need type I1
111 bug286.go:94: invalid map index false - need type I1
112 bug286.go:95: invalid map index 17 - need type I1
113 bug286.go:96: invalid map index "foo" - need type I1
114 bug286.go:99: invalid map index new(T2) - need type I2
115 bug286.go:100: invalid map index t2 - need type I2
116 */