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 type switches are caught be the compiler.
|
|
|
|
// Issue 2700, among other things.
|
|
|
|
// Does not compile.
|
|
|
|
|
2011-11-09 09:58:53 +00:00
|
|
|
package main
|
|
|
|
|
2012-01-24 12:53:00 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
2011-11-09 09:58:53 +00:00
|
|
|
|
|
|
|
type I interface {
|
2012-01-24 12:53:00 +00:00
|
|
|
M()
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
|
|
|
|
2017-03-25 22:17:59 +00:00
|
|
|
func main() {
|
2012-01-24 12:53:00 +00:00
|
|
|
var x I
|
|
|
|
switch x.(type) {
|
2017-03-25 22:17:59 +00:00
|
|
|
case string: // ERROR "impossible"
|
2012-01-24 12:53:00 +00:00
|
|
|
println("FAIL")
|
|
|
|
}
|
2017-03-25 22:17:59 +00:00
|
|
|
|
2012-01-24 12:53:00 +00:00
|
|
|
// Issue 2700: if the case type is an interface, nothing is impossible
|
2017-03-25 22:17:59 +00:00
|
|
|
|
2012-01-24 12:53:00 +00:00
|
|
|
var r io.Reader
|
2017-03-25 22:17:59 +00:00
|
|
|
|
2012-01-24 12:53:00 +00:00
|
|
|
_, _ = r.(io.Writer)
|
2017-03-25 22:17:59 +00:00
|
|
|
|
2012-01-24 12:53:00 +00:00
|
|
|
switch r.(type) {
|
|
|
|
case io.Writer:
|
|
|
|
}
|
2017-03-25 22:17:59 +00:00
|
|
|
|
2012-02-06 17:35:29 +00:00
|
|
|
// Issue 2827.
|
2017-03-25 22:17:59 +00:00
|
|
|
switch _ := r.(type) { // ERROR "invalid variable name _|no new variables"
|
2012-02-06 17:35:29 +00:00
|
|
|
}
|
2011-11-09 09:58:53 +00:00
|
|
|
}
|
2012-01-24 12:53:00 +00:00
|
|
|
|
2017-03-25 22:17:59 +00:00
|
|
|
func noninterface() {
|
|
|
|
var i int
|
|
|
|
switch i.(type) { // ERROR "cannot type switch on non-interface value"
|
|
|
|
case string:
|
|
|
|
case int:
|
|
|
|
}
|
2012-01-24 12:53:00 +00:00
|
|
|
|
2017-03-25 22:17:59 +00:00
|
|
|
type S struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
var s S
|
|
|
|
switch s.(type) { // ERROR "cannot type switch on non-interface value"
|
|
|
|
}
|
|
|
|
}
|