Skip to content

Commit 99ebaf1

Browse files
committed
transpile: Cast assignment result to expected type if needed
1 parent 8ea5763 commit 99ebaf1

4 files changed

Lines changed: 19 additions & 10 deletions

File tree

c2rust-transpile/src/translator/operators.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ impl<'c> Translation<'c> {
318318
fn convert_assignment_operator_with_rhs(
319319
&self,
320320
ctx: ExprContext,
321-
_expected_type_id: Option<CQualTypeId>,
322-
_result_type_id: CQualTypeId,
321+
expected_type_id: Option<CQualTypeId>,
322+
result_type_id: CQualTypeId,
323323
op: CBinOp,
324324
lhs: CExprId,
325325
rhs_type_id: CQualTypeId,
@@ -485,9 +485,18 @@ impl<'c> Translation<'c> {
485485
}
486486
};
487487

488-
Ok(assign_stmt.and_then(|assign_stmt| {
489-
WithStmts::new(vec![mk().semi_stmt(assign_stmt)], read)
490-
}))
488+
let assign_result = self.make_cast(
489+
ctx,
490+
result_type_id,
491+
expected_type_id.unwrap_or(result_type_id),
492+
WithStmts::new_val(read),
493+
)?;
494+
495+
Ok(assign_stmt
496+
.zip(assign_result)
497+
.and_then(|(assign_stmt, assign_result)| {
498+
WithStmts::new(vec![mk().semi_stmt(assign_stmt)], assign_result)
499+
}))
491500
})
492501
}
493502

c2rust-transpile/tests/snapshots.rs

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

392392
#[test]
393393
fn test_exprs() {
394-
transpile("exprs.c").expect_compile_error(true).run();
394+
transpile("exprs.c").run();
395395
}
396396

397397
#[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
@@ -139,7 +139,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
139139
pub unsafe extern "C" fn assign_result() {
140140
let mut l: ::core::ffi::c_ulong = 0 as ::core::ffi::c_ulong;
141141
l = 1 as ::core::ffi::c_ulong;
142-
let mut s1: size_t = l;
142+
let mut s1: size_t = l as size_t;
143143
l = l.wrapping_add(2 as ::core::ffi::c_ulong);
144-
let mut s2: size_t = l;
144+
let mut s2: size_t = l as size_t;
145145
}

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
@@ -139,7 +139,7 @@ pub unsafe extern "C" fn pointer_arithmetic() {
139139
pub unsafe extern "C" fn assign_result() {
140140
let mut l: ::core::ffi::c_ulong = 0 as ::core::ffi::c_ulong;
141141
l = 1 as ::core::ffi::c_ulong;
142-
let mut s1: size_t = l;
142+
let mut s1: size_t = l as size_t;
143143
l = l.wrapping_add(2 as ::core::ffi::c_ulong);
144-
let mut s2: size_t = l;
144+
let mut s2: size_t = l as size_t;
145145
}

0 commit comments

Comments
 (0)