interp: fix and refactor typeAssertStatus in

typeAssertStatus deals with the 3rd form of type assertion ("_, ok"), for
when one does not care about the result of the assertion itself.
Some cases for it, which are already fixed for the two other forms of
type assertions, had not been fixed for this form yet.

Therefore, this change fixes such cases for this form, while integrating
typeAssertStatus to the same code path as for the two other forms.
This commit is contained in:
mpl
2020-12-07 15:58:04 +01:00
committed by GitHub
parent 92d65c22f0
commit 662838fd80
4 changed files with 92 additions and 58 deletions

View File

@@ -48,6 +48,14 @@ func main() {
bType := reflect.TypeOf(TestStruct{})
fmt.Println(bType.Implements(aType))
// not redundant with the above, because it goes through a slightly different code path.
if _, ok := t.(MyWriter); !ok {
fmt.Println("TestStruct does not implement MyWriter")
return
} else {
fmt.Println("TestStruct implements MyWriter")
}
t = 42
foo, ok := t.(MyWriter)
if !ok {
@@ -57,6 +65,12 @@ func main() {
}
_ = foo
if _, ok := t.(MyWriter); !ok {
fmt.Println("42 does not implement MyWriter")
} else {
fmt.Println("42 implements MyWriter")
}
var tt interface{}
tt = time.Nanosecond
var myD MyStringer
@@ -72,6 +86,12 @@ func main() {
dType := reflect.TypeOf(time.Nanosecond)
fmt.Println(dType.Implements(cType))
if _, ok := tt.(MyStringer); !ok {
fmt.Println("time.Nanosecond does not implement MyStringer")
} else {
fmt.Println("time.Nanosecond implements MyStringer")
}
tt = 42
bar, ok := tt.(MyStringer)
if !ok {
@@ -81,6 +101,11 @@ func main() {
}
_ = bar
if _, ok := tt.(MyStringer); !ok {
fmt.Println("42 does not implement MyStringer")
} else {
fmt.Println("42 implements MyStringer")
}
}
// Output:
@@ -88,9 +113,13 @@ func main() {
// 11
// 11
// true
// TestStruct implements MyWriter
// 42 does not implement MyWriter
// 42 does not implement MyWriter
// time.Nanosecond implements MyStringer
// 1ns
// 1ns
// true
// time.Nanosecond implements MyStringer
// 42 does not implement MyStringer
// 42 does not implement MyStringer