diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 7e838946c..585aafa19 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -773,7 +773,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { c.convertBuiltin(n) case name != "": // Function was not found thus is can be only an invocation of func-typed variable or type conversion. - if isFunc { + // We care only about string conversions because all others are effectively no-op in NeoVM. + // E.g. one cannot write `bool(int(a))`, only `int32(int(a))`. + if isString(c.typeOf(n.Fun)) { + c.emitConvert(stackitem.ByteArrayT) + } else if isFunc { c.emitLoadVar(name) emit.Opcode(c.prog.BinWriter, opcode.CALLA) } diff --git a/pkg/compiler/convert_test.go b/pkg/compiler/convert_test.go index 613e1af3a..f0193acd0 100644 --- a/pkg/compiler/convert_test.go +++ b/pkg/compiler/convert_test.go @@ -86,3 +86,15 @@ func TestTypeConversion(t *testing.T) { eval(t, src, big.NewInt(42)) } + +func TestTypeConversionString(t *testing.T) { + src := `package foo + type mystr string + func Main() mystr { + b := []byte{'l', 'a', 'm', 'a', 'o'} + s := mystr(b) + b[0] = 'u' + return s + }` + eval(t, src, []byte("lamao")) +}