blob: 83695a9e88c0299e509b6e6ea326ffd58dba89be [file] [log] [blame]
Russ Cox0b477ef2012-02-16 23:48:57 -05001// errorcheck
Russ Cox7d15eda2011-12-02 12:30:56 -05002
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2011 The Go Authors. All rights reserved.
Russ Cox7d15eda2011-12-02 12:30:56 -05004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
Rob Pikefc0dc042012-02-19 13:19:43 +11007// Verify that illegal composite literals are detected.
8// Does not compile.
9
Russ Cox7d15eda2011-12-02 12:30:56 -050010package main
11
12var m map[int][3]int
Russ Cox7dc9d8c2011-12-02 14:13:12 -050013
Russ Cox7d15eda2011-12-02 12:30:56 -050014func f() [3]int
15
16func fp() *[3]int
Russ Cox7dc9d8c2011-12-02 14:13:12 -050017
Russ Cox7d15eda2011-12-02 12:30:56 -050018var mp map[int]*[3]int
19
20var (
Russ Cox7dc9d8c2011-12-02 14:13:12 -050021 _ = [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
Alberto Donizetti1737aef2017-04-22 15:28:58 +020025 _ = 301[:] // ERROR "cannot slice"
26 _ = 3.1[:] // ERROR "cannot slice"
27 _ = true[:] // ERROR "cannot slice"
28
Russ Cox7d15eda2011-12-02 12:30:56 -050029 // these are okay because they are slicing a pointer to an array
Russ Cox7dc9d8c2011-12-02 14:13:12 -050030 _ = (&[3]int{1, 2, 3})[:]
Russ Cox7d15eda2011-12-02 12:30:56 -050031 _ = mp[0][:]
32 _ = fp()[:]
Russ Cox7dc9d8c2011-12-02 14:13:12 -050033)
34
35type T struct {
36 i int
37 f float64
38 s string
39 next *T
40}
41
Alberto Donizetti1737aef2017-04-22 15:28:58 +020042type TP *T
43type Ti int
44
Russ Cox7dc9d8c2011-12-02 14:13:12 -050045var (
46 _ = &T{0, 0, "", nil} // ok
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080047 _ = &T{i: 0, f: 0, s: "", next: {}} // ERROR "missing type in composite literal|omit types within composite literal"
48 _ = &T{0, 0, "", {}} // ERROR "missing type in composite literal|omit types within composite literal"
Alberto Donizetti1737aef2017-04-22 15:28:58 +020049 _ = TP{i: 0, f: 0, s: "", next: {}} // ERROR "invalid pointer type"
50 _ = &Ti{} // ERROR "invalid pointer type"
Russ Cox7dc9d8c2011-12-02 14:13:12 -050051)
Russ Cox73d109c2015-05-19 16:49:47 -040052
53type M map[T]T
54
55var (
56 _ = M{{i:1}: {i:2}}
57 _ = M{T{i:1}: {i:2}}
58 _ = M{{i:1}: T{i:2}}
59 _ = M{T{i:1}: T{i:2}}
60)
61
62type S struct { s [1]*M1 }
63type M1 map[S]int
64var _ = M1{{s:[1]*M1{&M1{{}:1}}}:2}
65