Source file test/fixedbugs/bug285.go

     1  // run
     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: https://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 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  
    84  	m2[new(T2)] = 42 // this should work: *T2 is assignment-compatible with I2
    85  }
    86  
    87  /*
    88  6g -e bug286.go
    89  bug286.go:23: invalid map index false - need type B
    90  bug286.go:80: invalid map index z - need type interface { }
    91  bug286.go:83: invalid map index new(struct { x int }) - need type interface { }
    92  bug286.go:84: invalid map index p - need type interface { }
    93  bug286.go:85: invalid map index false - need type interface { }
    94  bug286.go:86: invalid map index 17 - need type interface { }
    95  bug286.go:87: invalid map index "foo" - need type interface { }
    96  bug286.go:93: invalid map index new(struct { x int }) - need type I1
    97  bug286.go:94: invalid map index false - need type I1
    98  bug286.go:95: invalid map index 17 - need type I1
    99  bug286.go:96: invalid map index "foo" - need type I1
   100  bug286.go:99: invalid map index new(T2) - need type I2
   101  bug286.go:100: invalid map index t2 - need type I2
   102  */
   103  

View as plain text