Skip to content

Commit 9d79e3b

Browse files
committed
transpile: In convert_subtraction, only cast if types differ
1 parent 9143df3 commit 9d79e3b

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

c2rust-transpile/src/translator/operators.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ impl<'c> Translation<'c> {
6666
ctx = ctx.decay_ref();
6767
}
6868

69-
let ty = self.convert_type(expr_type_id.ctype)?;
70-
7169
let lhs_kind = &self.ast_context.index_unwrap_parens(lhs).kind;
7270
let mut lhs_type_id = lhs_kind.get_qual_type().ok_or_else(|| {
7371
format_translation_err!(
@@ -175,8 +173,7 @@ impl<'c> Translation<'c> {
175173
lhs_val.zip(rhs_val).and_then_try(|(lhs_val, rhs_val)| {
176174
self.convert_binary_operator(
177175
op,
178-
ty,
179-
expr_type_id.ctype,
176+
expr_type_id,
180177
lhs_type_id,
181178
rhs_type_id,
182179
lhs_val,
@@ -221,12 +218,10 @@ impl<'c> Translation<'c> {
221218
None,
222219
)?;
223220

224-
let ty = self.convert_type(compute_res_type_id.ctype)?;
225221
let val = lhs.and_then_try(|lhs| {
226222
self.convert_binary_operator(
227223
bin_op,
228-
ty,
229-
compute_res_type_id.ctype,
224+
compute_res_type_id,
230225
compute_lhs_type_id,
231226
rhs_type_id,
232227
lhs,
@@ -433,12 +428,10 @@ impl<'c> Translation<'c> {
433428
None,
434429
)?;
435430

436-
let ty = self.convert_type(result_type_id.ctype)?;
437431
let val = lhs.and_then_try(|lhs|
438432
self.convert_binary_operator(
439433
op,
440-
ty,
441-
result_type_id.ctype,
434+
result_type_id,
442435
expr_or_comp_type_id,
443436
rhs_type_id,
444437
lhs,
@@ -514,22 +507,23 @@ impl<'c> Translation<'c> {
514507
fn convert_binary_operator(
515508
&self,
516509
op: CBinOp,
517-
ty: Box<Type>,
518-
ctype: CTypeId,
510+
result_type_id: CQualTypeId,
519511
lhs_type: CQualTypeId,
520512
rhs_type: CQualTypeId,
521513
lhs: Box<Expr>,
522514
rhs: Box<Expr>,
523515
) -> TranslationResult<WithStmts<Box<Expr>>> {
524516
let is_unsigned_integral_type = self
525517
.ast_context
526-
.resolve_type(ctype)
518+
.resolve_type(result_type_id.ctype)
527519
.kind
528520
.is_unsigned_integral_type();
529521

530522
Ok(WithStmts::new_val(match op {
531523
CBinOp::Add => return self.convert_addition(lhs_type, rhs_type, lhs, rhs),
532-
CBinOp::Subtract => return self.convert_subtraction(ty, lhs_type, rhs_type, lhs, rhs),
524+
CBinOp::Subtract => {
525+
return self.convert_subtraction(result_type_id, lhs_type, rhs_type, lhs, rhs)
526+
}
533527

534528
op if op.is_arithmetic() && is_unsigned_integral_type => {
535529
mk().method_call_expr(lhs, op.wrapping_method(), vec![rhs])
@@ -576,7 +570,7 @@ impl<'c> Translation<'c> {
576570

577571
fn convert_subtraction(
578572
&self,
579-
ty: Box<Type>,
573+
result_type_id: CQualTypeId,
580574
lhs_type_id: CQualTypeId,
581575
rhs_type_id: CQualTypeId,
582576
lhs: Box<Expr>,
@@ -593,7 +587,14 @@ impl<'c> Translation<'c> {
593587
offset = mk().binary_expr(BinOp::Div(Default::default()), offset, div);
594588
}
595589

596-
Ok(WithStmts::new_val(mk().cast_expr(offset, ty)).set_unsafe())
590+
let expr_type_kind = &self.ast_context.resolve_type(result_type_id.ctype).kind;
591+
592+
if !matches!(expr_type_kind, CTypeKind::PtrDiff) {
593+
let type_rs = self.convert_type(result_type_id.ctype)?;
594+
offset = mk().cast_expr(offset, type_rs);
595+
}
596+
597+
Ok(WithStmts::new_val(offset).set_unsafe())
597598
} else if let &CTypeKind::Pointer(pointee) = lhs_type {
598599
Ok(self.convert_pointer_offset(lhs, rhs, pointee.ctype, true, false))
599600
} else if lhs_type.is_unsigned_integral_type() {

c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2021.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
8989
let mut i2: [::core::ffi::c_int; 2] = [10 as ::core::ffi::c_int, 11 as ::core::ffi::c_int];
9090
let mut p1: *mut ::core::ffi::c_int = &raw mut i1 as *mut ::core::ffi::c_int;
9191
let mut p2: *mut ::core::ffi::c_int = &raw mut i2 as *mut ::core::ffi::c_int;
92-
let mut diff: ptrdiff_t = p1.offset_from(p2) as ptrdiff_t;
92+
let mut diff: ptrdiff_t = p1.offset_from(p2);
9393
let mut diff_int: ::core::ffi::c_int =
9494
p1.offset_from(p2) as ::core::ffi::c_long as ::core::ffi::c_int;
9595
}

c2rust-transpile/tests/snapshots/snapshots__transpile@exprs.c.2024.clang15.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
8989
let mut i2: [::core::ffi::c_int; 2] = [10 as ::core::ffi::c_int, 11 as ::core::ffi::c_int];
9090
let mut p1: *mut ::core::ffi::c_int = &raw mut i1 as *mut ::core::ffi::c_int;
9191
let mut p2: *mut ::core::ffi::c_int = &raw mut i2 as *mut ::core::ffi::c_int;
92-
let mut diff: ptrdiff_t = p1.offset_from(p2) as ptrdiff_t;
92+
let mut diff: ptrdiff_t = p1.offset_from(p2);
9393
let mut diff_int: ::core::ffi::c_int =
9494
p1.offset_from(p2) as ::core::ffi::c_long as ::core::ffi::c_int;
9595
}

0 commit comments

Comments
 (0)