lcompilers / lpython

Python compiler
https://lpython.org/
Other
1.37k stars 157 forks source link

Bug: All numeric types accept invalid string literals as arguments #2602

Open kmr-srbh opened 3 months ago

kmr-srbh commented 3 months ago

The problem

The numeric types i32, i64, f32 and f64 accept String Constants and return value which a str variable can store. What I do here is highly unlikely to be done in the real world, but still, it is not the expected behavior.

from lpython import i32, i64, f32, f64

integer32: str = i32("Hello, LPython!")
integer64: str = i64("Hello, LPython!")
float32: str = f32("Hello, LPython!")
float64: str = f64("Hello, LPython!")

print("integer32:", integer32)
print("integer64:", integer64)
print("float32:", float32)
print("float64:", float64)
(lp) saurabh-kumar@Awadh:~/Projects/System/lpython$ ./src/bin/lpython ./examples/example.py
integer32: Hello, LPython!
integer64: Hello, LPython!
float32: Hello, LPython!
float64: Hello, LPython!

The solution

The solution is not to disallow accepting string arguments, we need them as mentioned in #2554 . Instead, we need to handle invalid string literals like the above. Hexadecimal and numbers in other bases must be handled too.