blob: 287e32e71e1786caebf57b120df9c35eff4056eb [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 type switches are caught be the compiler.
8// Issue 2700, among other things.
9// Does not compile.
10
Luuk van Dijk13e92e42011-11-09 10:58:53 +010011package main
12
Luuk van Dijk0e919ff2012-01-24 13:53:00 +010013import (
14 "io"
15)
Luuk van Dijk13e92e42011-11-09 10:58:53 +010016
17type I interface {
Luuk van Dijk0e919ff2012-01-24 13:53:00 +010018 M()
Luuk van Dijk13e92e42011-11-09 10:58:53 +010019}
20
21func main(){
Luuk van Dijk0e919ff2012-01-24 13:53:00 +010022 var x I
23 switch x.(type) {
24 case string: // ERROR "impossible"
25 println("FAIL")
26 }
27
28 // Issue 2700: if the case type is an interface, nothing is impossible
29
30 var r io.Reader
31
32 _, _ = r.(io.Writer)
33
34 switch r.(type) {
35 case io.Writer:
36 }
Russ Cox74ee51e2012-02-06 12:35:29 -050037
38 // Issue 2827.
Ian Lance Taylor6ed800c2012-09-28 08:30:30 -070039 switch _ := r.(type) { // ERROR "invalid variable name _|no new variables"
Russ Cox74ee51e2012-02-06 12:35:29 -050040 }
Luuk van Dijk13e92e42011-11-09 10:58:53 +010041}
Luuk van Dijk0e919ff2012-01-24 13:53:00 +010042
43