-
Notifications
You must be signed in to change notification settings - Fork 281
transpile: fix out-of-range literals #1442
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
Conversation
efdb30b to
76e4cbc
Compare
fw-immunant
left a comment
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 looks good, modulo the one naming change mentioned inline.
It's unfortunate that without any knowledge of the target platform we can't have a nontrivial range for types like uintptr_t. But we would still need most of this infrastructure, just piping more information in (e.g. type limits for the current architecture), to know full ranges for all types in an architecture-dependent way. So let's merge this and leave the potential of following up with what's actually in range on this platform vs. what's guaranteed to be in range on all platforms.
|
For CI, looks like a macOS snapshot needs updating. |
c91d5ef to
94cfab4
Compare
This partially reverts 7eaa39f, "transpile: propagate expected types down to translation of literals". 7eaa39f, specifically the use of `fn literal_kind_matches_ty`, omits casts from literals where they are sometimes needed, breaking #1435. Before fixing it, this reverts the issue so that we can first add tests before fixing it.
94cfab4 to
cbf841b
Compare
… is 2^31 This is from `libmcs` and is currently broken (see #1435).
…in their type's range This fixes #1435. If a literal is within its type's range, then no explicit cast is needed, but otherwise, a cast is still needed. For fixed-width types, this is simple, but for platform-dependent types like `int`, this is trickier, as we don't know what platform the transpiled Rust will be compiled on. Thus, we use the narrowest ranges that are guaranteed to be valid for each type. On some platforms, this will result in an extra cast, but the same code will work on all platforms.
… the value, too, now
cbf841b to
c61b6ef
Compare
…in_range` regarding minimum ranges
…range` only works on integer/floats, respectively
…uaranteed_integer_in_range`
out_of_range_int.ctest #1436.This first reverts the part of 7eaa39f that broke #1435, which is omitting casts from literals where the type already matches without checking if the literal is within that type's range. Then tests are added and then
fn literal_kind_matches_tyis fixed to also check the type's range. If the literal may be out of range on any possible platform, then a cast remains.I didn't add a float to these tests since floats go through a different code path (there's no cast generated for them). So for them, we need to explicitly generate a cast if it's out of range.