Russ Cox | 57eb06f | 2012-02-16 23:51:04 -0500 | [diff] [blame] | 1 | // errorcheck |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 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 | |
Rob Pike | 80a9783 | 2012-02-24 11:48:19 +1100 | [diff] [blame] | 7 | // Verify that erroneous switch statements are detected by the compiler. |
| 8 | // Does not compile. |
| 9 | |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 10 | package main |
| 11 | |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 12 | type I interface { |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 13 | M() |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | func bad() { |
| 17 | var i I |
| 18 | var s string |
| 19 | |
| 20 | switch i { |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 21 | case s: // ERROR "mismatched types string and I|incompatible types" |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | switch s { |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 25 | case i: // ERROR "mismatched types I and string|incompatible types" |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | var m, m1 map[int]int |
| 29 | switch m { |
| 30 | case nil: |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 31 | case m1: // ERROR "can only compare map m to nil|map can only be compared to nil" |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 32 | default: |
| 33 | } |
| 34 | |
| 35 | var a, a1 []int |
| 36 | switch a { |
| 37 | case nil: |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 38 | case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil" |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 39 | default: |
| 40 | } |
| 41 | |
| 42 | var f, f1 func() |
| 43 | switch f { |
| 44 | case nil: |
Ian Lance Taylor | d5b7c51 | 2012-01-26 23:06:47 -0800 | [diff] [blame] | 45 | case f1: // ERROR "can only compare func f to nil|func can only be compared to nil" |
Russ Cox | 5bb54b8 | 2011-11-13 22:58:08 -0500 | [diff] [blame] | 46 | default: |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 47 | } |
Rémy Oudompheng | f4f1ba2 | 2012-08-03 21:47:26 +0200 | [diff] [blame] | 48 | |
| 49 | var ar, ar1 [4]func() |
| 50 | switch ar { // ERROR "cannot switch on" |
| 51 | case ar1: |
| 52 | default: |
| 53 | } |
| 54 | |
| 55 | var st, st1 struct{ f func() } |
| 56 | switch st { // ERROR "cannot switch on" |
| 57 | case st1: |
| 58 | } |
Luuk van Dijk | 13e92e4 | 2011-11-09 10:58:53 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func good() { |
| 62 | var i interface{} |
| 63 | var s string |
| 64 | |
| 65 | switch i { |
| 66 | case s: |
| 67 | } |
| 68 | |
| 69 | switch s { |
| 70 | case i: |
| 71 | } |
| 72 | } |