Skip to content

Commit bd7a6f9

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 bd7a6f9

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

pkg/compiler/codegen.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ func (c *codegen) emitStoreVar(pkg string, name string) {
316316
return
317317
}
318318
vi := c.getVarIndex(pkg, name)
319+
if vi.index == unspecifiedVarIndex {
320+
// Creating a local variable if it was not created earlier during inline calls.
321+
c.scope.newLocal(name)
322+
vi = c.getVarIndex(pkg, name)
323+
}
319324
c.emitStoreByIndex(vi.refType, vi.index)
320325
}
321326

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(-35, someCall())
476+
}`
477+
eval(t, src, big.NewInt(42))
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(-42)
486+
}`
487+
eval(t, src, big.NewInt(42))
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.SetFirstElem(s)
497+
return s[0]
498+
}`
499+
eval(t, src, big.NewInt(42))
500+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
return Negate(n)
17+
}
18+
19+
func Wrap2(n int) int {
20+
n *= -1
21+
return Wrap1(n)
22+
}
23+
24+
func SetFirstElem(s []int) {
25+
s[0] = 42
26+
}

0 commit comments

Comments
 (0)