Skip to content

Variable scope bug in lambda #1983

@wonderyl

Description

@wonderyl

Bug Report

1. Minimal reproduce step (Required)

  • File: scope_bug.k
schema Value:
  r: int

schema Value2:
  r: int

foo = lambda v: Value -> Value2 {
  Value2 {
    r = v.r
  }
}

schema bar[input: Value]:
  v: Value2

  v = foo(input)

x = Value {
  r = 1
}
y = bar(x)

schema bar[v2: Value]:
  v: Value2

  v = foo(v2)

x = Value {
  r = 1
}
y = bar(x)

Run kcl scope_bug.k gets

Error: EvaluationError
 --> /home/lei/kcl/scope_bug.k:9:1
  |
9 |     r = v.r
  |  invalid value 'UndefinedType' to load attribute 'r'
  |
  • File: no_scope_bug.k
schema Value:
  r: int

schema Value2:
  r: int

foo = lambda v2: Value -> Value2 {
  Value2 {
    r = v2.r
  }
}

schema bar[input: Value]:
  v: Value2

  v = foo(input)

x = Value {
  r = 1
}
y = bar(x)

Run kcl no_scope_bug.k gets

x:
  r: 1
'y':
  v:
    r: 1

Diff of the two file is

7c7
< foo = lambda v: Value -> Value2 {
---
> foo = lambda v2: Value -> Value2 {
9c9
<     r = v.r
---
>     r = v2.r

2. What did you expect to see? (Required)

The problem seems to be, in lambda foo, the input variable v is resolved to the field v of schema bar, which is not defined at the moment of calling foo. When I rename the input of lambda foo to v2. It worked correctly.

I would expect the input variable v takes precedence of field v of schema bar.

3. What did you see instead (Required)

EvaluationError

4. What is your KCL components version? (Required)

$ kcl -v
kcl version 0.11.2-linux-amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions