Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(interp): convert bin interface to src interface. #1562

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,58 @@ func typeAssert(n *node, withResult, withOk bool) {
value1(f).SetBool(ok)
}()
}

// assert `valf`(bin reflect.Value) as `typ`(src interface) then assign to (value0(f), value1(f))
if !ok {
for valf.Kind() == reflect.Interface {
if valf.IsNil() {
// like 'var err error', there is no elem the interface pointed to.
return next
}
valf = valf.Elem()
}

m1 := typ.methods()
if valf.NumMethod() < len(m1) {
if !withOk {
panic(n.cfgErrorf("interface conversion: %v is not %v", v.node.typ.id(), typID))
}
return next
}

ok = true
// compare all method by string
typf := valf.Type()
for i := 0; i < valf.NumMethod(); i++ {
method := typf.Method(i)
fnDeclare := valf.Method(i).Type().String()
if sign, existed := m1[method.Name]; existed {
if sign != fnDeclare {
if !withOk {
panic(n.cfgErrorf("interface conversion: wanted %s but %s", sign, method.Type.String()))
}
ok = false
}
delete(m1, method.Name)
}
}
if len(m1) > 0 {
if !withOk {
panic(n.cfgErrorf("interface conversion: method is not implemented %v", m1))
}
ok = false
}
if ok {
value0(f).Set(reflect.ValueOf(valueInterface{
node: typ.node,
value: valf,
}))
}
return next
}

if !ok {
fmt.Printf("assertion from bin interface %s to src interface %s is not allowed\n", valf.Type(), typID)
if !withOk {
panic(n.cfgErrorf("interface conversion: nil is not %v", typID))
}
Expand Down