Skip to content

Commit e8d55cd

Browse files
authored
refactor: Fix transform casts from non-unit to unit types (#1567)
Fixes the example from #1560 by emitting the following pattern: ```rust { let _ = crate::app::src::rng::randombytes( &raw mut seed as *mut ::core::ffi::c_uchar, (3 as ::core::ffi::c_int * crate::params_sphincs_haraka_128s_h::SPX_N) as ::core::ffi::c_ulonglong, ); () }; ```
2 parents bbcff96 + 5780ace commit e8d55cd

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

c2rust-refactor/src/transform/externs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ fn make_cast<'a, 'tcx>(
187187

188188
if needs_transmute {
189189
mk().call_expr(mk().path_expr(vec!["", "core", "mem", "transmute"]), vec![expr])
190+
} else if ty.is_unit() {
191+
// `{ let _ = expr; }` since the context expects a unit tuple.
192+
// `let` is technically a statement but its value is `()` which
193+
// is exactly what we need.
194+
mk().block_expr(mk().block(vec![
195+
mk().local_stmt(P(mk().local(mk().wild_pat(), None::<P<ast::Ty>>, Some(expr)))),
196+
]))
190197
} else {
191198
let expr = if let ExprKind::AddrOf(_, mutability, _) = expr.kind {
192199
// We have to cast to *T where &T is the type of expr before casting

0 commit comments

Comments
 (0)