2012-02-17 04:51:04 +00:00
|
|
|
// errorcheck
|
2011-11-09 09:58:53 +00:00
|
|
|
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-02-24 00:48:19 +00:00
|
|
|
// Verify that erroneous switch statements are detected by the compiler.
|
|
|
|
// Does not compile.
|
|
|
|
|
2011-11-09 09:58:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
type I interface {
|
2011-11-14 03:58:08 +00:00
|
|
|
M()
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func bad() {
|
|
|
|
var i I
|
|
|
|
var s string
|
|
|
|
|
|
|
|
switch i {
|
2012-01-27 07:06:47 +00:00
|
|
|
case s: // ERROR "mismatched types string and I|incompatible types"
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch s {
|
2012-01-27 07:06:47 +00:00
|
|
|
case i: // ERROR "mismatched types I and string|incompatible types"
|
2011-11-14 03:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var m, m1 map[int]int
|
|
|
|
switch m {
|
|
|
|
case nil:
|
2012-01-27 07:06:47 +00:00
|
|
|
case m1: // ERROR "can only compare map m to nil|map can only be compared to nil"
|
2011-11-14 03:58:08 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var a, a1 []int
|
|
|
|
switch a {
|
|
|
|
case nil:
|
2012-01-27 07:06:47 +00:00
|
|
|
case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil"
|
2011-11-14 03:58:08 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var f, f1 func()
|
|
|
|
switch f {
|
|
|
|
case nil:
|
2012-01-27 07:06:47 +00:00
|
|
|
case f1: // ERROR "can only compare func f to nil|func can only be compared to nil"
|
2011-11-14 03:58:08 +00:00
|
|
|
default:
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
2012-08-03 19:47:26 +00:00
|
|
|
|
|
|
|
var ar, ar1 [4]func()
|
|
|
|
switch ar { // ERROR "cannot switch on"
|
|
|
|
case ar1:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
var st, st1 struct{ f func() }
|
|
|
|
switch st { // ERROR "cannot switch on"
|
|
|
|
case st1:
|
|
|
|
}
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func good() {
|
|
|
|
var i interface{}
|
|
|
|
var s string
|
|
|
|
|
|
|
|
switch i {
|
|
|
|
case s:
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s {
|
|
|
|
case i:
|
|
|
|
}
|
|
|
|
}
|