blob: 0a8a0f09e60deb9c2a1b79e6b32f774d5b76406e [file] [log] [blame]
Rémy Oudompheng2ece2f52012-02-18 22:15:42 +01001// run
Robert Griesemer86c9aca2010-06-03 14:58:00 -07002
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
11package main
12
13type T2 struct {
14 x int
15}
16
17func (t *T2) f() int { return t.x }
18
19func main() {
20 type B bool
21 b := B(false)
22 mb := make(map[B]int)
Russ Coxa457fa52012-02-21 22:54:07 -050023 mb[false] = 42 // this should work: false is assignment compatible with B
Robert Griesemer86c9aca2010-06-03 14:58:00 -070024 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
Robert Griesemer86c9aca2010-06-03 14:58:00 -070048 type C chan int
49 c := make(C)
50 mc := make(map[C]int)
51 mc[nil] = 42
52 mc[c] = 42
53 mc[make(C)] = 42
54
55 type I1 interface{}
56 type I2 interface {
57 f() int
58 }
59 var i0 interface{} = z
60 var i1 I1 = p
61 m0 := make(map[interface{}]int)
62 m1 := make(map[I1]int)
63 m2 := make(map[I2]int)
64 m0[i0] = 42
65 m0[i1] = 42
66 m0[z] = 42 // this should work: z is assignment-compatible with interface{}
67 m0[new(struct {
68 x int
69 })] = 42 // this should work: *struct{x int} is assignment-compatible with interface{}
70 m0[p] = 42 // this should work: p is assignment-compatible with interface{}
71 m0[false] = 42 // this should work: false is assignment-compatible with interface{}
72 m0[17] = 42 // this should work: 17 is assignment-compatible with interface{}
73 m0["foo"] = 42 // this should work: "foo" is assignment-compatible with interface{}
74
75 m1[i0] = 42
76 m1[i1] = 42
77 m1[new(struct {
78 x int
79 })] = 42 // this should work: *struct{x int} is assignment-compatible with I1
80 m1[false] = 42 // this should work: false is assignment-compatible with I1
81 m1[17] = 42 // this should work: 17 is assignment-compatible with I1
82 m1["foo"] = 42 // this should work: "foo" is assignment-compatible with I1
83
Robert Griesemer86c9aca2010-06-03 14:58:00 -070084 m2[new(T2)] = 42 // this should work: *T2 is assignment-compatible with I2
Robert Griesemer86c9aca2010-06-03 14:58:00 -070085}
86
87/*
886g -e bug286.go
89bug286.go:23: invalid map index false - need type B
90bug286.go:80: invalid map index z - need type interface { }
91bug286.go:83: invalid map index new(struct { x int }) - need type interface { }
92bug286.go:84: invalid map index p - need type interface { }
93bug286.go:85: invalid map index false - need type interface { }
94bug286.go:86: invalid map index 17 - need type interface { }
95bug286.go:87: invalid map index "foo" - need type interface { }
96bug286.go:93: invalid map index new(struct { x int }) - need type I1
97bug286.go:94: invalid map index false - need type I1
98bug286.go:95: invalid map index 17 - need type I1
99bug286.go:96: invalid map index "foo" - need type I1
100bug286.go:99: invalid map index new(T2) - need type I2
101bug286.go:100: invalid map index t2 - need type I2
102*/