Skip to content

Commit 256636f

Browse files
committed
Fix scope issue when calling user function with global var
1 parent 2fe4d6a commit 256636f

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

goawk.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
)
4545

4646
const (
47-
version = "v1.1.4"
47+
version = "v1.1.5"
4848
)
4949

5050
func main() {

interp/interp_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ BEGIN { early() }
505505
{`function f(x) { 0 in _; f(_) } BEGIN { f() } # !awk !gawk`, "", "",
506506
`parse error at 1:25: can't pass array "_" as scalar param`, ""},
507507
{`BEGIN { for (i=0; i<1001; i++) f(); print x } function f() { x++ }`, "", "1001\n", "", ""},
508+
{`
509+
function bar(y) { return y[1] }
510+
function foo() { return bar(x) }
511+
BEGIN { x[1] = 42; print foo() }
512+
`, "", "42\n", "", ""},
508513

509514
// Type checking / resolver tests
510515
{`BEGIN { a[x]; a=42 }`, "", "", `parse error at 1:15: can't use array "a" as scalar`, "array"},

parser/resolve.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ func (p *parser) resolveUserCalls(prog *Program) {
156156
// type based on context, so mark the types for these as unknown.
157157
func (p *parser) processUserCallArg(funcName string, arg Expr, index int) {
158158
if varExpr, ok := arg.(*VarExpr); ok {
159-
ref := p.varTypes[p.funcName][varExpr.Name].ref
159+
scope, varFuncName := p.getScope(varExpr.Name)
160+
ref := p.varTypes[varFuncName][varExpr.Name].ref
160161
if ref == varExpr {
161162
// Only applies if this is the first reference to this
162163
// variable (otherwise we know the type already)
163-
scope := p.varTypes[p.funcName][varExpr.Name].scope
164-
p.varTypes[p.funcName][varExpr.Name] = typeInfo{typeUnknown, ref, scope, 0, funcName, index}
164+
p.varTypes[varFuncName][varExpr.Name] = typeInfo{typeUnknown, ref, scope, 0, funcName, index}
165165
}
166166
// Mark the last related varRef (the most recent one) as a
167167
// call argument for later error handling

0 commit comments

Comments
 (0)