2012-02-16 23:51:04 -05:00
|
|
|
// errorcheck
|
2011-11-09 10:58:53 +01: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.
|
|
|
|
|
2018-10-03 23:52:49 +00:00
|
|
|
// Verify that erroneous type switches are caught by the compiler.
|
2012-02-24 11:48:19 +11:00
|
|
|
// Issue 2700, among other things.
|
|
|
|
// Does not compile.
|
|
|
|
|
2011-11-09 10:58:53 +01:00
|
|
|
package main
|
|
|
|
|
2012-01-24 13:53:00 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
2011-11-09 10:58:53 +01:00
|
|
|
|
|
|
|
type I interface {
|
2012-01-24 13:53:00 +01:00
|
|
|
M()
|
2011-11-09 10:58:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-25 16:17:59 -06:00
|
|
|
func main() {
|
2012-01-24 13:53:00 +01:00
|
|
|
var x I
|
|
|
|
switch x.(type) {
|
2017-03-25 16:17:59 -06:00
|
|
|
case string: // ERROR "impossible"
|
2012-01-24 13:53:00 +01:00
|
|
|
println("FAIL")
|
|
|
|
}
|
2017-03-25 16:17:59 -06:00
|
|
|
|
2012-01-24 13:53:00 +01:00
|
|
|
// Issue 2700: if the case type is an interface, nothing is impossible
|
2017-03-25 16:17:59 -06:00
|
|
|
|
2012-01-24 13:53:00 +01:00
|
|
|
var r io.Reader
|
2017-03-25 16:17:59 -06:00
|
|
|
|
2012-01-24 13:53:00 +01:00
|
|
|
_, _ = r.(io.Writer)
|
2017-03-25 16:17:59 -06:00
|
|
|
|
2012-01-24 13:53:00 +01:00
|
|
|
switch r.(type) {
|
|
|
|
case io.Writer:
|
|
|
|
}
|
2017-03-25 16:17:59 -06:00
|
|
|
|
2012-02-06 12:35:29 -05:00
|
|
|
// Issue 2827.
|
2020-12-03 18:15:50 -08:00
|
|
|
switch _ := r.(type) { // ERROR "invalid variable name _|no new variables?"
|
2012-02-06 12:35:29 -05:00
|
|
|
}
|
2011-11-09 10:58:53 +01:00
|
|
|
}
|
2012-01-24 13:53:00 +01:00
|
|
|
|
2017-03-25 16:17:59 -06:00
|
|
|
func noninterface() {
|
|
|
|
var i int
|
2021-11-11 16:32:16 -08:00
|
|
|
switch i.(type) { // ERROR "cannot type switch on non-interface value|not an interface"
|
2017-03-25 16:17:59 -06:00
|
|
|
case string:
|
|
|
|
case int:
|
|
|
|
}
|
2012-01-24 13:53:00 +01:00
|
|
|
|
2017-03-25 16:17:59 -06:00
|
|
|
type S struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
var s S
|
2021-11-11 16:32:16 -08:00
|
|
|
switch s.(type) { // ERROR "cannot type switch on non-interface value|not an interface"
|
2017-03-25 16:17:59 -06:00
|
|
|
}
|
|
|
|
}
|