Skip to content

Commit ab16ca4

Browse files
committed
transpile: In convert_subtraction, only cast if types differ
1 parent 875ef75 commit ab16ca4

3 files changed

Lines changed: 18 additions & 18 deletions

File tree

c2rust-transpile/src/translator/operators.rs

Lines changed: 16 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!(
@@ -174,9 +172,9 @@ impl<'c> Translation<'c> {
174172

175173
lhs_val.zip(rhs_val).and_then_try(|(lhs_val, rhs_val)| {
176174
self.convert_binary_operator(
175+
ctx,
176+
expr_type_id,
177177
op,
178-
ty,
179-
expr_type_id.ctype,
180178
lhs_type_id,
181179
rhs_type_id,
182180
lhs_val,
@@ -218,12 +216,11 @@ impl<'c> Translation<'c> {
218216
WithStmts::new_val(read.clone()),
219217
)?;
220218

221-
let ty = self.convert_type(compute_res_type_id.ctype)?;
222219
let val = lhs.and_then_try(|lhs| {
223220
self.convert_binary_operator(
221+
ctx,
222+
compute_res_type_id,
224223
bin_op,
225-
ty,
226-
compute_res_type_id.ctype,
227224
compute_lhs_type_id,
228225
rhs_type_id,
229226
lhs,
@@ -426,12 +423,11 @@ impl<'c> Translation<'c> {
426423
WithStmts::new_val(read.clone()),
427424
)?;
428425

429-
let ty = self.convert_type(result_type_id.ctype)?;
430426
let val = lhs.and_then_try(|lhs|
431427
self.convert_binary_operator(
428+
ctx,
429+
result_type_id,
432430
op,
433-
ty,
434-
result_type_id.ctype,
435431
expr_or_comp_type_id,
436432
rhs_type_id,
437433
lhs,
@@ -503,23 +499,25 @@ impl<'c> Translation<'c> {
503499
/// arguments be usable as rvalues.
504500
fn convert_binary_operator(
505501
&self,
502+
ctx: ExprContext,
503+
expr_type_id: CQualTypeId,
506504
op: CBinOp,
507-
ty: Box<Type>,
508-
ctype: CTypeId,
509505
lhs_type: CQualTypeId,
510506
rhs_type: CQualTypeId,
511507
lhs: Box<Expr>,
512508
rhs: Box<Expr>,
513509
) -> TranslationResult<WithStmts<Box<Expr>>> {
514510
let is_unsigned_integral_type = self
515511
.ast_context
516-
.resolve_type(ctype)
512+
.resolve_type(expr_type_id.ctype)
517513
.kind
518514
.is_unsigned_integral_type();
519515

520516
Ok(WithStmts::new_val(match op {
521517
CBinOp::Add => return self.convert_addition(lhs_type, rhs_type, lhs, rhs),
522-
CBinOp::Subtract => return self.convert_subtraction(ty, lhs_type, rhs_type, lhs, rhs),
518+
CBinOp::Subtract => {
519+
return self.convert_subtraction(ctx, expr_type_id, lhs_type, rhs_type, lhs, rhs)
520+
}
523521

524522
op if op.is_arithmetic() && is_unsigned_integral_type => {
525523
mk().method_call_expr(lhs, op.wrapping_method(), vec![rhs])
@@ -566,7 +564,8 @@ impl<'c> Translation<'c> {
566564

567565
fn convert_subtraction(
568566
&self,
569-
ty: Box<Type>,
567+
ctx: ExprContext,
568+
expr_type_id: CQualTypeId,
570569
lhs_type_id: CQualTypeId,
571570
rhs_type_id: CQualTypeId,
572571
lhs: Box<Expr>,
@@ -577,7 +576,8 @@ impl<'c> Translation<'c> {
577576

578577
if let &CTypeKind::Pointer(pointee) = rhs_type {
579578
let val = self.make_pointer_difference(lhs, rhs, pointee.ctype);
580-
Ok(val.map(|val| mk().cast_expr(val, ty)))
579+
let source_type_id = self.ast_context.type_for_kind(&CTypeKind::PtrDiff).unwrap();
580+
self.make_cast(ctx, CQualTypeId::new(source_type_id), expr_type_id, val)
581581
} else if let &CTypeKind::Pointer(pointee) = lhs_type {
582582
Ok(self.convert_pointer_offset(lhs, rhs, pointee.ctype, true, false))
583583
} 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)