Skip to content

Commit 8893ad2

Browse files
committed
transpile: Cast assignment result to expected type if needed
1 parent 0ed1a31 commit 8893ad2

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

c2rust-transpile/src/translator/operators.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ impl<'c> Translation<'c> {
322322
fn convert_assignment_operator_with_rhs(
323323
&self,
324324
ctx: ExprContext,
325-
_expected_type_id: Option<CQualTypeId>,
326-
_result_type_id: CQualTypeId,
325+
expected_type_id: Option<CQualTypeId>,
326+
result_type_id: CQualTypeId,
327327
op: CBinOp,
328328
lhs: CExprId,
329329
rhs_type_id: CQualTypeId,
@@ -500,9 +500,21 @@ impl<'c> Translation<'c> {
500500
}
501501
};
502502

503-
Ok(assign_stmt.and_then(|assign_stmt| {
504-
WithStmts::new(vec![mk().semi_stmt(assign_stmt)], read)
505-
}))
503+
let assign_result = self.convert_cast(
504+
ctx,
505+
result_type_id,
506+
expected_type_id.unwrap_or(result_type_id),
507+
WithStmts::new_val(read),
508+
None,
509+
None,
510+
None,
511+
)?;
512+
513+
Ok(assign_stmt
514+
.zip(assign_result)
515+
.and_then(|(assign_stmt, assign_result)| {
516+
WithStmts::new(vec![mk().semi_stmt(assign_stmt)], assign_result)
517+
}))
506518
})
507519
}
508520

c2rust-transpile/tests/snapshots.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ fn test_empty_init() {
361361

362362
#[test]
363363
fn test_exprs() {
364-
transpile("exprs.c").expect_compile_error(true).run();
364+
transpile("exprs.c").run();
365365
}
366366

367367
#[test]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
9797
pub unsafe extern "C" fn assign_result() {
9898
let mut l: ::core::ffi::c_ulong = 0 as ::core::ffi::c_ulong;
9999
l = 1 as ::core::ffi::c_ulong;
100-
let mut s1: size_t = l;
100+
let mut s1: size_t = l as size_t;
101101
l = l.wrapping_add(2 as ::core::ffi::c_ulong);
102-
let mut s2: size_t = l;
102+
let mut s2: size_t = l as size_t;
103103
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
9797
pub unsafe extern "C" fn assign_result() {
9898
let mut l: ::core::ffi::c_ulong = 0 as ::core::ffi::c_ulong;
9999
l = 1 as ::core::ffi::c_ulong;
100-
let mut s1: size_t = l;
100+
let mut s1: size_t = l as size_t;
101101
l = l.wrapping_add(2 as ::core::ffi::c_ulong);
102-
let mut s2: size_t = l;
102+
let mut s2: size_t = l as size_t;
103103
}

0 commit comments

Comments
 (0)