Skip to content

Commit 6049b1f

Browse files
committed
transpile: Replace WithStmts::new_unsafe_val with builder calls
1 parent e1e48f6 commit 6049b1f

6 files changed

Lines changed: 15 additions & 29 deletions

File tree

c2rust-transpile/src/translator/literals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'c> Translation<'c> {
140140
val = mk().const_block_expr(mk().const_block(stmts));
141141
}
142142

143-
Ok(WithStmts::new_unsafe_val(val))
143+
Ok(WithStmts::new_val(val).set_unsafe())
144144
}
145145
}
146146
}

c2rust-transpile/src/translator/operators.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,11 @@ impl<'c> Translation<'c> {
422422
let assign_stmt = match op {
423423
// Regular (possibly volatile) assignment
424424
Assign if !is_volatile => WithStmts::new_val(mk().assign_expr(write, rhs)),
425-
Assign => WithStmts::new_unsafe_val(self.volatile_write(
425+
Assign => WithStmts::new_val(self.volatile_write(
426426
write,
427427
initial_lhs_type_id,
428428
rhs,
429-
)?),
429+
)?).set_unsafe(),
430430

431431
// Anything volatile needs to be desugared into explicit reads and writes
432432
op if is_volatile || is_unsigned_arith => {
@@ -473,9 +473,9 @@ impl<'c> Translation<'c> {
473473
#[allow(clippy::let_and_return /* , reason = "block is large, so variable name helps" */)]
474474
let write = if is_volatile {
475475
val.and_then_try(|val| {
476-
TranslationResult::Ok(WithStmts::new_unsafe_val(
476+
TranslationResult::Ok(WithStmts::new_val(
477477
self.volatile_write(write, initial_lhs_type_id, val)?,
478-
))
478+
).set_unsafe())
479479
})?
480480
} else {
481481
val.map(|val| mk().assign_expr(write, val))
@@ -617,7 +617,7 @@ impl<'c> Translation<'c> {
617617
offset = mk().binary_expr(BinOp::Div(Default::default()), offset, div);
618618
}
619619

620-
Ok(WithStmts::new_unsafe_val(mk().cast_expr(offset, ty)))
620+
Ok(WithStmts::new_val(mk().cast_expr(offset, ty)).set_unsafe())
621621
} else if let &CTypeKind::Pointer(pointee) = lhs_type {
622622
Ok(self.convert_pointer_offset(lhs, rhs, pointee.ctype, true, false))
623623
} else if lhs_type.is_unsigned_integral_type() {

c2rust-transpile/src/translator/pointers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<'c> Translation<'c> {
382382
res = mk().unary_expr(UnOp::Deref(Default::default()), res);
383383
}
384384

385-
WithStmts::new_unsafe_val(res)
385+
WithStmts::new_val(res).set_unsafe()
386386
}
387387

388388
/// Construct an expression for a NULL at any type, including forward declarations,
@@ -450,7 +450,7 @@ impl<'c> Translation<'c> {
450450
self.import_type(target_cty);
451451

452452
Ok(val.and_then(|val| {
453-
WithStmts::new_unsafe_val(transmute_expr(source_ty, target_ty, val))
453+
WithStmts::new_val(transmute_expr(source_ty, target_ty, val)).set_unsafe()
454454
}))
455455
} else {
456456
// Normal case
@@ -483,7 +483,7 @@ impl<'c> Translation<'c> {
483483
let intptr_t = mk().abs_path_ty(vec!["libc", "intptr_t"]);
484484
val = mk().cast_expr(val, intptr_t.clone());
485485

486-
WithStmts::new_unsafe_val(transmute_expr(intptr_t, target_ty, val))
486+
WithStmts::new_val(transmute_expr(intptr_t, target_ty, val)).set_unsafe()
487487
}))
488488
} else if source_ty_kind.is_bool() {
489489
self.use_crate(ExternCrate::Libc);
@@ -520,7 +520,7 @@ impl<'c> Translation<'c> {
520520

521521
if self.ast_context.is_function_pointer(source_cty) {
522522
Ok(val.and_then(|val| {
523-
WithStmts::new_unsafe_val(transmute_expr(source_ty, target_ty, val))
523+
WithStmts::new_val(transmute_expr(source_ty, target_ty, val)).set_unsafe()
524524
}))
525525
} else if let &CTypeKind::Enum(enum_decl_id) = target_ty_kind {
526526
val.try_map(|val| self.convert_cast_to_enum(ctx, target_cty, enum_decl_id, expr, val))

c2rust-transpile/src/translator/simd.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,10 @@ impl<'c> Translation<'c> {
270270
let n_bytes_expr = mk().lit_expr(mk().int_lit(bytes, ""));
271271
let expr = mk().repeat_expr(zero_expr, n_bytes_expr);
272272

273-
Ok(WithStmts::new_unsafe_val(transmute_expr(
274-
mk().infer_ty(),
275-
mk().infer_ty(),
276-
expr,
277-
)))
273+
Ok(
274+
WithStmts::new_val(transmute_expr(mk().infer_ty(), mk().infer_ty(), expr))
275+
.set_unsafe(),
276+
)
278277
} else {
279278
self.import_simd_function(fn_name)
280279
.expect("None of these fns should be unsupported in rust");

c2rust-transpile/src/translator/structs_unions.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,12 +541,7 @@ impl<'a> Translation<'a> {
541541
stmts.push(mk().expr_stmt(mk().ident_expr("init")));
542542

543543
let val = mk().block_expr(mk().block(stmts));
544-
545-
if is_unsafe {
546-
WithStmts::new_unsafe_val(val)
547-
} else {
548-
WithStmts::new_val(val)
549-
}
544+
WithStmts::new_val(val).merge_unsafe(is_unsafe)
550545
});
551546
}
552547

c2rust-transpile/src/with_stmts.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,6 @@ impl<T> WithStmts<T> {
2727
}
2828
}
2929

30-
pub fn new_unsafe_val(val: T) -> Self {
31-
WithStmts {
32-
stmts: vec![],
33-
val,
34-
is_unsafe: true,
35-
}
36-
}
37-
3830
pub fn and_then<U, F>(self, f: F) -> WithStmts<U>
3931
where
4032
F: FnOnce(T) -> WithStmts<U>,

0 commit comments

Comments
 (0)