Skip to content

Translate size_t/ssize_t as usize/isize#185

Merged
nunoplopes merged 28 commits into
Cpp2Rust:masterfrom
lucic71:u64-to-usize
Jun 12, 2026
Merged

Translate size_t/ssize_t as usize/isize#185
nunoplopes merged 28 commits into
Cpp2Rust:masterfrom
lucic71:u64-to-usize

Conversation

@lucic71

@lucic71 lucic71 commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

This PR translates size_t/ssize_t as usize/isize instead of u64/i64. All tests and rules are affected by this change. That's why this PR is very big. However the core of this change is quite small.

In rules/ I had to change every u64/i64 with usize/isize. This makes the rules cleaner because we get rid of artificial as usize casts in arguments and artificial as u64 casts in return values.

In libcc2rs/ I changed the arguments of some transalted functions (malloc, fwrite, etc) from u64 to usize. I also added usize/isize branches for VaArg.

In cpp2rust/ the change has 3 parts:

  1. I put usize/isize in addBuiltinTypes. This adds rules for: size_t / size_type / __size_t -> usize and ssize_t -> isize. __size_t is an internal type that describes the return value of sizeof
  2. ToString desugars its type by default, so I gave it a flag to leave scalars alone, and made search(QualType) look for the sugared type before the desugared one:
std::string ToString(clang::QualType qual_type,
                     ScalarSugar sugar = ScalarSugar::kDesugar);

This is needed because size_t isn't a real type, just a typedef over an integer.

  1. In Converter, I added an extra default argument to Convert, ConvertRValue and ConvertFreshRValue: std::optional<clang::QualType> implicit_convert_to. In Clang, both size_t and unsigned long are represented as the same type, but in Rust, usize and u64 are different types, an implicit cast between the two is not allowed. To solve this, Convert is the single point of adding a cast between 2 types that share the same underlying type in C but have different types in Rust, this is decided by NeedsImplicitScalarCast.

Callers of Convert have to pass the optional implicit_convert_to in order to trigger the scalar cast. Callers pass the target type, for example ConvertFreshRValue(rhs, lhs->getType()); in ConvertAssignment, and Convert takes care of the explicit Rust call if needed. Binary operations do the same using GetOperandImplicitConversionTarget, which converts both arguments to the same Rust type.

@lucic71 lucic71 marked this pull request as draft June 10, 2026 14:01
@lucic71 lucic71 marked this pull request as ready for review June 10, 2026 18:17
@nunoplopes nunoplopes merged commit 92e2e20 into Cpp2Rust:master Jun 12, 2026
9 checks passed
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.

2 participants