From d168c4f29682e032a14bb8f5ca23af08a6834635 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 7 Mar 2022 02:27:35 -0800 Subject: [PATCH] test: additional generic type switch test coverage None of the current generic type switch test cases exercise type switches where the instantiated case is an interface type. Change-Id: I9272fa61b8dde1fe1a3702d524d4f40253ef19b2 Reviewed-on: https://go-review.googlesource.com/c/go/+/390354 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Gopher Robot Reviewed-by: Cuong Manh Le --- test/typeparam/typeswitch1.go | 2 ++ test/typeparam/typeswitch1.out | 2 ++ test/typeparam/typeswitch2.go | 14 ++++++++------ test/typeparam/typeswitch2.out | 6 ++++-- test/typeparam/typeswitch3.go | 8 ++++++++ test/typeparam/typeswitch3.out | 3 +++ test/typeparam/typeswitch4.go | 8 ++++++++ test/typeparam/typeswitch4.out | 3 +++ 8 files changed, 38 insertions(+), 8 deletions(-) diff --git a/test/typeparam/typeswitch1.go b/test/typeparam/typeswitch1.go index e971779982..a0468d378f 100644 --- a/test/typeparam/typeswitch1.go +++ b/test/typeparam/typeswitch1.go @@ -28,4 +28,6 @@ func main() { f[float64](int8(9)) f[int32](int32(7)) f[int](int32(7)) + f[any](int(10)) + f[interface{ M() }](int(11)) } diff --git a/test/typeparam/typeswitch1.out b/test/typeparam/typeswitch1.out index dc5dfdb761..6b8a33c345 100644 --- a/test/typeparam/typeswitch1.out +++ b/test/typeparam/typeswitch1.out @@ -5,3 +5,5 @@ struct{T,T} other T int32/int16 +T +int diff --git a/test/typeparam/typeswitch2.go b/test/typeparam/typeswitch2.go index b2496fd1c4..286002a830 100644 --- a/test/typeparam/typeswitch2.go +++ b/test/typeparam/typeswitch2.go @@ -6,20 +6,20 @@ package main -import "reflect" +import "fmt" func f[T any](i interface{}) { switch x := i.(type) { case T: - println("T", x) + fmt.Println("T", x) case int: - println("int", x) + fmt.Println("int", x) case int32, int16: - println("int32/int16", reflect.ValueOf(x).Int()) + fmt.Println("int32/int16", x) case struct{ a, b T }: - println("struct{T,T}", x.a, x.b) + fmt.Println("struct{T,T}", x.a, x.b) default: - println("other", reflect.ValueOf(x).Int()) + fmt.Println("other", x) } } func main() { @@ -30,4 +30,6 @@ func main() { f[float64](int8(9)) f[int32](int32(7)) f[int](int32(7)) + f[any](int(10)) + f[interface{ M() }](int(11)) } diff --git a/test/typeparam/typeswitch2.out b/test/typeparam/typeswitch2.out index 85b54e38ae..6d4df54124 100644 --- a/test/typeparam/typeswitch2.out +++ b/test/typeparam/typeswitch2.out @@ -1,7 +1,9 @@ -T +6.000000e+000 +T 6 int 7 int32/int16 8 -struct{T,T} +1.000000e+000 +2.000000e+000 +struct{T,T} 1 2 other 9 T 7 int32/int16 7 +T 10 +int 11 diff --git a/test/typeparam/typeswitch3.go b/test/typeparam/typeswitch3.go index 83d81f37d0..b84fdd02ea 100644 --- a/test/typeparam/typeswitch3.go +++ b/test/typeparam/typeswitch3.go @@ -7,6 +7,10 @@ package main type I interface{ foo() int } +type J interface { + I + bar() +} type myint int @@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) } type myint32 int32 func (x myint32) foo() int { return int(x) } +func (x myint32) bar() {} func f[T I](i I) { switch x := i.(type) { @@ -37,4 +42,7 @@ func main() { f[myint32](myint32(8)) f[myint32](myfloat(7)) f[myint](myint32(9)) + f[I](myint(10)) + f[J](myint(11)) + f[J](myint32(12)) } diff --git a/test/typeparam/typeswitch3.out b/test/typeparam/typeswitch3.out index ed59987e6d..05ed533197 100644 --- a/test/typeparam/typeswitch3.out +++ b/test/typeparam/typeswitch3.out @@ -4,3 +4,6 @@ other 8 T 8 other 7 other 9 +T 10 +myint 11 +T 12 diff --git a/test/typeparam/typeswitch4.go b/test/typeparam/typeswitch4.go index 43a6fc12fc..3fdf552720 100644 --- a/test/typeparam/typeswitch4.go +++ b/test/typeparam/typeswitch4.go @@ -7,6 +7,10 @@ package main type I interface{ foo() int } +type J interface { + I + bar() +} type myint int @@ -19,6 +23,7 @@ func (x myfloat) foo() int { return int(x) } type myint32 int32 func (x myint32) foo() int { return int(x) } +func (x myint32) bar() {} func f[T I](i I) { switch x := i.(type) { @@ -35,4 +40,7 @@ func main() { f[myint32](myint32(9)) f[myint](myint32(10)) f[myint](myfloat(42)) + f[I](myint(10)) + f[J](myint(11)) + f[J](myint32(12)) } diff --git a/test/typeparam/typeswitch4.out b/test/typeparam/typeswitch4.out index d6121d077c..b98f0743c2 100644 --- a/test/typeparam/typeswitch4.out +++ b/test/typeparam/typeswitch4.out @@ -4,3 +4,6 @@ T/myint32 8 T/myint32 9 T/myint32 10 other 42 +T/myint32 10 +other 11 +T/myint32 12