Skip to content

Bug in variable initialization #651

Open
@certik

Description

@certik

There are still some bugs in how variable initialization is handled.

from ltypes import i32

def g(x: i32):
    print(x)

x: i32 = 7

def f():
    a: i32 = 5
    x: i32 = 3
    x = 5
    b: i32 = x+1
    g(a*b+3)

f()

It prints:

$ lpython --new-parser a.py && ./a.out 
23
$ PYTHONPATH=src/runtime/ltypes python a.py
33

The solution: unless x is declared as x: Constant[i32] (we currently do not support it, but we could), then any initialization must be assigned to it using Assignment, not as "value" in the Variable_t, because then you can't assign to it later (we should check this in verify). Above x still has 3, not 5. For Constant[i32] you have to assign to it right away, and then you can't change it.

So the above code effectively becomes:

def f():
    a: i32
    x: i32
    b: i32
    a = 5
    x = 3
    x = 5
    b = x+1
    g(a*b+3)

And everything should work.

Metadata

Metadata

Assignees

Labels

could closeIssues/PRs which can be closed.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions