blob: 28705e464ef2c20949e7298fe1072a24fe2ab2df [file] [log] [blame]
Russ Cox57eb06f2012-02-16 23:51:04 -05001// errorcheck
Luuk van Dijk13e92e42011-11-09 10:58:53 +01002
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 Pike80a97832012-02-24 11:48:19 +11007// Verify that erroneous switch statements are detected by the compiler.
8// Does not compile.
9
Luuk van Dijk13e92e42011-11-09 10:58:53 +010010package main
11
Luuk van Dijk13e92e42011-11-09 10:58:53 +010012type I interface {
Russ Cox5bb54b82011-11-13 22:58:08 -050013 M()
Luuk van Dijk13e92e42011-11-09 10:58:53 +010014}
15
16func bad() {
17 var i I
18 var s string
19
20 switch i {
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080021 case s: // ERROR "mismatched types string and I|incompatible types"
Luuk van Dijk13e92e42011-11-09 10:58:53 +010022 }
23
24 switch s {
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080025 case i: // ERROR "mismatched types I and string|incompatible types"
Russ Cox5bb54b82011-11-13 22:58:08 -050026 }
27
28 var m, m1 map[int]int
29 switch m {
30 case nil:
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080031 case m1: // ERROR "can only compare map m to nil|map can only be compared to nil"
Russ Cox5bb54b82011-11-13 22:58:08 -050032 default:
33 }
34
35 var a, a1 []int
36 switch a {
37 case nil:
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080038 case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil"
Russ Cox5bb54b82011-11-13 22:58:08 -050039 default:
40 }
41
42 var f, f1 func()
43 switch f {
44 case nil:
Ian Lance Taylord5b7c512012-01-26 23:06:47 -080045 case f1: // ERROR "can only compare func f to nil|func can only be compared to nil"
Russ Cox5bb54b82011-11-13 22:58:08 -050046 default:
Luuk van Dijk13e92e42011-11-09 10:58:53 +010047 }
Rémy Oudomphengf4f1ba22012-08-03 21:47:26 +020048
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 Dijk13e92e42011-11-09 10:58:53 +010059}
60
61func 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}