Skip to content

Commit e76d6a4

Browse files
odeke-emjosharian
authored andcommitted
cmd/compile: add test for non interface type switch
Ensure that we have a test for when the compiler encounters a type switch on a non-interface value. Change-Id: Icb222f986894d0190e1241ca65396b4950e7d14f Reviewed-on: https://go-review.googlesource.com/38661 Reviewed-by: Josh Bleecher Snyder <[email protected]> Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 783f166 commit e76d6a4

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

test/typeswitch3.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,39 @@ type I interface {
1818
M()
1919
}
2020

21-
func main(){
21+
func main() {
2222
var x I
2323
switch x.(type) {
24-
case string: // ERROR "impossible"
24+
case string: // ERROR "impossible"
2525
println("FAIL")
2626
}
27-
27+
2828
// Issue 2700: if the case type is an interface, nothing is impossible
29-
29+
3030
var r io.Reader
31-
31+
3232
_, _ = r.(io.Writer)
33-
33+
3434
switch r.(type) {
3535
case io.Writer:
3636
}
37-
37+
3838
// Issue 2827.
39-
switch _ := r.(type) { // ERROR "invalid variable name _|no new variables"
39+
switch _ := r.(type) { // ERROR "invalid variable name _|no new variables"
4040
}
4141
}
4242

43+
func noninterface() {
44+
var i int
45+
switch i.(type) { // ERROR "cannot type switch on non-interface value"
46+
case string:
47+
case int:
48+
}
4349

50+
type S struct {
51+
name string
52+
}
53+
var s S
54+
switch s.(type) { // ERROR "cannot type switch on non-interface value"
55+
}
56+
}

0 commit comments

Comments
 (0)