Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2011 The Go Authors. All rights reserved. |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
Rob Pike | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify that illegal composite literals are detected. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 10 | package main |
| 11 | |
| 12 | var m map[int][3]int |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 13 | |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 14 | func f() [3]int |
| 15 | |
| 16 | func fp() *[3]int |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 17 | |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 18 | var mp map[int]*[3]int |
| 19 | |
| 20 | var ( |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 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 | |
Alberto Donizetti | 1737aef | 2017-04-22 15:28:58 +0200 | [diff] [blame] | 25 | _ = 301[:] // ERROR "cannot slice" |
| 26 | _ = 3.1[:] // ERROR "cannot slice" |
| 27 | _ = true[:] // ERROR "cannot slice" |
| 28 | |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 29 | // these are okay because they are slicing a pointer to an array |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 30 | _ = (&[3]int{1, 2, 3})[:] |
Russ Cox | 7d15eda | 2011-12-02 12:30:56 -0500 | [diff] [blame] | 31 | _ = mp[0][:] |
| 32 | _ = fp()[:] |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type T struct { |
| 36 | i int |
| 37 | f float64 |
| 38 | s string |
| 39 | next *T |
| 40 | } |
| 41 | |
Alberto Donizetti | 1737aef | 2017-04-22 15:28:58 +0200 | [diff] [blame] | 42 | type TP *T |
| 43 | type Ti int |
| 44 | |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 45 | var ( |
| 46 | _ = &T{0, 0, "", nil} // ok |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 47 | _ = &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 Donizetti | 1737aef | 2017-04-22 15:28:58 +0200 | [diff] [blame] | 49 | _ = TP{i: 0, f: 0, s: "", next: {}} // ERROR "invalid pointer type" |
| 50 | _ = &Ti{} // ERROR "invalid pointer type" |
Russ Cox | 7dc9d8c | 2011-12-02 14:13:12 -0500 | [diff] [blame] | 51 | ) |
Russ Cox | 73d109c | 2015-05-19 16:49:47 -0400 | [diff] [blame] | 52 | |
| 53 | type M map[T]T |
| 54 | |
| 55 | var ( |
| 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 | |
| 62 | type S struct { s [1]*M1 } |
| 63 | type M1 map[S]int |
| 64 | var _ = M1{{s:[1]*M1{&M1{{}:1}}}:2} |
| 65 | |