Russ Cox | 0b477ef | 2012-02-16 23:48:57 -0500 | [diff] [blame] | 1 | // errorcheck |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [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 | fc0dc04 | 2012-02-19 13:19:43 +1100 | [diff] [blame] | 7 | // Verify simple assignment errors are caught by the compiler. |
| 8 | // Does not compile. |
| 9 | |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 10 | package main |
| 11 | |
| 12 | import "sync" |
| 13 | |
| 14 | type T struct { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 15 | int |
| 16 | sync.Mutex |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | func main() { |
| 20 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 21 | var x, y sync.Mutex |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 22 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 23 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 24 | } |
| 25 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 26 | var x, y T |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 27 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 28 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 29 | } |
| 30 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 31 | var x, y [2]sync.Mutex |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 32 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 33 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 34 | } |
| 35 | { |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 36 | var x, y [2]T |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 37 | x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 38 | _ = x |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 39 | } |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 40 | { |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 41 | x := sync.Mutex{0, 0} // ERROR "assignment.*Mutex" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 42 | _ = x |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 43 | } |
| 44 | { |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 45 | x := sync.Mutex{key: 0} // ERROR "(unknown|assignment).*Mutex" |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 46 | _ = x |
Russ Cox | a338231 | 2009-11-15 12:57:09 -0800 | [diff] [blame] | 47 | } |
Russ Cox | 9da6666 | 2009-12-03 22:09:58 -0800 | [diff] [blame] | 48 | { |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 49 | x := &sync.Mutex{} // ok |
| 50 | var y sync.Mutex // ok |
| 51 | y = *x // ok |
| 52 | *x = y // ok |
Rob Pike | 4f61fc9 | 2010-09-04 10:36:13 +1000 | [diff] [blame] | 53 | _ = x |
| 54 | _ = y |
Russ Cox | d03611f | 2011-11-15 12:20:59 -0500 | [diff] [blame] | 55 | } |
Evan Kroske | 55df81d | 2014-10-06 17:16:39 -0400 | [diff] [blame] | 56 | { |
| 57 | var x = 1 |
| 58 | { |
| 59 | x, x := 2, 3 // ERROR "x repeated on left side of :=" |
| 60 | _ = x |
| 61 | } |
| 62 | _ = x |
| 63 | } |
| 64 | { |
| 65 | a, a := 1, 2 // ERROR "a repeated on left side of :=" |
| 66 | _ = a |
| 67 | } |
Russ Cox | 8b6b380 | 2009-05-21 14:06:24 -0700 | [diff] [blame] | 68 | } |