Home | History | Annotate | Download | only in test
      1 // errorcheck
      2 
      3 // Copyright 2011 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 // Verify that illegal composite literals are detected.
      8 // Does not compile.
      9 
     10 package main
     11 
     12 var m map[int][3]int
     13 
     14 func f() [3]int
     15 
     16 func fp() *[3]int
     17 
     18 var mp map[int]*[3]int
     19 
     20 var (
     21 	_ = [3]int{1, 2, 3}[:] // ERROR "slice of unaddressable value"
     22 	_ = m[0][:]            // ERROR "slice of unaddressable value"
     23 	_ = f()[:]             // ERROR "slice of unaddressable value"
     24 
     25 	// these are okay because they are slicing a pointer to an array
     26 	_ = (&[3]int{1, 2, 3})[:]
     27 	_ = mp[0][:]
     28 	_ = fp()[:]
     29 )
     30 
     31 type T struct {
     32 	i    int
     33 	f    float64
     34 	s    string
     35 	next *T
     36 }
     37 
     38 var (
     39 	_ = &T{0, 0, "", nil}               // ok
     40 	_ = &T{i: 0, f: 0, s: "", next: {}} // ERROR "missing type in composite literal|omit types within composite literal"
     41 	_ = &T{0, 0, "", {}}                // ERROR "missing type in composite literal|omit types within composite literal"
     42 )
     43 
     44 type M map[T]T
     45 
     46 var (
     47 	_ = M{{i:1}: {i:2}}
     48 	_ = M{T{i:1}: {i:2}}
     49 	_ = M{{i:1}: T{i:2}}
     50 	_ = M{T{i:1}: T{i:2}}
     51 )
     52 
     53 type S struct { s [1]*M1 }
     54 type M1 map[S]int
     55 var _ = M1{{s:[1]*M1{&M1{{}:1}}}:2}
     56 
     57