compiler: do not convert interop types on assertion

This commit is contained in:
Evgeniy Stratonikov 2021-02-05 13:06:42 +03:00
parent 2f6345f2d9
commit 73a75cc27a
2 changed files with 14 additions and 2 deletions

View file

@ -284,3 +284,12 @@ func isInteropPath(s string) bool {
func isNativeHelpersPath(s string) bool { func isNativeHelpersPath(s string) bool {
return strings.HasPrefix(s, interopPrefix+"/native") return strings.HasPrefix(s, interopPrefix+"/native")
} }
// canConvert returns true if type doesn't need to be converted on type assertion.
func canConvert(s string) bool {
if isInteropPath(s) {
s = s[len(interopPrefix):]
return s != "/iterator.Iterator" && s != "/storage.Context"
}
return true
}

View file

@ -1197,8 +1197,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
// not the assertion type. // not the assertion type.
case *ast.TypeAssertExpr: case *ast.TypeAssertExpr:
ast.Walk(c, n.X) ast.Walk(c, n.X)
typ := toNeoType(c.typeOf(n.Type)) goTyp := c.typeOf(n.Type)
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)}) if canConvert(goTyp.String()) {
typ := toNeoType(goTyp)
emit.Instruction(c.prog.BinWriter, opcode.CONVERT, []byte{byte(typ)})
}
return nil return nil
} }
return c return c