Skip to content

wrong behavior for lambda with mutated captured variable #705

Description

@kaihsin

Since kirin has everything thats immutable, the x+=3 in the following cases gets mangled upon lowering

from kirin.prelude import basic

@basic
def gam(y):

    x = 1

    a = x + y

    x += 3

    b = x + y

    return a,b


gam.print()

gives:

func.func @gam(y : !Any) -> !Any {
  ^0(%gam_self, %y):
  │   %x = py.constant.constant 1 : !py.int
  │   %a = py.binop.add(%x, %y) : ~T
  │ %x_1 = py.constant.constant 4 : !py.int
  │   %b = py.binop.add(%x_1, %y) : ~T
  │   %0 = py.tuple.new(%a, %b) : !py.tuple[~T, ~T]
  │        func.return %0
} // func.func gam

However in the case where lambda is involved:

@basic
def main(z):

    x = 3

    def y(i):
        return x + i + z

    a = y(1)
    x += 1

    b = y(1)

    return a,b 

main.print()

def main2():

    x = 3

    def y(i):
        return x + i

    a = y(1)
    x += 1

    b = y(1)

    return a,b 

print("py",main2())
print("kirin",main())

The kirin gives wrong result than the python:

py (4, 5)
kirin (4, 4)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

category: bugCategory: this is a bug or something isn't working as expected.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions