Open Harding-Stardust opened 11 months ago
This is the correct behavior, because Int is signed and implemented with 64 bits of precision, what's happening here is a simple overflow, as it would happen in C++, not all languages error on overflow (although I agree this would be a desirable behavior) This doesn't happen in python because ints are using Arbitrary-precision arithmetic, see also
Agree. @willghatch the issue here is that the int constructor from IntegerLiteral is not producing an error or warning when it overflows. Was this on your todo list?
@lattner I've made related feature request for overflow errors here: https://github.com/modularml/mojo/issues/992
@lattner Actually, compiler warns about this if you do the literal to int conversion manually:
fn main():
alias x: IntLiteral = 1
alias y: IntLiteral = x << 65
print(Int(y))
# note: integer value 36893488147419103232 requires 67 bits to store,
but the destination bit width is only 64 bits wide
I agree with @theopomies that overflow at compile time should ideally trap. For instance, Rust lets people choose. Also, I'm surprised that it's nondeterministic, UB bad.
Agree. @willghatch the issue here is that the int constructor from IntegerLiteral is not producing an error or warning when it overflows. Was this on your todo list?
Yes, this is on my list but was pre-empted by some other things.
Bug description
I couldn't find and specifications on how large the type Int is (and if it's signed) so I fooled around in the Mojo Jupyter notebook with some large numbers like this:
Steps to reproduce
Used a clean notebook in the playground. Can't think of anything else that is needed to reproduce the bug.
System information
``shell
mojo -v
Using the Notebook (Can't find any version of Mojo or the kernel)modular -v
Using the Notebook (Can't find any version of Mojo or the kernel)