Rémy Oudompheng | 2ece2f5 | 2012-02-18 22:15:42 +0100 | [diff] [blame] | 1 | // run |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 2 | |
| 3 | // Copyright 2009 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 | |
Rob Pike | 501f0b5 | 2012-02-23 18:47:26 +1100 | [diff] [blame] | 7 | // Test various safe uses of indirection. |
| 8 | |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 9 | package main |
| 10 | |
| 11 | var m0 map[string]int |
| 12 | var m1 *map[string]int |
| 13 | var m2 *map[string]int = &m0 |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 14 | var m3 map[string]int = map[string]int{"a": 1} |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 15 | var m4 *map[string]int = &m3 |
| 16 | |
| 17 | var s0 string |
| 18 | var s1 *string |
| 19 | var s2 *string = &s0 |
| 20 | var s3 string = "a" |
| 21 | var s4 *string = &s3 |
| 22 | |
| 23 | var a0 [10]int |
| 24 | var a1 *[10]int |
| 25 | var a2 *[10]int = &a0 |
| 26 | |
| 27 | var b0 []int |
| 28 | var b1 *[]int |
| 29 | var b2 *[]int = &b0 |
Russ Cox | be2edb5 | 2009-03-03 08:39:12 -0800 | [diff] [blame] | 30 | var b3 []int = []int{1, 2, 3} |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 31 | var b4 *[]int = &b3 |
| 32 | |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 33 | func crash() { |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 34 | // these uses of nil pointers |
| 35 | // would crash but should type check |
| 36 | println("crash", |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 37 | len(a1)+cap(a1)) |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Robert Griesemer | 542099d | 2009-12-09 19:27:08 -0800 | [diff] [blame] | 40 | func nocrash() { |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 41 | // this is spaced funny so that |
| 42 | // the compiler will print a different |
| 43 | // line number for each len call if |
| 44 | // it decides there are type errors. |
| 45 | // it might also help in the traceback. |
| 46 | x := |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 47 | len(m0) + |
| 48 | len(m3) |
Russ Cox | 4c5c0f4 | 2009-06-25 14:44:09 -0700 | [diff] [blame] | 49 | if x != 1 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 50 | println("wrong maplen") |
| 51 | panic("fail") |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | x = |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 55 | len(s0) + |
| 56 | len(s3) |
Russ Cox | 4c5c0f4 | 2009-06-25 14:44:09 -0700 | [diff] [blame] | 57 | if x != 1 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 58 | println("wrong stringlen") |
| 59 | panic("fail") |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | x = |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 63 | len(a0) + |
| 64 | len(a2) |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 65 | if x != 20 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 66 | println("wrong arraylen") |
| 67 | panic("fail") |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | x = |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 71 | len(b0) + |
| 72 | len(b3) |
Russ Cox | 4c5c0f4 | 2009-06-25 14:44:09 -0700 | [diff] [blame] | 73 | if x != 3 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 74 | println("wrong slicelen") |
| 75 | panic("fail") |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | x = |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 79 | cap(b0) + |
| 80 | cap(b3) |
Russ Cox | 4c5c0f4 | 2009-06-25 14:44:09 -0700 | [diff] [blame] | 81 | if x != 3 { |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 82 | println("wrong slicecap") |
| 83 | panic("fail") |
Russ Cox | cbd08ed | 2009-01-09 15:38:01 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Rob Pike | 325cf8e | 2010-03-24 16:46:53 -0700 | [diff] [blame] | 87 | func main() { nocrash() } |