fix: type assert when status is _
If the status is _, there is no storage allocated in frame, and the status assign operation should be skipped. Fixes #761.
This commit is contained in:
13
_test/interface45.go
Normal file
13
_test/interface45.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
var i interface{} = 1
|
||||
var s struct{}
|
||||
s, _ = i.(struct{})
|
||||
fmt.Println(s)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// {}
|
||||
@@ -230,10 +230,11 @@ func typeAssert(n *node) {
|
||||
|
||||
func typeAssert2(n *node) {
|
||||
c0, c1 := n.child[0], n.child[1]
|
||||
value := genValue(c0) // input value
|
||||
value0 := genValue(n.anc.child[0]) // returned result
|
||||
value1 := genValue(n.anc.child[1]) // returned status
|
||||
typ := c1.typ // type to assert or convert to
|
||||
value := genValue(c0) // input value
|
||||
value0 := genValue(n.anc.child[0]) // returned result
|
||||
value1 := genValue(n.anc.child[1]) // returned status
|
||||
setStatus := n.anc.child[1].ident != "_" // do not assign status to "_"
|
||||
typ := c1.typ // type to assert or convert to
|
||||
typID := typ.id()
|
||||
rtype := typ.rtype // type to assert
|
||||
next := getExec(n.tnext)
|
||||
@@ -247,7 +248,9 @@ func typeAssert2(n *node) {
|
||||
} else {
|
||||
ok = false
|
||||
}
|
||||
value1(f).SetBool(ok)
|
||||
if setStatus {
|
||||
value1(f).SetBool(ok)
|
||||
}
|
||||
return next
|
||||
}
|
||||
case isInterface(typ):
|
||||
@@ -257,7 +260,9 @@ func typeAssert2(n *node) {
|
||||
if ok {
|
||||
value0(f).Set(v)
|
||||
}
|
||||
value1(f).SetBool(ok)
|
||||
if setStatus {
|
||||
value1(f).SetBool(ok)
|
||||
}
|
||||
return next
|
||||
}
|
||||
case n.child[0].typ.cat == valueT:
|
||||
@@ -267,7 +272,9 @@ func typeAssert2(n *node) {
|
||||
if ok {
|
||||
value0(f).Set(v)
|
||||
}
|
||||
value1(f).SetBool(ok)
|
||||
if setStatus {
|
||||
value1(f).SetBool(ok)
|
||||
}
|
||||
return next
|
||||
}
|
||||
default:
|
||||
@@ -277,13 +284,18 @@ func typeAssert2(n *node) {
|
||||
if ok {
|
||||
value0(f).Set(v.value)
|
||||
}
|
||||
value1(f).SetBool(ok)
|
||||
if setStatus {
|
||||
value1(f).SetBool(ok)
|
||||
}
|
||||
return next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func canAssertTypes(src, dest reflect.Type) bool {
|
||||
if dest == nil {
|
||||
return false
|
||||
}
|
||||
if src == dest {
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user