Skip to content

Commit e95eeb3

Browse files
author
Tural Devrishev
committed
compiler: fix panic for inline invocations
Close #3993. Signed-off-by: Tural Devrishev <tural@nspcc.ru>
1 parent bf9d0c3 commit e95eeb3

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

pkg/compiler/codegen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,10 @@ func (c *codegen) emitStoreVar(pkg string, name string) {
316316
return
317317
}
318318
vi := c.getVarIndex(pkg, name)
319+
if vi.index == unspecifiedVarIndex && vi.ctx != nil {
320+
c.scope.newLocal(name)
321+
vi = c.getVarIndex(pkg, name)
322+
}
319323
c.emitStoreByIndex(vi.refType, vi.index)
320324
}
321325

pkg/compiler/inline_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,47 @@ func TestInlineForeignType(t *testing.T) {
454454
}`
455455
eval(t, src, big.NewInt(29))
456456
}
457+
458+
func TestInlineModifyArg(t *testing.T) {
459+
src := `package foo
460+
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline/d"
461+
462+
func Main() int {
463+
return d.Negate(-42)
464+
}`
465+
eval(t, src, big.NewInt(42))
466+
}
467+
468+
func TestInlineMixedArgs(t *testing.T) {
469+
src := `package foo
470+
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline/d"
471+
472+
func someCall() int { return 7 }
473+
474+
func Main() int {
475+
return d.AddNeg(-3, someCall())
476+
}`
477+
eval(t, src, big.NewInt(-4))
478+
}
479+
480+
func TestInlineChain(t *testing.T) {
481+
src := `package foo
482+
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline/d"
483+
484+
func Main() int {
485+
return d.Wrap2(-2)
486+
}`
487+
eval(t, src, big.NewInt(2))
488+
}
489+
490+
func TestInlineSlice(t *testing.T) {
491+
src := `package foo
492+
import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline/d"
493+
494+
func Main() int {
495+
s := make([]int, 1)
496+
d.SetOneFirstElem(s)
497+
return s[0]
498+
}`
499+
eval(t, src, big.NewInt(1))
500+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package d
2+
3+
func Negate(n int) int {
4+
n *= -1
5+
return n
6+
}
7+
8+
func AddNeg(a int, b int) int {
9+
a *= -1
10+
b *= -1
11+
return a + b
12+
}
13+
14+
func Wrap1(n int) int {
15+
n *= -1
16+
n *= -1
17+
return Negate(n)
18+
}
19+
20+
func Wrap2(n int) int {
21+
n *= -1
22+
n *= -1
23+
return Wrap1(n)
24+
}
25+
26+
func SetOneFirstElem(s []int) {
27+
s[0] = 1
28+
}

0 commit comments

Comments
 (0)