Skip to content

Conversation

@kkysen
Copy link
Contributor

@kkysen kkysen commented Nov 1, 2025

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_ty is 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.

@kkysen kkysen force-pushed the kkysen/fix-out-of-range-literal branch from efdb30b to 76e4cbc Compare November 1, 2025 08:14
Copy link
Contributor

@fw-immunant fw-immunant left a 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.

@fw-immunant
Copy link
Contributor

For CI, looks like a macOS snapshot needs updating.

@kkysen kkysen force-pushed the kkysen/fix-out-of-range-literal branch 2 times, most recently from c91d5ef to 94cfab4 Compare November 16, 2025 01:43
@kkysen kkysen changed the title transpile: fix out-of-range integers transpile: fix out-of-range literals Nov 18, 2025
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.
@kkysen kkysen force-pushed the kkysen/fix-out-of-range-literal branch from 94cfab4 to cbf841b Compare November 24, 2025 09:12
… 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.
@kkysen kkysen force-pushed the kkysen/fix-out-of-range-literal branch from cbf841b to c61b6ef Compare November 24, 2025 13:48
@kkysen kkysen merged commit 8001582 into master Nov 24, 2025
5 checks passed
@kkysen kkysen deleted the kkysen/fix-out-of-range-literal branch November 24, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Integer literal overflow in C causes Rust compilation error

4 participants