Closed kmr-srbh closed 2 months ago
This is actually a bug related to the variable scope. When creating a variable for the ConstantList
to loop on, the variable scope is set to local. The above example, being in a global scope, fails but those in a local scope work as intended.
from lpython import i32
def foo():
i: i32
for i in [1, 2, 3, 4, 5]:
print(i)
foo()
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
1
2
3
4
5
The offending line is
current_body->push_back(al, ASRUtils::STMT(assign));
In case of global scope, the current_body pointer is a null pointer and hence the seg fault occurs by dereferencing it.
Afaik current_body
points to a list of statements in the current scope, and it is written in its documentation that it exists only for Functions, For etc and not Modules, ClassDefs/Structs.
@kmr-srbh if you know the equivalent for current_body for the module, it is an easy fix. I am unaware of the same.
The fix was very simple we just had to use global_init
when currrent_body
doesnt exist.
Example 1
Example 2
The same is true for a
StringConstant
.Stacktrace