Skip to content

Commit 68ca2e3

Browse files
authored
transpile: Turn WithStmts methods into builders (#1789)
Following the suggestion made at #1644 (comment).
2 parents 37d4534 + 4eaec90 commit 68ca2e3

8 files changed

Lines changed: 53 additions & 74 deletions

File tree

c2rust-transpile/src/translator/functions.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl<'c> Translation<'c> {
401401

402402
// Function pointer call
403403
_ => {
404-
let mut callee = self.convert_expr(ctx.used(), func, None)?;
404+
let callee = self.convert_expr(ctx.used(), func, None)?;
405405
let make_fn_ty = |ret_ty: Box<Type>| {
406406
let ret_ty = match *ret_ty {
407407
Type::Tuple(TypeTuple { elems: ref v, .. }) if v.is_empty() => ReturnType::Default,
@@ -419,20 +419,18 @@ impl<'c> Translation<'c> {
419419
// K&R function pointer without arguments
420420
let ret_ty = self.convert_type(ret_ty.ctype)?;
421421
let target_ty = make_fn_ty(ret_ty);
422-
callee.set_unsafe();
423422
callee.map(|fn_ptr| {
424423
let fn_ptr = unwrap_function_pointer(fn_ptr);
425424
transmute_expr(mk().infer_ty(), target_ty, fn_ptr)
426-
})
425+
}).set_unsafe()
427426
}
428427
None => {
429428
// We have to infer the return type from our expression type
430429
let ret_ty = self.convert_type(call_expr_ty.ctype)?;
431430
let target_ty = make_fn_ty(ret_ty);
432-
callee.set_unsafe();
433431
callee.map(|fn_ptr| {
434432
transmute_expr(mk().infer_ty(), target_ty, fn_ptr)
435-
})
433+
}).set_unsafe()
436434
}
437435
Some(CTypeKind::Function(_, ty_arg_tys, ..)) => {
438436
arg_tys = Some(ty_arg_tys.clone());

c2rust-transpile/src/translator/literals.rs

Lines changed: 7 additions & 11 deletions
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
}
@@ -183,17 +183,13 @@ impl<'c> Translation<'c> {
183183
Ok(val.wrap_unsafe().and_then(|val| {
184184
let item = mk().mutbl().static_item(&fresh_name, fresh_ty, val);
185185
let fresh_stmt = mk().item_stmt(item);
186-
let mut val = WithStmts::new(vec![fresh_stmt], mk().ident_expr(fresh_name));
187186

188-
// Accessing a static variable is unsafe.
189-
// In the current nightly, this applies also to taking a raw pointer,
190-
// but this requirement was removed in later versions of the
191-
// `raw_ref_op` feature.
192-
if self.tcfg.edition < Edition2024 {
193-
val.set_unsafe();
194-
}
195-
196-
val
187+
WithStmts::new(vec![fresh_stmt], mk().ident_expr(fresh_name))
188+
// Accessing a static variable is unsafe.
189+
// In the current nightly, this applies also to taking a raw pointer,
190+
// but this requirement was removed in later versions of the
191+
// `raw_ref_op` feature.
192+
.merge_unsafe(self.tcfg.edition < Edition2024)
197193
}))
198194
} else {
199195
Ok(val.and_then(|val| {

c2rust-transpile/src/translator/mod.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,9 +2361,7 @@ impl<'c> Translation<'c> {
23612361
.span(span)
23622362
.mutbl()
23632363
.static_item(&ident2, ty, default_init);
2364-
let mut init = init?;
2365-
init.set_unsafe();
2366-
let mut init = init.to_expr();
2364+
let mut init = init?.set_unsafe().to_expr();
23672365

23682366
self.add_static_initializer_to_section(ctx, &ident2, typ, &mut init)?;
23692367
self.items.borrow_mut()[&self.main_file].add_item(static_item);
@@ -3131,9 +3129,7 @@ impl<'c> Translation<'c> {
31313129
}
31323130
}
31333131

3134-
let mut res = WithStmts::new_val(val);
3135-
res.merge_unsafe(set_unsafe);
3136-
Ok(res)
3132+
Ok(WithStmts::new_val(val).merge_unsafe(set_unsafe))
31373133
}
31383134

31393135
OffsetOf(ty, ref kind) => match kind {
@@ -3255,7 +3251,7 @@ impl<'c> Translation<'c> {
32553251

32563252
if is_explicit {
32573253
let stmts = self.compute_variable_array_sizes(ctx, ty.ctype)?;
3258-
val.prepend_stmts(stmts);
3254+
val = val.prepend_stmts(stmts);
32593255
}
32603256

32613257
// Shuffle Vector "function" builtins will add a cast to the output of the
@@ -3316,14 +3312,17 @@ impl<'c> Translation<'c> {
33163312
let is_unsafe = lhs.is_unsafe() || rhs.is_unsafe();
33173313
let then = mk().block(lhs.into_stmts());
33183314
let else_ = mk().block_expr(mk().block(rhs.into_stmts()));
3315+
let res = cond
3316+
.and_then(|c| {
3317+
WithStmts::new(
3318+
vec![mk().semi_stmt(mk().ifte_expr(c, then, Some(else_)))],
3319+
self.panic_or_err(
3320+
"Conditional expression is not supposed to be used",
3321+
),
3322+
)
3323+
})
3324+
.merge_unsafe(is_unsafe);
33193325

3320-
let mut res = cond.and_then(|c| {
3321-
WithStmts::new(
3322-
vec![mk().semi_stmt(mk().ifte_expr(c, then, Some(else_)))],
3323-
self.panic_or_err("Conditional expression is not supposed to be used"),
3324-
)
3325-
});
3326-
res.merge_unsafe(is_unsafe);
33273326
Ok(res)
33283327
} else {
33293328
let then = lhs.to_block();
@@ -3345,7 +3344,7 @@ impl<'c> Translation<'c> {
33453344
if ctx.is_unused() {
33463345
let mut lhs = self.convert_condition(ctx, false, lhs)?;
33473346
let rhs = self.convert_expr(ctx, rhs, None)?;
3348-
lhs.merge_unsafe(rhs.is_unsafe());
3347+
lhs = lhs.merge_unsafe(rhs.is_unsafe());
33493348

33503349
Ok(lhs.and_then(|val| {
33513350
WithStmts::new(

c2rust-transpile/src/translator/operators.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,11 @@ impl<'c> Translation<'c> {
408408
let assign_stmt = match op {
409409
// Regular (possibly volatile) assignment
410410
Assign if !is_volatile => WithStmts::new_val(mk().assign_expr(write, rhs)),
411-
Assign => WithStmts::new_unsafe_val(self.volatile_write(
411+
Assign => WithStmts::new_val(self.volatile_write(
412412
write,
413413
initial_lhs_type_id,
414414
rhs,
415-
)?),
415+
)?).set_unsafe(),
416416

417417
// Anything volatile needs to be desugared into explicit reads and writes
418418
op if is_volatile || is_unsigned_arith => {
@@ -459,9 +459,9 @@ impl<'c> Translation<'c> {
459459
#[allow(clippy::let_and_return /* , reason = "block is large, so variable name helps" */)]
460460
let write = if is_volatile {
461461
val.and_then_try(|val| {
462-
TranslationResult::Ok(WithStmts::new_unsafe_val(
462+
TranslationResult::Ok(WithStmts::new_val(
463463
self.volatile_write(write, initial_lhs_type_id, val)?,
464-
))
464+
).set_unsafe())
465465
})?
466466
} else {
467467
val.map(|val| mk().assign_expr(write, val))
@@ -593,7 +593,7 @@ impl<'c> Translation<'c> {
593593
offset = mk().binary_expr(BinOp::Div(Default::default()), offset, div);
594594
}
595595

596-
Ok(WithStmts::new_unsafe_val(mk().cast_expr(offset, ty)))
596+
Ok(WithStmts::new_val(mk().cast_expr(offset, ty)).set_unsafe())
597597
} else if let &CTypeKind::Pointer(pointee) = lhs_type {
598598
Ok(self.convert_pointer_offset(lhs, rhs, pointee.ctype, true, false))
599599
} else if lhs_type.is_unsigned_integral_type() {
@@ -748,13 +748,12 @@ impl<'c> Translation<'c> {
748748
mk().assign_expr(write, val)
749749
};
750750

751-
let mut val = WithStmts::new(
751+
let val = WithStmts::new(
752752
vec![save_old_val, mk().expr_stmt(assign_stmt)],
753753
mk().ident_expr(val_name),
754-
);
755-
if is_unsafe {
756-
val.set_unsafe();
757-
}
754+
)
755+
.merge_unsafe(is_unsafe);
756+
758757
Ok(val)
759758
},
760759
)

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: 6 additions & 8 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");
@@ -334,17 +333,16 @@ impl<'c> Translation<'c> {
334333
mk().call_expr(mk().ident_expr(fn_call_name), params)
335334
};
336335

337-
let mut val = if ctx.is_used() {
336+
let val = if ctx.is_used() {
338337
WithStmts::new_val(call)
339338
} else {
340339
WithStmts::new(
341340
vec![mk().expr_stmt(call)],
342341
self.panic_or_err("No value for unused shuffle vector return"),
343342
)
344343
};
345-
val.merge_unsafe(is_unsafe);
346344

347-
Ok(val)
345+
Ok(val.merge_unsafe(is_unsafe))
348346
})
349347
}
350348

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: 9 additions & 15 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>,
@@ -95,12 +87,14 @@ impl<T> WithStmts<T> {
9587
}
9688
}
9789

98-
pub fn set_unsafe(&mut self) {
90+
pub fn set_unsafe(mut self) -> Self {
9991
self.is_unsafe = true;
92+
self
10093
}
10194

102-
pub fn merge_unsafe(&mut self, is_unsafe: bool) {
95+
pub fn merge_unsafe(mut self, is_unsafe: bool) -> Self {
10396
self.is_unsafe = self.is_unsafe || is_unsafe;
97+
self
10498
}
10599

106100
pub fn into_stmts(self) -> Vec<Stmt> {
@@ -142,13 +136,15 @@ impl<T> WithStmts<T> {
142136
self.is_unsafe
143137
}
144138

145-
pub fn add_stmt(&mut self, stmt: Stmt) {
139+
pub fn add_stmt(mut self, stmt: Stmt) -> Self {
146140
self.stmts.push(stmt);
141+
self
147142
}
148143

149-
pub fn prepend_stmts(&mut self, mut stmts: Vec<Stmt>) {
144+
pub fn prepend_stmts(mut self, mut stmts: Vec<Stmt>) -> Self {
150145
stmts.append(&mut self.stmts);
151146
self.stmts = stmts;
147+
self
152148
}
153149

154150
pub fn is_pure(&self) -> bool {
@@ -211,8 +207,6 @@ impl<T> FromIterator<WithStmts<T>> for WithStmts<Vec<T>> {
211207
stmts.append(val.stmts_mut());
212208
res.push(val.into_value());
213209
}
214-
let mut translation = WithStmts::new(stmts, res);
215-
translation.merge_unsafe(is_unsafe);
216-
translation
210+
WithStmts::new(stmts, res).merge_unsafe(is_unsafe)
217211
}
218212
}

0 commit comments

Comments
 (0)