compiler: convert to ByteArray for string variables

Convert to ByteArray when converting variable to `string`, because
underlying byte-slice changes should not affect result string.
This commit is contained in:
Evgenii Stratonikov 2020-06-24 18:43:29 +03:00
parent 1d275ceb65
commit 2c5ab95b8a
2 changed files with 17 additions and 1 deletions

View file

@ -773,7 +773,11 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
c.convertBuiltin(n) c.convertBuiltin(n)
case name != "": case name != "":
// Function was not found thus is can be only an invocation of func-typed variable or type conversion. // 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) c.emitLoadVar(name)
emit.Opcode(c.prog.BinWriter, opcode.CALLA) emit.Opcode(c.prog.BinWriter, opcode.CALLA)
} }

View file

@ -86,3 +86,15 @@ func TestTypeConversion(t *testing.T) {
eval(t, src, big.NewInt(42)) 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"))
}