lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.5k stars 158 forks source link

Support string repeat with non constant integer #2675

Closed kmr-srbh closed 5 months ago

kmr-srbh commented 5 months ago

fixes #2652

my_string: str = "#"
n: i32 = 5

print(my_string * n)
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
#####
kmr-srbh commented 5 months ago

@Shaikh-Ubaid the following, unrelated to the changes in the PR, fails:

my_string: str = "#"
repeated_string: str = my_string * -1

print(repeated_string)
(base) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
malloc(): corrupted top size
Aborted (core dumped)

An assert statement had checked for this case before, but interestingly the assertion did not fail. The error is observed when we try to use the obtained value, either by trying to print it, or store it.

kmr-srbh commented 5 months ago

@Shaikh-Ubaid after you approve these changes, I will make the changes stated in https://github.com/lcompilers/lpython/pull/2651/files#r1573132771