Skip to content

Commit 35eee04

Browse files
authored
transpile: Remove static_initializer_is_unsafe (#1745)
- Depends on #1644. This was already slated for removal in a TODO comment, and it doesn't seem like it's actually needed. `WithStmts` is handling everything already. Removing this function helps a little towards #1736 as well.
2 parents cd09d2b + 6f8ad2e commit 35eee04

2 files changed

Lines changed: 11 additions & 59 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,53 +1631,6 @@ impl<'c> Translation<'c> {
16311631
}
16321632
}
16331633

1634-
fn static_initializer_is_unsafe(&self, expr_id: Option<CExprId>, qty: CQualTypeId) -> bool {
1635-
// SIMD types are always unsafe in statics
1636-
match self.ast_context.resolve_type(qty.ctype).kind {
1637-
CTypeKind::Vector(..) => return true,
1638-
CTypeKind::ConstantArray(ctype, ..) => {
1639-
let kind = &self.ast_context.resolve_type(ctype).kind;
1640-
1641-
if let CTypeKind::Vector(..) = kind {
1642-
return true;
1643-
}
1644-
}
1645-
_ => {}
1646-
}
1647-
1648-
// Get the initializer if there is one
1649-
let expr_id = match expr_id {
1650-
Some(expr_id) => expr_id,
1651-
None => return false,
1652-
};
1653-
1654-
// Look for code which can only be translated unsafely
1655-
let iter = DFExpr::new(&self.ast_context, expr_id.into());
1656-
1657-
for i in iter {
1658-
let expr_id = match i {
1659-
SomeId::Expr(expr_id) => expr_id,
1660-
_ => unreachable!("Found static initializer type other than expr"),
1661-
};
1662-
1663-
use CExprKind::*;
1664-
match self.ast_context[expr_id].kind {
1665-
ImplicitCast(_, _, cast_kind, _, _) | ExplicitCast(_, _, cast_kind, _, _) => {
1666-
use CastKind::*;
1667-
match cast_kind {
1668-
IntegralToPointer | FunctionToPointerDecay | PointerToIntegral => {
1669-
return true;
1670-
}
1671-
_ => {}
1672-
}
1673-
}
1674-
_ => {}
1675-
}
1676-
}
1677-
1678-
false
1679-
}
1680-
16811634
/// The purpose of this function is to decide on whether or not a static initializer's
16821635
/// translation is able to be compiled as a valid rust static initializer
16831636
fn static_initializer_is_uncompilable(
@@ -2126,12 +2079,6 @@ impl<'c> Translation<'c> {
21262079
let mut items = init.stmts_to_items().ok_or_else(|| {
21272080
format_err!("Expected only item statements in static initializer")
21282081
})?;
2129-
2130-
// TODO: Replace this by relying entirely on
2131-
// WithStmts.is_unsafe() of the translated variable
2132-
if self.static_initializer_is_unsafe(initializer, typ) {
2133-
init.set_unsafe()
2134-
}
21352082
let init = init.wrap_unsafe().to_pure_expr().unwrap();
21362083
let item = static_def.span(span).static_item(new_name, ty, init);
21372084
items.push(item);

c2rust-transpile/src/translator/simd.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,13 @@ impl<'c> Translation<'c> {
295295
) -> TranslationResult<WithStmts<Box<Expr>>> {
296296
let param_translation = self.convert_exprs(ctx, ids, None)?;
297297
param_translation.and_then_try(|mut params| {
298+
let mut is_unsafe = false;
299+
298300
// When used in a const context, we cannot call the standard functions since they
299301
// are not const and so we are forced to transmute
300302
let call = if ctx.is_const {
303+
is_unsafe = true;
301304
let tuple = mk().tuple_expr(params);
302-
303305
transmute_expr(mk().infer_ty(), mk().infer_ty(), tuple)
304306
} else {
305307
let fn_call_name = match (&self.ast_context[ctype].kind, len) {
@@ -332,14 +334,17 @@ impl<'c> Translation<'c> {
332334
mk().call_expr(mk().ident_expr(fn_call_name), params)
333335
};
334336

335-
if ctx.is_used() {
336-
Ok(WithStmts::new_val(call))
337+
let mut val = if ctx.is_used() {
338+
WithStmts::new_val(call)
337339
} else {
338-
Ok(WithStmts::new(
340+
WithStmts::new(
339341
vec![mk().expr_stmt(call)],
340342
self.panic_or_err("No value for unused shuffle vector return"),
341-
))
342-
}
343+
)
344+
};
345+
val.merge_unsafe(is_unsafe);
346+
347+
Ok(val)
343348
})
344349
}
345350

0 commit comments

Comments
 (0)