mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
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:
parent
1d275ceb65
commit
2c5ab95b8a
2 changed files with 17 additions and 1 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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"))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue