-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib] Add UInt
debug_assert(value >= 0)
for Int
implicit conversion
#3753
base: nightly
Are you sure you want to change the base?
[stdlib] Add UInt
debug_assert(value >= 0)
for Int
implicit conversion
#3753
Conversation
Signed-off-by: martinvuyk <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a reasonable stop gap solution to avoid the problems caused with the implicit construction right now. WDYT @ConnorGray?
…ssert-int-implicit-conversion
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
Signed-off-by: martinvuyk <[email protected]>
!sync |
Hi @martinvuyk to keep you updated, the |
Thanks for the update @jackos. I figured it's a way to express explicitly that you want to build from a negative int while allowing for APIs to get implicitly converted and python devs not having to think too much with regards to int vs uint types. IMO Int -> UInt should remain implicit. We could also make the debug assert be always on in release mode as well so the implicit conversion from negative value becomes impossible if not using the builtin |
…ssert-int-implicit-conversion
FYI now that we just gained support for explicit constructor, @jackos has an internal design doc that we're working toward around safer numeric types. I'll let him run with this PR, but I suspect we'll just incorporate some ideas in this PR in the bigger picture of his set of changes/design. The TLDR though is we'll be making the narrowing conversion of |
Hi @jackos and @JoeLoser, thanks for keeping me updated, I really appreciate it. And kudos to jack, the I'll be a bit insistent again and stand for my opinion. |
Hi @martinvuyk when making it explicit there aren't many places throughout the codebase that implicitly convert a Python doesn't have the concept of a uint, it's only when the programmer is using the type system outside of what a python programmer would do. There are unexpected consequences to having the implicit conversion, e.g. in operations like I looked at modern strictly typed languages like Zig, Go, Swift, and Rust and they all have protections for this, I think it's a good tradeoff. |
Hadn't thought about that one, yeah that would be an issue. I've myself done
My main motivation is that I don't want them to have to think too much about that difference. But it's hard while also trying to not surprise programmers and having high perf people using the same language as someone hacking together some very shady script logic.
We'll keep the |
Yeah that's where we differ from other languages, with our scalar types being SIMD types of width 1 it should be ergonomic for kernel engineers and also protect people writing simple programs. I raised if we should add the
I want this to cause the
With the error saying to do this if you intended to bitcast the sign:
Or a method:
Want to get a consensus before doing this though, there's an argument that the explicit constructor shouldn't have a debug_assert and just allow the sign to be bitcast. Also the name should be the same to allow for truncating values / overflowing e.g. in |
To me the This all starts sounding to me like we need to step back and think about struct Int:
...
fn cast[T: AnyType](self) -> T:
constrained[DType.is_scalar[T]() or _type_is_eq[T, UInt]()]()
... # cast to that type to keep the same API for Using an explicit cast would have no warnings or asserts. |
Add
UInt
debug_assert(value >= 0)
forInt
implicit conversion. This PR also introduces a newuint() -> UInt
builtin function as alternative for constructing from negative values explicitly.