fix runtime panics on self assignment (#387)

This commit is contained in:
E Sequeira 2022-07-20 16:09:43 +01:00 committed by GitHub
parent 818d6dc687
commit 6fc8053992
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View file

@ -692,12 +692,15 @@ func (c *Compiler) compileAssign(
return c.errorf(node, "operator ':=' not allowed with selector")
}
_, isFunc := rhs[0].(*parser.FuncLit)
symbol, depth, exists := c.symbolTable.Resolve(ident, false)
if op == token.Define {
if depth == 0 && exists {
return c.errorf(node, "'%s' redeclared in this block", ident)
}
symbol = c.symbolTable.Define(ident)
if isFunc {
symbol = c.symbolTable.Define(ident)
}
} else {
if !exists {
return c.errorf(node, "unresolved reference '%s'", ident)
@ -718,6 +721,10 @@ func (c *Compiler) compileAssign(
}
}
if op == token.Define && !isFunc {
symbol = c.symbolTable.Define(ident)
}
switch op {
case token.AddAssign:
c.emit(node, parser.OpBinaryOp, int(token.Add))

View file

@ -1023,6 +1023,8 @@ func TestCompilerErrorReport(t *testing.T) {
expectCompileError(t, `a = 1`,
"Compile Error: unresolved reference 'a'\n\tat test:1:1")
expectCompileError(t, `a := a`,
"Compile Error: unresolved reference 'a'\n\tat test:1:6")
expectCompileError(t, `a, b := 1, 2`,
"Compile Error: tuple assignment not allowed\n\tat test:1:1")
expectCompileError(t, `a.b := 1`,