Skip to content

transpile: Cast overflow builtin operands to the output type#1863

Merged
thedataking merged 3 commits into
masterfrom
perl/overflow-builtin-types
Jul 2, 2026
Merged

transpile: Cast overflow builtin operands to the output type#1863
thedataking merged 3 commits into
masterfrom
perl/overflow-builtin-types

Conversation

@thedataking

@thedataking thedataking commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

The type-generic __builtin_{add,sub,mul}_overflow accept operand and result types that all differ, which the previous translation did not handle. When all three types translate to one Rust type, keep mapping to the native overflowing_* methods; otherwise compute in i128 and derive the overflow flag from a narrowing roundtrip check. Mixed-type uses involving 128-bit integers are rejected rather than translated wrongly (#1878); a new snapshot-test knob pins which decls are rejected.

Covers the overflow-builtin uses in libgit2 (#1861), which are all ≤64-bit integer types.

Possibly superseded by the broader builtin type-handling rework in #1860 / #1707.
Tagging @Rua for coordination.

@thedataking thedataking requested a review from ahomescu June 18, 2026 08:43
@Rua

Rua commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

@thedataking I don't think there is any overlap between the two PRs, since this one deals with the parameter types and not the actual result type of the function.

@thedataking thedataking changed the base branch from master to perl/zero-init-imports June 18, 2026 10:27
@thedataking thedataking added the bug Something isn't working label Jun 18, 2026
Comment thread c2rust-transpile/src/translator/builtins.rs Outdated
@thedataking thedataking changed the base branch from perl/zero-init-imports to master June 23, 2026 15:59
@thedataking thedataking changed the base branch from master to perl/fix-bitfields-in-const-macros June 24, 2026 03:48
Base automatically changed from perl/fix-bitfields-in-const-macros to master June 24, 2026 04:12
@thedataking thedataking force-pushed the perl/overflow-builtin-types branch from 7fc752f to b8781e0 Compare June 24, 2026 04:29
@thedataking thedataking force-pushed the perl/overflow-builtin-types branch from b8781e0 to bce177f Compare July 1, 2026 08:32
@thedataking thedataking requested a review from fw-immunant July 1, 2026 09:27
@thedataking thedataking force-pushed the perl/overflow-builtin-types branch 2 times, most recently from 47dd51b to 78b9790 Compare July 1, 2026 10:46
@thedataking thedataking force-pushed the perl/overflow-builtin-types branch from 78b9790 to 01da5b4 Compare July 1, 2026 22:55
The type-generic __builtin_{add,sub,mul}_overflow accept operand and
result types that all differ, without arithmetic conversions. The
previous translation applied Rust's overflowing_* directly to the
operands, which does not compile when their Rust types differ and does
not account for a narrower result type.

When all three types translate to one Rust type, keep using the native
overflowing_* method. Otherwise, compute in i128 (overflow is defined
against the infinite-precision result) and derive the overflow flag
from the i128 overflow together with a narrowing roundtrip check.
Snapshot tests run with fail_on_error, so a decl that fails translation
aborts the test. Add an expect_translation_error knob that turns
fail_on_error off; failed decls are then omitted from the output, so
the snapshot records which decls are rejected.
The i128 used to compute the infinite-precision result cannot
represent all values of unsigned __int128 operands or results, which
would silently corrupt the overflow flag. Same-type 128-bit uses keep
mapping to Rust's native overflowing_* methods.

See #1878.
@thedataking thedataking force-pushed the perl/overflow-builtin-types branch from 01da5b4 to 5b44f57 Compare July 1, 2026 23:01
@thedataking thedataking requested a review from ahomescu July 1, 2026 23:08
Comment thread c2rust-transpile/src/translator/builtins.rs
@thedataking thedataking merged commit 909ae1f into master Jul 2, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants